分类: C/C++
2008-03-17 11:14:07
void CCRectTracker_DemoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//当鼠标左键按下时
CRectTracker temp;
temp.TrackRubberBand(this,point,TRUE);
temp.m_rect.NormalizeRect();
CDialog::OnLButtonDown(nFlags, point);
}
这段代码确实实现了我所期望的效果。但在程序的另一个地方:
void CCRectTracker_DemoDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//当鼠标左键松开时
MessageBox("鼠标左键松开","松开",NULL);
CDialog::OnLButtonUp(nFlags, point);
}
OnLButtonUp()事件丝毫不理睬我鼠标的动作——MessageBox()并没有执行。仔细一想:TrackRubberBand()是以鼠标左键的松开作为结束标志的,当WM_LBUTTONUP消息发出后,TrackRubberBand()在OnLButtonUp()事件响应前就截获了该消息,所以OnLButtonUp()事件就无法正常响应了……想到这里,脑海中也就浮现了一个解决这个小问题的办法,那就是:在TrackRubberBand()后,向程序自身发送一个自己构造的WM_LBUTTONUP消息。于是将OnLButtonDown()中的代码改写成为以下形式:
void CCRectTracker_DemoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//当鼠标左键按下时
CRectTracker temp;
temp.TrackRubberBand(this,point,TRUE);
temp.m_rect.NormalizeRect();
//自己构造的WM_LBUTTONUP消息
this->SendMessage(WM_LBUTTONUP,NULL,NULL);
CDialog::OnLButtonDown(nFlags, point);
}
测试后的结果另人满意,现在我所遇到的问题迎刃而解了。在本文附带的Demo程序中,程序分别获取鼠标左键按下和松开时的坐标,然后调用修改后的OnPaint()来画出“橡皮筋矩形”,下面给出Demo中的部分关键代码:
BOOL CCRectTracker_DemoDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
//////////////////////
//改变光标的形状
if (pWnd == this && m_rectTracker.SetCursor(this, nHitTest))
return TRUE;
//////////////////////
else
return CDialog::OnSetCursor(pWnd, nHitTest, message);
}
void CCRectTracker_DemoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
///////////////////////////////////////
//如鼠标点击位置不在选框区域内,则构造一个新选框
Invalidate(TRUE);
if(m_rectTracker.HitTest(point)<0)
{
IsLButtonDown=TRUE;
CRectTracker temp;
temp.TrackRubberBand(this,point,TRUE);
temp.m_rect.NormalizeRect();
//鼠标(矩形选框)起始位置
pt_start=point;
//鼠标(矩形选框)结束位置
GetCursorPos(&pt_end);
this->SendMessage(WM_LBUTTONUP,NULL,NULL);
}
//否则重置选框大小或位置
else
{
m_rectTracker.Track(this,point,TRUE);
m_rectTracker.m_rect.NormalizeRect();
this->OnPaint();
}
///////////////////////////////////////
CDialog::OnLButtonDown(nFlags, point);
}
void CCRectTracker_DemoDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
/////////////////////////////////
//确保在构造一个新选框时才响应OnLButtonUp,否则在响应OnLButtonDblClk时会出现错误结果
if(IsLButtonDown==TRUE)
{
//MessageBox("鼠标左键松开","松开",NULL);
//由于GetCursorPos获得的pt_end是Screen坐标,故用ScreenToClient()进行转换
rect.right=pt_end.x;
rect.bottom=pt_end.y;
ScreenToClient(&rect);
pt_end.x=rect.right;
pt_end.y=rect.bottom;
//left,top,right,bottom
m_rectTracker.m_rect.SetRect(pt_start.x,pt_start.y,pt_end.x,pt_end.y);
m_rectTracker.m_rect.NormalizeRect();
this->OnPaint();
IsLButtonDown=FALSE;
}
/////////////////////////////////
CDialog::OnLButtonUp(nFlags, point);
}
void CCRectTracker_DemoDlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
////////////////////////////
MessageBox("鼠标左键双击","双击",NULL);
////////////////////////////
CDialog::OnLButtonDblClk(nFlags, point);
}
由于本人水平所限,关于CRectTracker类的详细使用方法请参阅其他资料。谢谢!
附:
以上代码在Win2000P+SP4/Win98se + VC6+SP6测试通过。
源码可从我的个人主页下载。