画矩形其实无非就是在鼠标移动的时候,不断地画出矩形和擦除已经画的矩形
void ModelAcquire::OnMouseMove(UINT nFlags,CPoint point)
{
//TODO: 在此添加消息处理程序代码和/或调用默认值
pdc= GetDC();
pen= new CPen(PS_SOLID, 1, RGB(255, 0, 0)); //创建画笔
DeleteObject(pdc->SelectObject(pen));//把创建的画笔选进设备描述表
if(isLButton== TRUE && CursorIsInControl(point, IDC_MODEL_ALL) == TRUE
&&isRestButton == TRUE)//按下左键并且在控件区域内
{
HCURSORhCur = LoadCursor( NULL, IDC_CROSS);
::SetCursor(hCur);
DrawRect(pdc, point);
}
else{
HCURSORhCur = LoadCursor( NULL, IDC_ARROW) ;
::SetCursor(hCur);
}
//在display图片控制区域按下了左键
if(isLButtonInDisplayPicCtl == TRUE && CursorIsInControl(point,IDC_MODEL_DISPLAY) == TRUE) {
HCURSORhCur = LoadCursor( NULL, IDC_CROSS);
::SetCursor(hCur);
DrawFixRect(pdc,point);
}
CDialogEx::OnMouseMove(nFlags,point);
}
void ModelAcquire::OnLButtonUp(UINT nFlags,CPoint point)
{
//TODO: 在此添加消息处理程序代码和/或调用默认值
if(isLButton == TRUE) {
isLButton= FALSE;
m_oldPoint.x= 0;
m_oldPoint.y= 0;
isRestButton= FALSE;
CBrush*pBrushOld = (CBrush*)pdc->SelectStockObject(NULL_BRUSH);
pdc->Rectangle(m_allRect );
pdc->SelectObject(pBrushOld);
}
if(isLButtonInDisplayPicCtl == TRUE && CommonFunc::RectInRect(m_disRect,m_disPicCtlRect) == TRUE) {
isLButtonInDisplayPicCtl= FALSE;
CBrush*pBrushOld = (CBrush*)pdc->SelectStockObject(NULL_BRUSH);
pdc->Rectangle(m_disRect );
pdc->SelectObject(pBrushOld);
//擦除矩形框
Invalidate();//重绘,在OnPaint函数中画出
isLButtonUp= TRUE;
}
CDialogEx::OnLButtonUp(nFlags,point);
}
void ModelAcquire::OnLButtonDown(UINTnFlags, CPoint point)
{
//TODO: 在此添加消息处理程序代码和/或调用默认值
if(CursorIsInControl(point,IDC_MODEL_ALL) == TRUE)//鼠标是否在控件范围之内
{
isLButton= TRUE;
m_sPoint= point;
}
if(CursorIsInControl(point,IDC_MODEL_DISPLAY) == TRUE)//鼠标是否在控件范围之内
{
isLButtonInDisplayPicCtl= TRUE;
if(isLButtonUp == TRUE) {
//擦除矩形框
GetCursorPos(&point);
SetCursorPos(point.x- 5, point.y - 5);
m_disRect.SetRectEmpty();
Invalidate();
isLButtonUp= FALSE;
}
}
CDialogEx::OnLButtonDown(nFlags,point);
}
bool ModelAcquire::CursorIsInControl(CPointpoint, UINT ID )
{
ClientToScreen(&point);
CRectrect;
GetDlgItem(ID)->GetClientRect(rect);
GetDlgItem(ID)->ClientToScreen(rect);
if(rect.PtInRect(point))
returnTRUE;
returnFALSE;
}
void ModelAcquire::DrawRect(CDC *pdc,CPoint point)
{
//擦除矩形框
if(m_oldPoint.x != 0 ) {
m_allRect.SetRect(m_sPoint.x,m_sPoint.y, m_oldPoint.x, m_oldPoint.y);
m_allRect.NormalizeRect();
pdc->DrawFocusRect(&m_allRect);
}
//画矩形框
m_allRect.SetRect(m_sPoint.x,m_sPoint.y, point.x, point.y);
m_allRect.NormalizeRect();
pdc->DrawFocusRect(&m_allRect);
m_oldPoint= point;
}
void ModelAcquire::DrawFixRect(CDC *pdc,CPoint point)
{
if(isFirstDrawRect == TRUE) {
//擦除矩形框
m_disRect.NormalizeRect();
pdc->DrawFocusRect(&m_disRect);
}
//画矩形框
m_disRect.SetRect(point.x- FixWidth / 2, point.y - FixHeight / 2, point.x + FixWidth / 2, point.y +FixHeight / 2);
if(CommonFunc::RectInRect(m_disRect, m_disPicCtlRect) == TRUE) {
m_disRect.NormalizeRect();
pdc->DrawFocusRect(&m_disRect);
isFirstDrawRect= TRUE;
}
else
isFirstDrawRect= FALSE;
}