1、OwnDraw) ; MSG msg ; HWND hwnd ; WNDCLASS wndclass ; hInst = hInstance ; 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.hCurs
2、or = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = szAppName ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass) MessageBox (NULL, TEXT (This program requires Windows NT!), szAppName, MB_ICONERROR) ; return 0
3、 ; hwnd = CreateWindow (szAppName, TEXT (Owner-Draw Button Demo WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0) TranslateMessage (&msg) ; DispatchMessage (& return msg.wParam ;v
4、oid Triangle (HDC hdc, POINT pt) SelectObject (hdc, GetStockObject (BLACK_BRUSH) ; Polygon (hdc, pt, 3) ; SelectObject (hdc, GetStockObject (WHITE_BRUSH) ;LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) static HWND hwndSmaller, hwndLarger ; static int cxClient, cyCli
5、ent, cxChar, cyChar ; int cx, cy ; LPDRAWITEMSTRUCT pdis ; POINT pt3 ; RECT rc ; switch (message) case WM_CREATE : cxChar = LOWORD (GetDialogBaseUnits () ; cyChar = HIWORD (GetDialogBaseUnits () ; / Create the owner-draw pushbuttons hwndSmaller = CreateWindow (TEXT (button), TEXT ( WS_CHILD | WS_VIS
6、IBLE | BS_OWNERDRAW, 0, 0, BTN_WIDTH, BTN_HEIGHT, hwnd, (HMENU) ID_SMALLER, hInst, NULL) ; hwndLarger = CreateWindow (TEXT ( hwnd, (HMENU) ID_LARGER, hInst, NULL) ; case WM_SIZE : cxClient = LOWORD (lParam) ; cyClient = HIWORD (lParam) ; / Move the buttons to the new center MoveWindow (hwndSmaller,
7、cxClient / 2 - 3 * BTN_WIDTH / 2, cyClient / 2 - BTN_HEIGHT / 2, BTN_WIDTH, BTN_HEIGHT, TRUE) ; MoveWindow (hwndLarger, cxClient / 2 + BTN_WIDTH / 2, case WM_COMMAND : GetWindowRect (hwnd, &rc) ; / Make the window 10% smaller or larger switch (wParam) case ID_SMALLER : rc.left += cxClient / 20 ; rc.
8、right -= cxClient / 20 ; rc.top += cyClient / 20 ; rc.bottom -= cyClient / 20 ; break ; case ID_LARGER : rc.left -= cxClient / 20 ; rc.right += cxClient / 20 ; rc.top -= cyClient / 20 ; rc.bottom += cyClient / 20 ; MoveWindow (hwnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE) ; ca
9、se WM_DRAWITEM : pdis = (LPDRAWITEMSTRUCT) lParam ; / Fill area with white and frame it black FillRect (pdis-hDC, &pdis-rcItem, (HBRUSH) GetStockObject (WHITE_BRUSH) ; FrameRect (pdis- (HBRUSH) GetStockObject (BLACK_BRUSH) ; / Draw inward and outward black triangles cx = pdis-rcItem.right - pdis-rcI
10、tem.left ; cy = pdis-rcItem.bottom - pdis-rcItem.top ; switch (pdis-CtlID) pt0.x = 3 * cx / 8 ; pt0.y = 1 * cy / 8 ; pt1.x = 5 * cx / 8 ; pt1.y = 1 * cy / 8 ; pt2.x = 4 * cx / 8 ; pt2.y = 3 * cy / 8 ; Triangle (pdis-hDC, pt) ; pt0.x = 7 * cx / 8 ; pt0.y = 3 * cy / 8 ; pt1.x = 7 * cx / 8 ; pt1.y = 5
11、* cy / 8 ; pt2.x = 5 * cx / 8 ; pt2.y = 4 * cy / 8 ; pt0.x = 5 * cx / 8 ; pt0.y = 7 * cy / 8 ; pt1.x = 3 * cx / 8 ; pt1.y = 7 * cy / 8 ; pt2.y = 5 * cy / 8 ; pt0.x = 1 * cx / 8 ; pt0.y = 5 * cy / 8 ; pt1.x = 1 * cx / 8 ; pt1.y = 3 * cy / 8 ; pt2.x = 3 * cx / 8 ; pt2.y = 1 * cy / 8 ; pt2.x = 7 * cx /
12、 8 ; pt2.y = 7 * cy / 8 ; pt2.x = 1 * cx / 8 ; / Invert the rectangle if the button is selected if (pdis-itemState & ODS_SELECTED) InvertRect (pdis-rcItem) ; / Draw a focus rectangle if the button has the focus ODS_FOCUS) pdis-rcItem.left += cx / 16 ;rcItem.top += cy / 16 ;rcItem.right -= cx / 16 ;rcItem.bottom -= cy / 16 ; DrawFocusRect (pdis- case WM_DESTROY : PostQuitMessage (0) ; return DefWindowProc (hwnd, message, wParam, lParam) ;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1