写了一个简单一点的控件,就是一个矩形框,我想让它在容器内能用鼠标任意托动,结果发现鼠标按下去和拖动的时候控件的界面显示不出来,拖动完毕后可以显示出来,这是怎么回事?
void CColorLampCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
// TODO: Replace the following code with your own drawing code.
CRect rect;
rect.SetRect(rcBounds.left,rcBounds.top,rcBounds.left+rcBounds.Width(),rcBounds.top+rcBounds.Height());
CBrush brush;
CBrush* oldBrush;
brush.CreateSolidBrush(RGB(255,0,0));
oldBrush = pdc->SelectObject(&brush);
pdc->FillRect(&rect,&brush);
pdc->Rectangle(&rect);
pdc->SelectObject(oldBrush);
brush.DeleteObject();
if (!IsOptimizedDraw())
{
// The container does not support optimized drawing. // TODO: if you selected any GDI objects into the device context *pdc,
// restore the previously-selected objects here.
// For more information, please see MFC technical note #nnn,
// "Optimizing an ActiveX Control".
}
}
void CColorLampCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default SetCapture();
m_iMouseX = point.x;
m_iMouseY = point.y;

GetClientRect(&m_rect);
::ClientToScreen(m_hWnd,&m_rect.TopLeft());
m_oldPoint = m_rect.TopLeft();

MoveWindow(0,0,m_iHeight,m_iWidth);
GetClientRect(&m_rect);
::ClientToScreen(m_hWnd,&m_rect.TopLeft());

m_bIsMoving = true;

COleControl::OnLButtonDown(nFlags, point);
}
void CColorLampCtrl::OnLButtonUp(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
ReleaseCapture();
m_bIsMoving = false;
COleControl::OnLButtonUp(nFlags, point);
}
void CColorLampCtrl::OnMouseMove(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
if(m_bIsMoving)
{
CPoint p = point;
ClientToScreen(&p);
MoveWindow(p.x-m_rect.left-m_iMouseX,p.y-m_rect.top-m_iMouseY,m_iHeight,m_iWidth);
TRACE("mouse move\n");
}

COleControl::OnMouseMove(nFlags, point);
}

解决方案 »

  1.   

    从上面代码看你是自已移动的窗体,并在Up 事件中ReleaseCapture() 你把它放到move 中
    但这么做不对呀,你不应自已在控件中移动,应在父中移动才对
      

  2.   

    刷新可能造成这个问题!~
    在OnPaint\OnDraw中,详细查看你的代码.
      

  3.   

    鼠标移动的过程中没有响应OnDraw()造成的
    就是说你必须在鼠标移动的过程中,绘制矩形窗口.
    可以在OnMouseMove()强制刷新,或采用OnPaint()事件来刷新