现在的位置: 首页 > 综合 > 正文

深入SetOP2函数

2013年05月12日 ⁄ 综合 ⁄ 共 3375字 ⁄ 字号 评论关闭

SetROP2函数:设置前景绘制模式,当前绘制的像素值是当前屏幕像素值的反,这样可以擦出上一次绘制的结果:

模仿 《windows程序设计》第七章BLOCKOUT程序:

code:

 

  1. #include <windows.h>  
  2.   
  3. LRESULT CALLBACK wndproc(HWND,UINT,WPARAM,LPARAM);  
  4.   
  5. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )  
  6. {  
  7.     static TCHAR szAppName[] = TEXT ("BlokOut1") ;  
  8.     HWND         hwnd ;  
  9.     MSG          msg ;  
  10.     WNDCLASS     wndclass ;  
  11.       
  12.     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;  
  13.     wndclass.lpfnWndProc   = wndproc ;  
  14.     wndclass.cbClsExtra    = 0 ;  
  15.     wndclass.cbWndExtra    = 0 ;  
  16.     wndclass.hInstance     = hInstance ;  
  17.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;  
  18.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;  
  19.     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;  
  20.     wndclass.lpszMenuName  = NULL ;  
  21.     wndclass.lpszClassName = szAppName ;  
  22.       
  23.     if (!RegisterClass (&wndclass))  
  24.     {  
  25.         MessageBox (NULL, TEXT ("Program requires Windows NT!"),   
  26.             szAppName, MB_ICONERROR) ;  
  27.         return 0 ;  
  28.     }  
  29.       
  30.     hwnd = CreateWindow (szAppName, TEXT ("Mouse Button Demo"),  
  31.         WS_OVERLAPPEDWINDOW,  
  32.         CW_USEDEFAULT, CW_USEDEFAULT,  
  33.         CW_USEDEFAULT, CW_USEDEFAULT,  
  34.         NULL, NULL, hInstance, NULL) ;  
  35.       
  36.     ShowWindow (hwnd, SW_SHOWNORMAL) ;  
  37.     UpdateWindow (hwnd) ;  
  38.       
  39.     while (GetMessage (&msg, NULL, 0, 0))  
  40.     {  
  41.         TranslateMessage (&msg) ;  
  42.         DispatchMessage (&msg) ;  
  43.     }  
  44.      return msg.wParam ;  
  45. }  
  46.   
  47. void Draw(HWND hwnd,POINT ptbeg,POINT ptend)  
  48. {  
  49.     HDC hdc;  
  50.     hdc=GetDC(hwnd);  
  51.     SetROP2(hdc,R2_NOT);//可以把这行注释掉,看看结果  
  52.     SelectObject(hdc,GetStockObject(NULL_BRUSH));  
  53.     Rectangle(hdc,ptbeg.x,ptbeg.y,ptend.x,ptend.y);  
  54.     ReleaseDC(hwnd,hdc);  
  55. }  
  56.   
  57. LRESULT CALLBACK wndproc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)  
  58. {  
  59.     static POINT ptbeg,ptend,Boxbeg,Boxend;  
  60.     static BOOL  flag1,flag2;  
  61.     HDC          hdc;  
  62.     PAINTSTRUCT ps;  
  63.     switch(message)  
  64.     {  
  65.     case WM_LBUTTONDOWN:  
  66.         ptbeg.x=ptend.x=LOWORD(lparam);  
  67.         ptbeg.y=ptend.y=HIWORD(lparam);  
  68.   
  69.         Draw(hwnd,ptbeg,ptend);  
  70.         flag1=TRUE;  
  71.         SetCursor(LoadCursor(NULL,IDC_CROSS));  
  72.   
  73.         return 0;  
  74.     case WM_MOUSEMOVE:  
  75.         if (flag1)  
  76.         {  
  77.             SetCursor(LoadCursor(NULL,IDC_CROSS));  
  78.             Draw(hwnd,ptbeg,ptend);  
  79.             ptend.x=LOWORD(lparam);  
  80.             ptend.y=HIWORD(lparam);  
  81.             Draw(hwnd,ptbeg,ptend);  
  82.         }  
  83.         return 0;  
  84.     case WM_LBUTTONUP:  
  85.         if (flag1)  
  86.         {  
  87.             SetCursor(LoadCursor(NULL,IDC_ARROW));  
  88.             Draw(hwnd,ptbeg,ptend);  
  89.             Boxbeg=ptbeg;  
  90.             Boxend.x=LOWORD(lparam);  
  91.             Boxend.y=HIWORD(lparam);  
  92.             flag1=FALSE;  
  93.             flag2=TRUE;  
  94.             InvalidateRect(hwnd,NULL,TRUE);  
  95.         }  
  96.         return 0;  
  97.     case WM_PAINT:  
  98.         if (flag2)  
  99.         {   hdc=BeginPaint(hwnd,&ps);  
  100.             SelectObject(hdc,GetStockObject(BLACK_BRUSH));  
  101.             //Draw(hwnd,Boxbeg,Boxend);  
  102.             Rectangle(hdc,Boxbeg.x,Boxbeg.y,Boxend.x,Boxend.y);  
  103.             EndPaint(hwnd,&ps);  
  104.         }  
  105.         return 0;  
  106.     case WM_DESTROY:  
  107.         PostQuitMessage(0);  
  108.         return 0;  
  109.     }  
  110.     return DefWindowProc(hwnd,message,wparam,lparam);  

 }

抱歉!评论已关闭.