子視窗控制項(3)之屬性羅列和自繪按鈕

來源:互聯網
上載者:User

Windows有29種系統色彩,每種顏色都在0-255之間,它使用GetSysColor和SetSysColors來調用顏色參數:

也就是Windows下的畫板的顏色:

羅列一下具體的29種顏色的參數:

 

如下調用:

SetBkColor(hdc,GetSysColor(COLOR_BTNFACE));SetTextColor(hdc,GetSysColor(Color_WINDOWTEXT));//更改了系統色彩,要用下面的語句傳遞訊息case WM_SYSCOLORCHANGE:    InvalidateRect(hwnd,NULL,TRUE);    break;

一般使用擷取WM_CTLCOLORSTATIC:訊息去改變顏色:

下面看一段自繪按鈕的代碼:

#include<windows.h>#include<windowsx.h>#define ID_SMALLER 1#define ID_LARGER 2#define BTN_WIDTH (8*cxChar)#define BTN_HEIGHT  (4*cyChar)LRESULT CALLBACK WindowProc(  HWND hwnd,      // handle to window  UINT uMsg,      // message identifier  WPARAM wParam,  // first message parameter  LPARAM lParam   // second message parameter);HINSTANCE hInst;int WINAPI WinMain(  HINSTANCE hInstance,      // handle to current instance  HINSTANCE hPrevInstance,  // handle to previous instance  LPSTR lpCmdLine,          // command line  int nCmdShow              // show state){static TCHAR szAppName[]=TEXT("leidemingzi");HWND hwnd;MSG msg;WNDCLASS wndclass;hInst=hInstance;wndclass.cbClsExtra=0;wndclass.cbWndExtra=0;wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);wndclass.hInstance=hInstance;wndclass.lpfnWndProc=WindowProc;wndclass.lpszClassName=szAppName;wndclass.lpszMenuName=NULL;wndclass.style=CS_HREDRAW|CS_VREDRAW;if(!RegisterClass(&wndclass)){MessageBox(NULL,TEXT("the program requrie the window nt"),TEXT("tips"),MB_ICONERROR);return 0;}hwnd=CreateWindow(  szAppName,  // registered class name  TEXT("this is title"), // window name  WS_OVERLAPPEDWINDOW,        // window style  CW_USEDEFAULT,                // horizontal position of window  CW_USEDEFAULT,                // vertical position of window  CW_USEDEFAULT,           // window width CW_USEDEFAULT,          // window height  NULL,      // handle to parent or owner window  NULL,          // menu handle or child identifier  hInstance,  // handle to application instance  NULL        // window-creation data);ShowWindow(hwnd,nCmdShow);UpdateWindow(hwnd);while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;}void Triangle(HDC hdc,POINT pt[]){SelectObject(hdc,GetStockObject(BLACK_BRUSH));Polygon(hdc,pt,3);SelectObject(hdc,GetStockObject(WHITE_BRUSH));}LRESULT CALLBACK WindowProc(  HWND hwnd,      // handle to window  UINT uMsg,      // message identifier  WPARAM wParam,  // first message parameter  LPARAM lParam   // second message parameter){static HWND hwndSmaller,hwndLarger;static int cxClient,cyClient,cxChar,cyChar;LPDRAWITEMSTRUCT pdis;POINT pt[3];int cx,cy;RECT rect;switch(uMsg){case WM_CREATE:cxChar=LOWORD(GetDialogBaseUnits());cyChar=HIWORD(GetDialogBaseUnits());hwndSmaller=CreateWindow(TEXT("button"),TEXT(""),WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,0,0,BTN_WIDTH,BTN_HEIGHT,hwnd,(HMENU)ID_SMALLER,hInst,NULL);hwndLarger=CreateWindow(TEXT("button"),TEXT(""),WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,0,0,BTN_WIDTH,BTN_HEIGHT,hwnd,(HMENU)ID_LARGER,hInst,NULL);return 0;case WM_SIZE:cxClient=GET_X_LPARAM(lParam);cyClient=GET_Y_LPARAM(lParam);MoveWindow(hwndSmaller,cxClient/2-3*BTN_WIDTH/2,cyClient/2-BTN_HEIGHT/2,BTN_WIDTH,BTN_HEIGHT,TRUE);MoveWindow(hwndLarger,cxClient/2+BTN_WIDTH/2,cyClient/2-BTN_HEIGHT/2,BTN_WIDTH,BTN_HEIGHT,TRUE);return 0;case WM_COMMAND:GetWindowRect(hwnd,&rect);switch(wParam){case ID_SMALLER:rect.left+=cxClient/20;rect.right-=cxClient/20;rect.top+=cyClient/20;rect.right-=cyClient/20;break;case ID_LARGER:rect.left-=cxClient/20;rect.right+=cxClient/20;rect.top-=cyClient/20;rect.right+=cyClient/20;break;}MoveWindow(hwnd,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,TRUE);return 0;case WM_DRAWITEM:pdis=(LPDRAWITEMSTRUCT)lParam;FillRect(pdis->hDC,&pdis->rcItem,(HBRUSH)GetStockObject(WHITE_BRUSH));FrameRect(pdis->hDC,&pdis->rcItem,(HBRUSH)GetStockObject(BLACK_BRUSH));cx=pdis->rcItem.right-pdis->rcItem.left;cy=pdis->rcItem.bottom-pdis->rcItem.top;switch (pdis->CtlID)          {          case ID_SMALLER :               pt[0].x = 3 * cx / 8 ;  pt[0].y = 1 * cy / 8 ;               pt[1].x = 5 * cx / 8 ;  pt[1].y = 1 * cy / 8 ;               pt[2].x = 4 * cx / 8 ;  pt[2].y = 3 * cy / 8 ;                              Triangle (pdis->hDC, pt) ;                              pt[0].x = 7 * cx / 8 ;  pt[0].y = 3 * cy / 8 ;               pt[1].x = 7 * cx / 8 ;  pt[1].y = 5 * cy / 8 ;               pt[2].x = 5 * cx / 8 ;  pt[2].y = 4 * cy / 8 ;                              Triangle (pdis->hDC, pt) ;                              pt[0].x = 5 * cx / 8 ;  pt[0].y = 7 * cy / 8 ;               pt[1].x = 3 * cx / 8 ;  pt[1].y = 7 * cy / 8 ;               pt[2].x = 4 * cx / 8 ;  pt[2].y = 5 * cy / 8 ;                              Triangle (pdis->hDC, pt) ;                              pt[0].x = 1 * cx / 8 ;  pt[0].y = 5 * cy / 8 ;               pt[1].x = 1 * cx / 8 ;  pt[1].y = 3 * cy / 8 ;               pt[2].x = 3 * cx / 8 ;  pt[2].y = 4 * cy / 8 ;                              Triangle (pdis->hDC, pt) ;               break ;                         case ID_LARGER :               pt[0].x = 5 * cx / 8 ;  pt[0].y = 3 * cy / 8 ;               pt[1].x = 3 * cx / 8 ;  pt[1].y = 3 * cy / 8 ;               pt[2].x = 4 * cx / 8 ;  pt[2].y = 1 * cy / 8 ;                              Triangle (pdis->hDC, pt) ;                              pt[0].x = 5 * cx / 8 ;  pt[0].y = 5 * cy / 8 ;               pt[1].x = 5 * cx / 8 ;  pt[1].y = 3 * cy / 8 ;               pt[2].x = 7 * cx / 8 ;  pt[2].y = 4 * cy / 8 ;                              Triangle (pdis->hDC, pt) ;                              pt[0].x = 3 * cx / 8 ;  pt[0].y = 5 * cy / 8 ;               pt[1].x = 5 * cx / 8 ;  pt[1].y = 5 * cy / 8 ;               pt[2].x = 4 * cx / 8 ;  pt[2].y = 7 * cy / 8 ;                              Triangle (pdis->hDC, pt) ;                              pt[0].x = 3 * cx / 8 ;  pt[0].y = 3 * cy / 8 ;               pt[1].x = 3 * cx / 8 ;  pt[1].y = 5 * cy / 8 ;               pt[2].x = 1 * cx / 8 ;  pt[2].y = 4 * cy / 8 ;                              Triangle (pdis->hDC, pt) ;               break ;          }if(pdis->itemState*ODS_SELECTED)InvertRect(pdis->hDC,&pdis->rcItem);if(pdis->itemState&ODS_FOCUS){pdis->rcItem.left+=cx/16;pdis->rcItem.top+=cy/16;pdis->rcItem.right-=cx/16;pdis->rcItem.bottom-=cy/16;DrawFocusRect(pdis->hDC,&pdis->rcItem);}return 0;case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd,uMsg,wParam,lParam);}

其中,GetDialogBaseUnits()方法類似於TEXTMETRIC的tmAveCharWidth屬性

case WM_DRAWITEM在繪製OWNERDRAW控制項生效

再看一下DRAWITEMSTRUCT的定義,詳細請看MSDN

typedef struct tagDRAWITEMSTRUCT {  UINT      CtlType;   UINT      CtlID;   UINT      itemID;   UINT      itemAction;   UINT      itemState;   HWND      hwndItem;   HDC       hDC;   RECT      rcItem;   ULONG_PTR itemData; } DRAWITEMSTRUCT

如下:

選中的時候,按鈕顏色取反,左邊按鈕使表單變小,右邊按鈕使表單變大

忠告:

對於使用自繪按鈕的人,Windows會為你獲得裝置環境,並把它放在DRAWITEMSTRUCT結構的一個欄位中,必須原樣不動保持這個裝置的狀態,任何一個被選入該裝置環境的GDI對象必須設定回原有的不被選中的狀態,此外不能在按鈕的地區以外繪製任何圖形。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.