浅谈在应用CRectTracker类的程序中响应WM_LBUTTONUP消息(2)
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测试通过。
- 上一篇:Socket模拟SOAP消息
- 下一篇:用MFC的消息映像实现动态菜单