我有一个波形是在Picture控件上面做显示的
现在的要求是:在波形上可以拖动鼠标画一个矩形void CTestOScopeDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
if(m_bStart)
{
GetDlgItem(IDC_OSCOPE)->SetCapture();
m_bCaptured=true;
m_nStartPos=m_nEndPos=point;
}
CDialog::OnLButtonDown(nFlags, point);
}
void CTestOScopeDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
if(m_bCaptured)
{
m_nEndPos=point;
m_bCaptured=false;
m_bStart=false;
ReleaseCapture();
}
CDialog::OnLButtonUp(nFlags, point);
}void CTestOScopeDlg::OnMouseMove(UINT nFlags, CPoint point)
{ if(m_bCaptured)
{
CRect rect;
GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);
if(PtInRect(rect,CPoint(point)))
{
(GetDlgItem(IDC_OSCOPE)->GetDC())->Rectangle(m_nStartPos.x,m_nStartPos.y,point.x,point.y);
}
}
CDialog::OnMouseMove(nFlags, point);
}为什么根本就没办法把矩形区域限定在Picture控件上面呢?
现在的要求是:在波形上可以拖动鼠标画一个矩形void CTestOScopeDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
if(m_bStart)
{
GetDlgItem(IDC_OSCOPE)->SetCapture();
m_bCaptured=true;
m_nStartPos=m_nEndPos=point;
}
CDialog::OnLButtonDown(nFlags, point);
}
void CTestOScopeDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
if(m_bCaptured)
{
m_nEndPos=point;
m_bCaptured=false;
m_bStart=false;
ReleaseCapture();
}
CDialog::OnLButtonUp(nFlags, point);
}void CTestOScopeDlg::OnMouseMove(UINT nFlags, CPoint point)
{ if(m_bCaptured)
{
CRect rect;
GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);
if(PtInRect(rect,CPoint(point)))
{
(GetDlgItem(IDC_OSCOPE)->GetDC())->Rectangle(m_nStartPos.x,m_nStartPos.y,point.x,point.y);
}
}
CDialog::OnMouseMove(nFlags, point);
}为什么根本就没办法把矩形区域限定在Picture控件上面呢?
中的point是针对CTestOScopeDlg客户区的GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);
得到的rect是IDC_OSCOPE客户区的两者参考点不一样,可以都校正到屏幕坐标下再计算。ClientToScreen(point);
GetDlgItem(IDC_OSCOPE)->ClientToScreen(rect);
if(PtInRect(rect,CPoint(point)))
{
....
}
void CTestOScopeDlg::OnMouseMove(UINT nFlags, CPoint point)
{
if(m_bCaptured)
{
CRect rect;
//GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);
ClientToScreen(&point);
GetDlgItem(IDC_OSCOPE)->ClientToScreen(rect);该成这样后直接没反应了,拖动鼠标没有任何反应
if(PtInRect(rect,CPoint(point)))
{
HDC hdc=::GetDC(GetDlgItem(IDC_OSCOPE)->GetSafeHwnd());
SetROP2(hdc,R2_NOTXORPEN);
HPEN hpen;
hpen=CreatePen(PS_SOLID,2,RGB(255,0,0));
SelectObject(hdc,hpen);
SelectObject(hdc,GetStockObject(NULL_BRUSH));
::Rectangle(hdc,m_nStartPos.x,m_nStartPos.y,m_nEndPos.x,m_nEndPos.y);
m_nEndPos=point;
::Rectangle(hdc,m_nStartPos.x,m_nStartPos.y,m_nEndPos.x,m_nEndPos.y);
::ReleaseDC(GetDlgItem(IDC_OSCOPE)->GetSafeHwnd(),hdc);
::DeleteObject(hpen);
}
}
// GetDlgItem(IDC_OSCOPE)->Invalidate();
CDialog::OnMouseMove(nFlags, point);
}
有一个做法应该可以:
把代码移动到PretranslateMessage里去里面有个msg结构体,分别写if(msg == WM_MOUSEMOVE)
{
OnMouseMove();
}
esle if(msg == WM_LBUTTONDOWN)
{
OnLButton();
}中间可能要用到坐标转换,试试再问吧
ClientToScreen(&point);
GetDlgItem(IDC_OSCOPE)->ClientToScreen(rect);该成这样后直接没反应了,拖动鼠标没有任何反应----------------------
GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);还要保留,要不rect就是一个随机数了。
dialog能收到消息,因为Picture控件没有对这些消息进行处理,其它控件的话就不行了。
LS的意思是?
那怎么在Picture控件中画矩形呢?
我修改成 CRect rect;
GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);
ClientToScreen(&point);
GetDlgItem(IDC_OSCOPE)->ClientToScreen(&point);
GetDlgItem(IDC_OSCOPE)->ClientToScreen(rect);
还是不行哦
不是校正坐标的问题,要响应OnMouseMove也改是在控件里响应
方法很多种~
你自己试一下,肯定能运行到断点。这是picture特性决定的。
应为
CRect rect;
GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);
GetDlgItem(IDC_OSCOPE)->ClientToScreen(rect);
ClientToScreen(&point);
void CTestOScopeDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bCaptured)
{
ReleaseCapture();
m_bCaptured = FALSE;
} CDialog::OnLButtonUp(nFlags, point);
}void CTestOScopeDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect rect;
GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);
GetDlgItem(IDC_OSCOPE)->ClientToScreen(rect);
ClientToScreen(&point); if(rect.PtInRect(point))
{
SetCapture();
m_bCaptured = TRUE; GetDlgItem(IDC_OSCOPE)->Invalidate(); //消除上一次绘制的矩形
GetDlgItem(IDC_OSCOPE)->ScreenToClient(&point);
m_endPos = m_startPos = point;
}
CDialog::OnLButtonDown(nFlags, point);
}void CTestOScopeDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bCaptured)
{
ClientToScreen(&point); CRect rect;
GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);
GetDlgItem(IDC_OSCOPE)->ClientToScreen(rect);
if(rect.PtInRect(point))
{
GetDlgItem(IDC_OSCOPE)->ScreenToClient(&point); HDC hdc=::GetDC(GetDlgItem(IDC_OSCOPE)->GetSafeHwnd());
SetROP2(hdc,R2_NOTXORPEN);
HPEN hpen;
hpen=CreatePen(PS_SOLID,2,RGB(255,0,0));
SelectObject(hdc,hpen);
SelectObject(hdc,GetStockObject(NULL_BRUSH));
::Rectangle(hdc,m_startPos.x,m_startPos.y,m_endPos.x,m_endPos.y);
m_endPos = point;
::Rectangle(hdc,m_startPos.x,m_startPos.y,m_endPos.x,m_endPos.y);
::ReleaseDC(GetDlgItem(IDC_OSCOPE)->GetSafeHwnd(),hdc);
::DeleteObject(hpen);
}
}
CDialog::OnMouseMove(nFlags, point);
}
不过我运行后还是没反应
我还增加了这段代码:
BOOL CTestOScopeDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message==WM_MOUSEMOVE)
{
SendMessage(WM_MOUSEMOVE);
}
if(pMsg->message==WM_LBUTTONDOWN)
{
SendMessage(WM_LBUTTONDOWN);
}
if(pMsg->message==WM_LBUTTONUP)
{
SendMessage(WM_LBUTTONUP);
}
return CDialog::PreTranslateMessage(pMsg);
}
如果方便的话,麻烦你在对话框上放一个picture控件,然后把这些代码COPY进去帮忙调试一下
分数我可以另外加给你
http://download.csdn.net/source/2562606