1.在OnInitDialog()函数里添加
m_rectTracker.m_nStyle=CRectTracker::resizeOutside|CRectTracker::solidLine;//设置橡皮筋的线型
m_rect.left=0;
m_rect.right=100;
m_rect.bottom=100;
m_rect.top=0;
2.在OnPaint()函数里添加
void CModifyRectDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
CClientDC dc(this);
//设置边界
m_rectTracker.m_rect=m_rect;
//坐标映射;L逻辑 ---> D设备
dc.LPtoDP(&m_rectTracker.m_rect);
//画图形的边界
//if(m_IsDraw) m_rectTracker.Draw(&dc);
//dc.SelectObject(oldBrush);
CPen pen(PS_SOLID, 2, RGB(0, 255, 0));//创建画笔为绿色
dc.SelectObject(&pen);//在dc上放置画笔
//dc.SetROP2(R2_NOT);
dc.SelectStockObject(NULL_BRUSH);//使画出的矩形区域为透明
dc.Rectangle(m_rectTracker.m_rect);
//dc.DrawEdge(&m_rectTracker.m_rect,EDGE_SUNKEN, BF_RECT);
if(m_IsDraw)
m_rectTracker.Draw(&dc);//若选择了该区域,则显示边框以及8个调整点
pen.DeleteObject();
//DeleteObject(hBrush);
}
}
在这里初始化一个矩形,用画笔将它画在控件上
3.
void CModifyRectDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//ModifyCursor(point);
int ret=m_rectTracker.HitTest(point);
if(ret>=0)//判断是否要画调整边框
{
m_IsDraw=TRUE;
CClientDC dc(this);
InvalidateRect(&m_rectTracker.m_rect);//重绘窗口
}
else
{
m_IsDraw=FALSE;
CRect rect=m_rectTracker.m_rect;
InvalidateRect(&rect);
//Invalidate();
//Invalidate();
//OutputDebugString(TEXT("aaa"));
}
//此函数会拦截掉鼠标消息,所以要在这个函数后面写鼠标弹起要处理的事件.
if(m_rectTracker.Track(this,point,false,NULL))
{
//鼠标弹起后才执行下面的程序
CClientDC dc(this);
CRect rect=m_rect;
m_rect=m_rectTracker.m_rect;
//dc.DPtoLP(&m_rectTracker.m_rect);
//Invalidate(TRUE);
InvalidateRect(&rect,0);
//Invalidate(0);
//OutputDebugString(TEXT("bbb"));
ClipCursor(NULL);
}
else
{
//鼠标弹起后执行下面的程序.
ClipCursor(NULL);
//OutputDebugString(TEXT("ddd"));
}
CDialog::OnLButtonDown(nFlags, point);
}
void CModifyRectDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDialog::OnMouseMove(nFlags, point);
}
//用这种方法可以自定义光标形状,但是会有光标闪烁.
void CModifyRectDlg::ModifyCursor(CPoint point)
{
static int CurStyle=-2;
int ret=m_rectTracker.HitTest(point);
switch(ret)
{
case CRectTracker::hitTopLeft:
case CRectTracker::hitBottomRight:
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZENWSE));
break;
case CRectTracker::hitTopRight:
case CRectTracker::hitBottomLeft:
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZENESW));
break;
case CRectTracker::hitRight:
case CRectTracker::hitLeft:
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZEWE));
break;
case CRectTracker::hitBottom:
case CRectTracker::hitTop:
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZENS));
break;
case CRectTracker::hitMiddle:
SetCursor(LoadCursor(NULL,IDC_SIZEALL));
break;
default:
break;
}
}
BOOL CModifyRectDlg::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此添加专用代码和/或调用基类
return CDialog::PreCreateWindow(cs);
}
BOOL CModifyRectDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//SetCursor(LoadCursor(NULL,IDC_SIZEALL));
//这里控制是否改变光标.
if(m_IsDraw && pWnd==this && m_rectTracker.SetCursor(this,nHitTest))//
{
return TRUE;
}
return CDialog::OnSetCursor(pWnd, nHitTest, message);
}
4.结果显示如下