Buit矩形(我自己的类型),不会随窗口调整大小



我在摆弄窗口,到目前为止,当我点击(按钮向下)并拖动,然后放开(按钮向上)时,我确实会出现一个squere。。。当我调整窗口大小时,它怎么会消失?为什么?我错过了什么?谢谢你花时间复习这篇文章。

//LEFT BUTTON DOWN MOVEMENT
   case WM_LBUTTONDOWN:
      // if not drwing then save start poostion and set drawing flag
      // goes here startPosition
      if(! isDrawing )
      {
         startPosition = MAKEPOINTS( lParam );// poiint where the mouseis clicked
         isDrawing = true;
      }
      else{
         // how did I get a button down while I was drawing
      }
      break;
//when the button is let up the message is sent, its a drawing mode or not drawing, or draging draw.....
// LEFT BUTTON UP movement
   case WM_LBUTTONUP:
      // if drawing is not occuring, then 
      // if I am drawing then save endeding point(position), draw a rectangle (startPositon and endPoistion)
      //
      if(isDrawing )
      {
         endPosition = currentPosition = MAKEPOINTS( lParam );
         //draw rectangle here.
        // This is what we had 1st....  RectType rect( startPosition.x, startPosition.y, endPosition.x, endPosition.y);
         RectType rec( startPosition , endPosition );// this is what we want...need to write teh code for this.
         //something and I cant call beginPaint(),and end paint();
         if( hdc = GetDC(hWnd) ) 
         {
            ::Rectangle(hdc, rec.myRect.left, rec.myRect.top, rec.myRect.right, rec.myRect.bottom);
            ReleaseDC( hWnd, hdc );// this is how we call on paint with out being in WM_Paint
         }
         isDrawing = false;       // this is the flag.            
      }
      break;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    switch (message)
    {
//LEFT BUTTON DOWN MOVEMENT
   case WM_LBUTTONDOWN:
      // if not drwing then save start poostion and set drawing flag
      // goes here startPosition
      if(! isDrawing )
      {
         startPosition = MAKEPOINTS( lParam );// poiint where the mouseis clicked
         isDrawing = true;
      }
      else{
         // how did I get a button down while I was drawing
      }
      break;
//when the button is let up the message is sent, its a drawing mode or not drawing, or draging draw.....
// LEFT BUTTON UP movement
   case WM_LBUTTONUP:
      // if drawing is not occuring, then 
      // if I am drawing then save endeding point(position), draw a rectangle (startPositon and endPoistion)
      //
      if(isDrawing )
      {
         endPosition = currentPosition = MAKEPOINTS( lParam );
         //draw rectangle here.
        // This is what we had 1st....  RectType rect( startPosition.x, startPosition.y, endPosition.x, endPosition.y);
         RectType rec( startPosition , endPosition );// this is what we want...need to write teh code for this.
         //something and I cant call beginPaint(),and end paint();
         if( hdc = GetDC(hWnd) ) 
         {
            ::Rectangle(hdc, rec.myRect.left, rec.myRect.top, rec.myRect.right, rec.myRect.bottom);
            ReleaseDC( hWnd, hdc );// this is how we call on paint with out being in WM_Paint
         }
         isDrawing = false;       // this is the flag.            
      }
      break;
   case WM_MOUSEMOVE:
      //if I am drawing...
      //if I am not drawing save current postion, and latested or endingPositon
      // darw rectangle( StartPos, latestPos)
      if( isDrawing )//
      {
         currentPosition = MAKEPOINTS( lParam );
         RectType rec( startPosition , currentPosition );// this is what we want. 
         //draw rectangle here.
         if( hdc = GetDC(hWnd) ) // GetDC is the window function....
         {
            POINTS prevPosition = currentPosition;
            currentPosition = MAKEPOINTS(lParam);
            RectType rec( startPosition , currentPosition );
            RectType prevRec( startPosition, prevPosition);
            HDC dc   = GetDC( hWnd );
            HPEN pen = CreatePen( PS_DASH, 0, RGB(0, 0, 0 ));
                HBRUSH brush = CreateSolidBrush( RGB(rand()%255, rand()%255, rand()%255)  );//---------------------------------------------NEWWWWWWWWWWWWRGB(200,0,0)
            SelectObject(hdc,pen);
//give us a quaiz hollow square.below.  
            SelectObject(hdc,GetStockObject( HOLLOW_BRUSH )  );
            //::SetROP2( hdc, R2_XORPEN );//these use exclusive OR bit wise logical operator...try R2_NOT,for later
// 1^0 = 1...so if the comparing two are differnet 1 and 0 we have 1
// 1^1 = 0...if we have all the same 0,0 then it would be 1....
            ::Rectangle(hdc, prevRec.myRect.left, prevRec.myRect.top , prevRec.myRect.right, prevRec.myRect.bottom );
            ::Rectangle(hdc, rec.myRect.left, rec.myRect.top , rec.myRect.right, rec.myRect.bottom );
            ReleaseDC( hWnd, hdc );// this is how we call on paint with out being in WM_Paint
         }  
      }
      break;

您需要处理WM_PAINT并在那里进行绘图,而不是在WM_MOUSEMOVE中。在WM_MOUSEMOVE(或其他地方)中,移动矩形后调用::Invalidate。这将导致Windows发送WM_PAINT消息。

最新更新