Go deep into the SetOP2 Function

Source: Internet
Author: User

SetROP2 function: sets the foreground painting mode. The currently drawn pixel value is the inverse of the current screen pixel value, so that the last painting result can be erased:

Simulate the BLOCKOUT program in chapter 7 of windows programming:

Code:

 

[C-sharp]View plaincopyprint?

  1. # Include <windows. h>
  2. Lresult callback wndproc (HWND, UINT, WPARAM, LPARAM );
  3. Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  4. {
  5. Static TCHAR szAppName [] = TEXT ("BlokOut1 ");
  6. HWND hwnd;
  7. MSG msg;
  8. WNDCLASS wndclass;
  9. Wndclass. style = CS_HREDRAW | CS_VREDRAW;
  10. Wndclass. lpfnWndProc = wndproc;
  11. Wndclass. cbClsExtra = 0;
  12. Wndclass. cbWndExtra = 0;
  13. Wndclass. hInstance = hInstance;
  14. Wndclass. hIcon = LoadIcon (NULL, IDI_APPLICATION );
  15. Wndclass. hCursor = LoadCursor (NULL, IDC_ARROW );
  16. Wndclass. hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH );
  17. Wndclass. lpszMenuName = NULL;
  18. Wndclass. lpszClassName = szAppName;
  19. If (! RegisterClass (& wndclass ))
  20. {
  21. MessageBox (NULL, TEXT ("Program requires Windows NT! "),
  22. SzAppName, MB_ICONERROR );
  23. Return 0;
  24. }
  25. Hwnd = CreateWindow (szAppName, TEXT ("Mouse Button Demo "),
  26. WS_OVERLAPPEDWINDOW,
  27. CW_USEDEFAULT, CW_USEDEFAULT,
  28. CW_USEDEFAULT, CW_USEDEFAULT,
  29. NULL, NULL, hInstance, NULL );
  30. ShowWindow (hwnd, SW_SHOWNORMAL );
  31. UpdateWindow (hwnd );
  32. While (GetMessage (& msg, NULL, 0, 0 ))
  33. {
  34. TranslateMessage (& msg );
  35. DispatchMessage (& msg );
  36. }
  37. Return msg. wParam;
  38. }
  39. Void Draw (HWND hwnd, POINT ptbeg, POINT ptend)
  40. {
  41. HDC hdc;
  42. Hdc = GetDC (hwnd );
  43. SetROP2 (hdc, R2_NOT); // you can comment out this line and check the result.
  44. SelectObject (hdc, GetStockObject (NULL_BRUSH ));
  45. Rectangle (hdc, ptbeg. x, ptbeg. y, ptend. x, ptend. y );
  46. ReleaseDC (hwnd, hdc );
  47. }
  48. Lresult callback wndproc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
  49. {
  50. Static POINT ptbeg, ptend, Boxbeg, Boxend;
  51. Static BOOL flag1, flag2;
  52. HDC hdc;
  53. PAINTSTRUCT ps;
  54. Switch (message)
  55. {
  56. Case WM_LBUTTONDOWN:
  57. Ptbeg. x = ptend. x = LOWORD (lparam );
  58. Ptbeg. y = ptend. y = HIWORD (lparam );
  59. Draw (hwnd, ptbeg, ptend );
  60. Flag1 = TRUE;
  61. SetCursor (LoadCursor (NULL, IDC_CROSS ));
  62. Return 0;
  63. Case WM_MOUSEMOVE:
  64. If (flag1)
  65. {
  66. SetCursor (LoadCursor (NULL, IDC_CROSS ));
  67. Draw (hwnd, ptbeg, ptend );
  68. Ptend. x = LOWORD (lparam );
  69. Ptend. y = HIWORD (lparam );
  70. Draw (hwnd, ptbeg, ptend );
  71. }
  72. Return 0;
  73. Case WM_LBUTTONUP:
  74. If (flag1)
  75. {
  76. SetCursor (LoadCursor (NULL, IDC_ARROW ));
  77. Draw (hwnd, ptbeg, ptend );
  78. Boxbeg = ptbeg;
  79. Boxend. x = LOWORD (lparam );
  80. Boxend. y = HIWORD (lparam );
  81. Flag1 = FALSE;
  82. Flag2 = TRUE;
  83. InvalidateRect (hwnd, NULL, TRUE );
  84. }
  85. Return 0;
  86. Case WM_PAINT:
  87. If (flag2)
  88. {Hdc = BeginPaint (hwnd, & ps );
  89. SelectObject (hdc, GetStockObject (BLACK_BRUSH ));
  90. // Draw (hwnd, Boxbeg, Boxend );
  91. Rectangle (hdc, Boxbeg. x, Boxbeg. y, Boxend. x, Boxend. y );
  92. EndPaint (hwnd, & ps );
  93. }
  94. Return 0;
  95. Case WM_DESTROY:
  96. PostQuitMessage (0 );
  97. Return 0;
  98. }
  99. Return DefWindowProc (hwnd, message, wparam, lparam );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.