CMyView::CMyVi ():m_sizeEllipse(100,-100);
m_PointTopLeft(0,0);
m_sizeOffset(0,0)
{
m_bCaptured = FALSE;
}
然后分别添加鼠标消息.WM_LBUTTONDOWN,WM_LBUTTONUP,WM_MOUSEMOVE.首先,在左键按下的鼠标消息里,先判断点击点是否在我们的圆形区域里.如果是,则计算点击点到图形左上角的偏移量.代码如下:
CMyView::OnLButtonDown(UINT nFlags,CPoint point)
{
CRect rectEllipse(m_pointTopLeft,m_sizeEllipse);
CRgn circle;
CClientDC dc(this);
OnPrepareDC(&dc);
dc.LpToDp(rectEllipse);
circle.CreateEllipseRgnIndirect(rectEllipse);
if(circle.PtInRegion(point))
{
SetCapture();
m_bCaptured = TRUE;
CPoint pointTopLeft(m_pointTopLeft);
dc.LpToDp(&pointTopLeft);
m_sizeOffset = point - pointTopLeft;
SetCursor(LoadCursor(NULL,IDC_CROSS));
}
在WM_LBUTTONUP事件中添加如下代码.
CMyView::OnLButtonUp(UINT nFlag,CPoint point)'
{
if(m_Captured)
{
ReleaseCapture();
m_Catpured = TRUE;
}
}
在WM_MOUSEMOVE事件中添加如下代码:
CMYView::OnMouseMove(UINT nFlag,CPoint point)
{
if(m_Captured)
{
CClientDC dc(this);
OnPrepareDC(&dc);
CRect rectOld(m_pointLeftTop,m_sizeEllipse);
InvalidateRect(rectOld,TRUE);
m_pointTopLeft = point - m_sizeOffset;
dc.DpToLp(&m_pointTopLeft);
CRect rectNew(m_pointTopLeft,m_sizeEllipse);
dc.LptoDP(rectnew);
InvalidateRect(rectNew,TRUE);
}
}
最后在OnDraw函数,重新画圆:
pDC->Ellipse(CRect(m_pointTopLeft,m_sizeEllipse));
整个过程基本上实现了.有的地方需要整理点。.