下面这个程序,在单击每个按钮时,该按钮会向其父窗口发送WM_COMMAND消息,这个窗口过程就是我们所熟悉的WindowProc,函数将在客户区的右侧显示这个消息的wParam和lParam参数
创建子窗口的参数如下
类名 | TEXT("button") |
窗口文本 | button[i].szText |
窗口样式 | WS_CHILD|WS_VISIBLE|button[i].iStyle |
X坐标 | cxChar |
Y坐标 | cyChar*(1+2*i) |
宽度 | 20*xChar |
高度 | 7*yChar/4 |
父窗口 | hwnd |
子窗口ID | (HMUNE)i |
案例句柄 | ((LPCREATESTRUCT)lParam)->hInstance |
额外参数 | NULL |
其中案例句柄可以声明一个全局变量(HINSTANCE hInst;),然后在WinMain赋值 hInst=hInstance;
列表框,按钮控件,编辑控件相应的wParam,lParam变量的含义是一样的
LOWORD(wParam) | 子窗口ID |
HIWORD(wParam) | 通知码 |
lParam | 子窗口 |
在后面的博客中,会多次出现
cxClient=LOWORD(lParam); switch (LOWORD (wParam))//低位wParam获取通知码 { case SB_PAGEDOWN : ......
case WM_VSCROLL : i = GetWindowLong ((HWND) lParam, GWL_ID) ; switch (LOWORD (wParam))//低位wParam获取通知码 { case SB_PAGEDOWN : color[i] += 15 ;
switch (message)//确定焦点,实现用tab键来转换焦点 { case WM_KEYDOWN : if (wParam == VK_TAB) SetFocus (GetDlgItem (GetParent (hwnd),
诸如此类的东西,大家要弄明白wParam,lParam参数在不同的环境,所代表的意义。
看完整程序:
#include<windows.h> struct{ int iStyle; TCHAR * szText; } button[]={ BS_PUSHBUTTON ,TEXT("PUSHBUTTON"), BS_DEFPUSHBUTTON,TEXT("DEFPUSHBUTTON"), BS_CHECKBOX,TEXT("CHECKBOX"), BS_AUTOCHECKBOX,TEXT("AUTOCHECKBOX"), BS_RADIOBUTTON,TEXT("RADIOBUTTON"), BS_3STATE,TEXT("3STATE"), BS_AUTO3STATE,TEXT("AUTO3STATE"), BS_GROUPBOX,TEXT("GROUPBOX"), BS_OWNERDRAW,TEXT("OWNERDRAW") }; #define NUM (sizeof(button)/sizeof(button[0])) LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); 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; 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 require 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; } LRESULT CALLBACK WindowProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static HWND hwndButton[NUM] ; static RECT rect ; static TCHAR szTop[] = TEXT ("message wParam lParam"), szUnd[] = TEXT ("_______ ______ ______"), szFormat[] = TEXT ("%-16s%04X-%04X %04X-%04X"), szBuffer[50] ; static int cxChar, cyChar ; HDC hdc ; PAINTSTRUCT ps ; int i ; switch (message) { case WM_CREATE : cxChar = LOWORD (GetDialogBaseUnits ()) ; cyChar = HIWORD (GetDialogBaseUnits ()) ; for (i = 0 ; i < NUM ; i++) hwndButton[i] = CreateWindow ( TEXT("button"), button[i].szText, WS_CHILD | WS_VISIBLE | button[i].iStyle, cxChar, cyChar * (1 + 2 * i), 20 * cxChar, 7 * cyChar / 4, hwnd, (HMENU) i, ((LPCREATESTRUCT) lParam)->hInstance, NULL) ; return 0 ; case WM_SIZE : rect.left = 24 * cxChar ; rect.top = 2 * cyChar ; rect.right = LOWORD (lParam) ; rect.bottom = HIWORD (lParam) ; return 0 ; case WM_PAINT : InvalidateRect (hwnd, &rect, TRUE) ; hdc = BeginPaint (hwnd, &ps) ; SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ; SetBkMode (hdc, TRANSPARENT) ; TextOut (hdc, 24 * cxChar, cyChar, szTop, lstrlen (szTop)) ; TextOut (hdc, 24 * cxChar, cyChar, szUnd, lstrlen (szUnd)) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DRAWITEM : case WM_COMMAND : ScrollWindow (hwnd, 0, -cyChar, &rect, &rect) ; hdc = GetDC (hwnd) ; SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ; TextOut (hdc, 24 * cxChar, cyChar * (rect.bottom / cyChar - 1), szBuffer, wsprintf (szBuffer, szFormat, message == WM_DRAWITEM ? TEXT ("WM_DRAWITEM") : TEXT ("WM_COMMAND"), HIWORD (wParam), LOWORD (wParam), HIWORD (lParam), LOWORD (lParam))) ; ReleaseDC (hwnd, hdc) ; ValidateRect (hwnd, &rect) ; break ; case WM_DESTROY : PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }
看看效果图: