各位大神好,我用PreTranslateMessage(MSG* pMsg)写了个消息处理函数,但是为什么鼠标弹起的消息不能响应呢?
就是鼠标弹起后,没有去执行鼠标弹起的处理函数,代码如下:
BOOL CNewStatic::PreTranslateMessage(MSG* pMsg) 
{
// TODO: Add your specialized code here and/or call the base class
 CPoint point;
 point.x=LOWORD(pMsg->lParam);
 point.y=HIWORD(pMsg->lParam);     
     if(pMsg->message==WM_LBUTTONDOWN)
 {  
KEYDOWN=TRUE;
        cvCopy(org,img);//为了备份现有文件
sprintf(temp,"(%d,%d)",point.x,point.y);  
pre_pt = cvPoint(point.x,point.y);  
cvPutText(img,temp, pre_pt, &font, cvScalar(0,0, 0, 255));  
cvCircle( img, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );  
RRDrawPicToHDC(img);
cvCopy(img,tmp);
Sleep(100);
 }
     else if(pMsg->message==WM_MOUSEMOVE)
 {
        
        if(KEYDOWN==TRUE)
 { 
  cvCopy(tmp,img);
         sprintf(temp,"(%d,%d)",point.x,point.y);  
         cur_pt = cvPoint(point.x,point.y);        
         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
         cvRectangle(img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );  
         RRDrawPicToHDC(img);
}
    if(KEYDOWN==FALSE)
{
 cvCopy(tmp,img);
 sprintf(temp,"(%d,%d)",point.x,point.y);  
 cur_pt = cvPoint(point.x,point.y);        
 cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
         ////RRDrawPicToHDC(img);
 }  }
 else if(pMsg->message==WM_LBUTTONUP)
 {
KEYDOWN=FALSE;
        cvCopy(tmp,img);
        sprintf(temp,"(%d,%d)",point.x,point.y);  
        cur_pt = cvPoint(point.x,point.y);        
        cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
        cvCircle( img, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );  
        cvRectangle( img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );  
        RRDrawPicToHDC(img); 
cvCopy(tmp,img);
        int width=abs(pre_pt.x-cur_pt.x);  
        int height=abs(pre_pt.y-cur_pt.y); 
aaa=width;//截取的图像宽度
bbb=height;//截取的图像高度 
        if(width==0 || height==0)  
        {  
            return 0;
          
        }  
        dst=cvCreateImage(cvSize(width,height),org->depth,org->nChannels);  
        CvRect rect;  
        if(pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y)  
        {  
            rect=cvRect(pre_pt.x,pre_pt.y,width,height);  
        }  
        else if(pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y)  
        {  
            rect=cvRect(cur_pt.x,pre_pt.y,width,height);  
        }  
        else if(pre_pt.x>cur_pt.x && pre_pt.y>cur_pt.y)  
        {  
            rect=cvRect(cur_pt.x,cur_pt.y,width,height);  
        }  
        else if(pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y)  
        {  
            rect=cvRect(pre_pt.x,cur_pt.y,width,height);  
        }  
        cvSetImageROI(org,rect); 
cvCopy(org,dst);
cvNamedWindow("sunlu",CV_WINDOW_AUTOSIZE);
cvShowImage("sunlu",dst);
        cvResetImageROI(org);    
        cvSaveImage("D:\\第一次截取的图像.bmp",dst); 
 }
return CStatic::PreTranslateMessage(pMsg);
}
但是当我删掉鼠标移动消息之后就可以响应鼠标左键弹起消息,请问是什么原因啊》

解决方案 »

  1.   

    备注一下:我这里面调用了open cv,里面的绘图函数如下:
    void CNewStatic::RRDrawPicToHDC(IplImage* shishi)
    {    
    CDC *pDC = this->GetDC();
    HDC hDC = pDC->GetSafeHdc();
    CRect rect;
    this->GetClientRect(&rect);    
      
    CvvImage cimg;
    cimg.CopyOf(shishi);
    cimg.DrawToHDC(hDC,&rect);
    ReleaseDC(pDC);
      

  2.   

    你直接添加WM_LBUTTONDOWN,WM_MOUSEMOVE这几个消息,然后在里面处理就可以了
      

  3.   

    开始我也是分别响应啊
    void CNewStatic::OnLButtonUp(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
        Sleep(100);
        KEYDOWN=FALSE;
    cvCopy(tmp,img);
            sprintf(temp,"(%d,%d)",point.x,point.y);  
            cur_pt = cvPoint(point.x,point.y);        
            cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
            cvCircle( img, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );  
            cvRectangle( img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );  
            RRDrawPicToHDC(img); 
    cvCopy(tmp,img);
            int width=abs(pre_pt.x-cur_pt.x);  
            int height=abs(pre_pt.y-cur_pt.y); 
    aaa=width;//截取的图像宽度
    bbb=height;//截取的图像高度 
            if(width==0 || height==0)  
            {  
                AfxMessageBox("error");
              
            }  
            dst=cvCreateImage(cvSize(width,height),org->depth,org->nChannels);  
            CvRect rect;  
            if(pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y)  
            {  
                rect=cvRect(pre_pt.x,pre_pt.y,width,height);  
            }  
            else if(pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y)  
            {  
                rect=cvRect(cur_pt.x,pre_pt.y,width,height);  
            }  
            else if(pre_pt.x>cur_pt.x && pre_pt.y>cur_pt.y)  
            {  
                rect=cvRect(cur_pt.x,cur_pt.y,width,height);  
            }  
            else if(pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y)  
            {  
                rect=cvRect(pre_pt.x,cur_pt.y,width,height);  
            }  
            cvSetImageROI(org,rect); 
    cvCopy(org,dst);
    cvNamedWindow("sunlu",CV_WINDOW_AUTOSIZE);
    cvShowImage("sunlu",dst);
            cvResetImageROI(org);    
            cvSaveImage("D:\\第一次截取的图像.bmp",dst); 可是开始时鼠标左键按下能响应,按下左键时移动也能响应,再弹起鼠标时就不能响应,有点类似于失去鼠标焦点,但我又没有更改鼠标焦点,为什么会这样啊?还有会不会是消息阻塞啊    
    Sleep(100);
    CStatic::OnLButtonUp(nFlags, point);
    }void CNewStatic::OnMouseMove(UINT nFlags, CPoint point) 
    {      
    // TODO: Add your message handler code here and/or call default
      
      
     if(nFlags & MK_LBUTTON==MK_LBUTTON)
     { 
      cvCopy(tmp,img);
             sprintf(temp,"(%d,%d)",point.x,point.y);  
             cur_pt = cvPoint(point.x,point.y);        
             cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
             cvRectangle(img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );  
             RRDrawPicToHDC(img);
     }
         else
    {
      cvCopy(tmp,img);
     sprintf(temp,"(%d,%d)",point.x,point.y);  
     cur_pt = cvPoint(point.x,point.y);        
     cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
             RRDrawPicToHDC(img);
    }
            CStatic::OnMouseMove(nFlags, point);
    }*/void CNewStatic::RRDrawPicToHDC(IplImage* shishi)
    {    
    CDC *pDC = this->GetDC();
    HDC hDC = pDC->GetSafeHdc();
    CRect rect;
    this->GetClientRect(&rect);    
      
    CvvImage cimg;
    cimg.CopyOf(shishi);
    cimg.DrawToHDC(hDC,&rect);
    ReleaseDC(pDC); /* BITMAPINFO* bi =(BITMAPINFO *) new BYTE[sizeof(BITMAPINFOHEADER)];
        bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bi->bmiHeader.biWidth = rect.Width();
    bi->bmiHeader.biHeight     = rect.Height();
    bi->bmiHeader.biBitCount     = 24;
    bi->bmiHeader.biPlanes     = 1;
    bi->bmiHeader.biCompression     = BI_RGB;
    bi->bmiHeader.biSizeImage = 0;
    bi->bmiHeader.biXPelsPerMeter = 0;
    bi->bmiHeader.biYPelsPerMeter = 0;
    bi->bmiHeader.biClrUsed     = 0;
    bi->bmiHeader.biClrImportant = 0;
        //SetStretchBltMode(hDC,HALFTONE);
    this->OnPaint();
        ::StretchDIBits(hDC,0,0,rect.Width(),rect.Height(),
                   0,0,rect.Width(),rect.Height(),(CONST VOID *)shishi->imageData,(BITMAPINFO*)bi,
         DIB_RGB_COLORS,SRCCOPY);
         ReleaseDC(pDC);*/}