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?
- # Include <windows. h>
- Lresult callback wndproc (HWND, UINT, WPARAM, LPARAM );
- Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
- {
- Static TCHAR szAppName [] = TEXT ("BlokOut1 ");
- HWND hwnd;
- MSG msg;
- WNDCLASS wndclass;
- Wndclass. style = CS_HREDRAW | CS_VREDRAW;
- 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;
- If (! RegisterClass (& wndclass ))
- {
- MessageBox (NULL, TEXT ("Program requires Windows NT! "),
- SzAppName, MB_ICONERROR );
- Return 0;
- }
- Hwnd = CreateWindow (szAppName, TEXT ("Mouse Button Demo "),
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT,
- CW_USEDEFAULT, CW_USEDEFAULT,
- NULL, NULL, hInstance, NULL );
- ShowWindow (hwnd, SW_SHOWNORMAL );
- UpdateWindow (hwnd );
- While (GetMessage (& msg, NULL, 0, 0 ))
- {
- TranslateMessage (& msg );
- DispatchMessage (& msg );
- }
- Return msg. wParam;
- }
- Void Draw (HWND hwnd, POINT ptbeg, POINT ptend)
- {
- HDC hdc;
- Hdc = GetDC (hwnd );
- SetROP2 (hdc, R2_NOT); // you can comment out this line and check the result.
- SelectObject (hdc, GetStockObject (NULL_BRUSH ));
- Rectangle (hdc, ptbeg. x, ptbeg. y, ptend. x, ptend. y );
- ReleaseDC (hwnd, hdc );
- }
- Lresult callback wndproc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
- {
- Static POINT ptbeg, ptend, Boxbeg, Boxend;
- Static BOOL flag1, flag2;
- HDC hdc;
- PAINTSTRUCT ps;
- Switch (message)
- {
- Case WM_LBUTTONDOWN:
- Ptbeg. x = ptend. x = LOWORD (lparam );
- Ptbeg. y = ptend. y = HIWORD (lparam );
- Draw (hwnd, ptbeg, ptend );
- Flag1 = TRUE;
- SetCursor (LoadCursor (NULL, IDC_CROSS ));
- Return 0;
- Case WM_MOUSEMOVE:
- If (flag1)
- {
- SetCursor (LoadCursor (NULL, IDC_CROSS ));
- Draw (hwnd, ptbeg, ptend );
- Ptend. x = LOWORD (lparam );
- Ptend. y = HIWORD (lparam );
- Draw (hwnd, ptbeg, ptend );
- }
- Return 0;
- Case WM_LBUTTONUP:
- If (flag1)
- {
- SetCursor (LoadCursor (NULL, IDC_ARROW ));
- Draw (hwnd, ptbeg, ptend );
- Boxbeg = ptbeg;
- Boxend. x = LOWORD (lparam );
- Boxend. y = HIWORD (lparam );
- Flag1 = FALSE;
- Flag2 = TRUE;
- InvalidateRect (hwnd, NULL, TRUE );
- }
- Return 0;
- Case WM_PAINT:
- If (flag2)
- {Hdc = BeginPaint (hwnd, & ps );
- SelectObject (hdc, GetStockObject (BLACK_BRUSH ));
- // Draw (hwnd, Boxbeg, Boxend );
- Rectangle (hdc, Boxbeg. x, Boxbeg. y, Boxend. x, Boxend. y );
- EndPaint (hwnd, & ps );
- }
- Return 0;
- Case WM_DESTROY:
- PostQuitMessage (0 );
- Return 0;
- }
- Return DefWindowProc (hwnd, message, wparam, lparam );
}