子窗口控件(2)之消息处理
通知码进一步给出每条消息的意思,下面是按钮的通知码的可能值。
按钮通知码标识符值 BN_CLICKED0BN_PAINT1BN_HILITE或BN_PUSHED2BN_DISABLE3BN_DISABLE4BN_DOUBLECLICKED或BN_DBLCLK5
BN_SETFOCUS
6BN_KILLFOCUS7每个子窗口有一个句柄和唯一的ID,知道其中一个就可以找到另一个,如果知道了子窗口句柄,可以通过一下函数获取ID
id=GetWindowLong(hwndChlid,GWL_ID);
也可以使用另一个函数
id=GetDlgCtrlID(hwndChild);
知道子窗口ID和父窗口句柄,可以得到子窗口句柄
hwndChild=GetDlgItem(hwndParend,id);
通过给Windows发送一个BM_GETSTATE消息,可以模拟按键按钮的状态变化,下面的语句可以让按钮看上去像按下去一样
SendMessage(hwndButton,BM_SETSTATE,1,0);//按下去的效果SendMessage(hwndButton,BM_SETSTATE,0,0);//正常状况
复选框:
在处理来自控件的WM_COMMAND消息时,可以使用下面的代码切换选中标记
SendMessage((HWND)lParam,MB_SETCHECK,(WPARAM)!SendMessage((HWND)lParam,BM_GETCHECK,0,0),0);iCheck=(int)SendMessage(hwndButton,BM_GETCHECK,0,0);//选中返回非0
单选按钮:
SendMessage(hwndButton,BM_SETCHECK,1,0);//选中SendMessage(hwndButton,BM_SETCHECK,0,0);//未选中
改变按钮文本:
SetWindowText(hwnd,pszString);iLength=GetWindowText(hwnd,pszBuffer,iMaxLength);iLength=GetWindowLength(hwnd);
可见的按钮和启动的按钮
如果在创建窗口类是没有包括WS_VISIBLE,子窗口将不会显示,除非使用了下面的语句:
ShowWindow(hwndChild,SW_SHOWNORMAL);IsWindowVisible(hwndChild);//判断是否可见
可以使用下面语句禁止和重启按钮
EnableWindow(hwndChild,FALSE);//按钮不可用,按钮变成灰色Enablewindow(hwndChild,TRUE);//按钮可用
下面来看看一段完整的程序:
#include<windows.h>#include<windowsx.h>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 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, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter){static HWND m_hEditWnd;static HWND m_hButton;static int nButtonID=1;switch(uMsg){case WM_CREATE:m_hEditWnd=CreateWindow(TEXT("EDIT"),TEXT(""),WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_VSCROLL|WS_BORDER,0,0,0,0,hwnd,NULL,hInst,NULL);m_hButton=CreateWindow(TEXT("BUTTON"),TEXT("按钮"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,0,0,0,0,hwnd,(HMENU)nButtonID,hInst,NULL);SetWindowText(m_hEditWnd,TEXT("win32 application programing"));break;case WM_SIZE:MoveWindow(m_hEditWnd,0,0,200,100,TRUE);MoveWindow(m_hButton,140,110,60,20,TRUE);break;case WM_DESTROY:PostQuitMessage(0);return 0;case WM_KEYDOWN:switch(wParam){case VK_ESCAPE:PostQuitMessage(0);break;}break;case WM_COMMAND:if(wParam==nButtonID){TCHAR szBuffer[100];GetWindowText(m_hEditWnd,szBuffer,98);MessageBox(hwnd,szBuffer,TEXT("Edit Content"),MB_OK);}}return DefWindowProc(hwnd,uMsg,wParam,lParam);}结果如下:
