MOOC清華《VC++面向對象與可視化程序設計》第4章:鼠標操作例程(灰白選框)

#include <windows.h>

LRESULT CALLBACK  WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpszCmdParam, int nCmdShow)
     {
     static char szAppName[] = "4_4" ;
     HWND        hwnd ;
     MSG         msg ;
     WNDCLASS    wndclass ;

     if (!hPrevInstance) 
          {
          wndclass.style         = CS_HREDRAW | CS_VREDRAW |CS_DBLCLKS;
          wndclass.lpfnWndProc   = WndProc ;
          wndclass.cbClsExtra    = 0 ;
          wndclass.cbWndExtra    = 0 ;
          wndclass.hInstance     = hInstance ;
          wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
          wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
          wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
          wndclass.lpszMenuName  = NULL ;
          wndclass.lpszClassName = szAppName ;
		  RegisterClass (&wndclass) ;
          }

     hwnd = CreateWindow (szAppName, "Blow-Up Mouse Demo",
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT, CW_USEDEFAULT,
                         CW_USEDEFAULT, CW_USEDEFAULT,
                         NULL, NULL, hInstance, NULL) ;

     ShowWindow (hwnd, nCmdShow) ;
     UpdateWindow (hwnd) ;
	 while (GetMessage (&msg, NULL, 0, 0))
          {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
          }
     return msg.wParam ;
     }

LRESULT CALLBACK  WndProc(HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
     {
     static BOOL  operate=FALSE,ready=TRUE;
	 static POINT BeginP,EndP;
	 static RECT rect={0,0,0,0} ;
     HDC          hdc ;
     PAINTSTRUCT  PtStr;
	 HBRUSH       hBrush;
     switch (message)
          {
          case WM_LBUTTONDOWN:
               if ((!operate)&&ready)
                    {
                    operate=TRUE ;//左鍵擊活俘獲
					ready=FALSE;
                    SetCapture (hwnd) ;//把所有的鼠標信息輸入到被左鍵擊活的窗口.
                    SetCursor (LoadCursor (NULL, IDC_CROSS)) ;//載入光標
					BeginP.x=LOWORD(lParam);
					BeginP.y=HIWORD(lParam);
                    }
               return 0 ;

          case WM_MOUSEMOVE:
               if (operate)
                    {
                    EndP.x=LOWORD(lParam);
					EndP.y=HIWORD(lParam);
                    rect.left=BeginP.x<EndP.x?BeginP.x:EndP.x;
					rect.right=BeginP.x>EndP.x?BeginP.x:EndP.x;
					rect.top=BeginP.y<EndP.y?BeginP.y:EndP.y;
					rect.bottom=BeginP.y>EndP.y?BeginP.y:EndP.y;
                    SetCursor (LoadCursor (NULL, IDC_WAIT)) ;//載入光標
					InvalidateRect(hwnd,NULL,TRUE);
                    }
               return 0 ;

          case WM_LBUTTONUP:
               if (operate)
                    {
                    operate=FALSE;
                    SetCursor (LoadCursor (NULL, IDC_WAIT));
                    GetClientRect (hwnd, &rect) ;
					InvalidateRect(hwnd,NULL,TRUE);
					SetCursor(LoadCursor(NULL,IDC_ARROW));
                    ReleaseCapture () ;//把鼠標從當前窗口中釋放出來,您應該不難找出前面與之配套的函數
                    }
               return 0 ;
          case WM_PAINT:
			   if(ready==FALSE)
			   {    hdc=BeginPaint(hwnd,&PtStr);
			        hBrush=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
					SelectObject(hdc,hBrush);
					Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
					DeleteObject(hBrush);
					EndPaint(hwnd,&PtStr);
			   }
			   else
			   {    hdc=BeginPaint(hwnd,&PtStr);
			        GetClientRect(hwnd,&rect);
					Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
					EndPaint(hwnd,&PtStr);
			   }
			   return 0;
		  case WM_LBUTTONDBLCLK:
			   if(ready==FALSE)
			   {     ready=TRUE;
			         InvalidateRect(hwnd,NULL,TRUE);
			   }
			   return 0;
          case WM_DESTROY:
               PostQuitMessage (0) ;
               return 0 ; 
          }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
     }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章