我有一个波形是在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控件上面呢?

解决方案 »

  1.   

    CTestOScopeDlg::OnMouseMove(UINT nFlags, CPoint point)
    中的point是针对CTestOScopeDlg客户区的GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);
    得到的rect是IDC_OSCOPE客户区的两者参考点不一样,可以都校正到屏幕坐标下再计算。ClientToScreen(point);
    GetDlgItem(IDC_OSCOPE)->ClientToScreen(rect);
    if(PtInRect(rect,CPoint(point)))
    {
    ....
    }
      

  2.   


    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);
    }
      

  3.   

    你在dialog的mouseMove里写代码,鼠标移动到Picture控件上根本不会响应,
    有一个做法应该可以:
    把代码移动到PretranslateMessage里去里面有个msg结构体,分别写if(msg == WM_MOUSEMOVE)
    {
      OnMouseMove();
    }
    esle if(msg == WM_LBUTTONDOWN)
    {
      OnLButton();
    }中间可能要用到坐标转换,试试再问吧
      

  4.   

    因为最原始的消息是从最上层开始判断后往下层窗口传的,这一层可以获得所有的mousemove消息~在这里写应该没问题
      

  5.   

    //GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);
    ClientToScreen(&point);
    GetDlgItem(IDC_OSCOPE)->ClientToScreen(rect);该成这样后直接没反应了,拖动鼠标没有任何反应----------------------
    GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);还要保留,要不rect就是一个随机数了。
      

  6.   


    dialog能收到消息,因为Picture控件没有对这些消息进行处理,其它控件的话就不行了。
      

  7.   


    LS的意思是?
    那怎么在Picture控件中画矩形呢?
    我修改成 CRect rect;
    GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);
    ClientToScreen(&point);
    GetDlgItem(IDC_OSCOPE)->ClientToScreen(&point);
            GetDlgItem(IDC_OSCOPE)->ClientToScreen(rect);
    还是不行哦
      

  8.   

    不理解我的意思么?你到mousemove里设置断点,当你在picture上动的时候,不会运行到断点,也就是代码不会执行,要换到PreTranslateMessage里~
      

  9.   


    不是校正坐标的问题,要响应OnMouseMove也改是在控件里响应
    方法很多种~
      

  10.   


    你自己试一下,肯定能运行到断点。这是picture特性决定的。
      

  11.   


    应为
    CRect rect;
    GetDlgItem(IDC_OSCOPE)->GetClientRect(rect);
    GetDlgItem(IDC_OSCOPE)->ClientToScreen(rect);
    ClientToScreen(&point);
      

  12.   

    刚才试了一下,把你的代码改成如下形式了:
    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);
    }
      

  13.   

    LS的兄弟,谢谢你了啊
    不过我运行后还是没反应
    我还增加了这段代码:
    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进去帮忙调试一下
    分数我可以另外加给你
      

  14.   

    把你在PreTranslateMessage中加的代码都去掉就行了,我在VC6中试过了,你把那三个函数替换了,结果正常。
      

  15.   

    你可以去资源中下载例程,这次CSDN发彪了,刚上传两三分钟就能看到了。
    http://download.csdn.net/source/2562606