子窗口控件中
wParam和lParam变量的含义
LOWORD(wParam) | 子窗口ID |
HIWORD(wParam) | 通知码 |
lParam | 子窗口句柄 |
编辑控件的通知消息
EN_SETFOCUS | 编辑控件得到了输入焦点 |
EN_KILLFOCUS | 失去了输入焦点 |
EN_CHANGE | 编辑框的内容将变化 |
EN_UPDATE | 内容已经变化 |
EN_ERRSPACE | 编辑框没有空间了 |
EN_MAXTEXT | 没有控件完成插入了 |
EN_HSCROLL | 水平滚动条被单击了 |
EN_VSCROLL | 垂直滚动条被单击了 |
常用函数
SendMessage(hwndEdit,WM_CUT,0,0);//剪切 SendMessage(hwndEdit,WM_COPY,0,0);//复制 SendMessage(hwndEdit,WM_CLEAR,0,0);//删除 SendMessage(hwndEdit,WM_PASTE,0,0);//粘贴
获取编辑器的行数
iCount=SendMessage(hwndEdit,EM_GETLINECOUNT,0,0);//获取行数 iOffset=SendMessage(hwndEdit,EM_LINEINDEX,iLine,0);//获取位移量 iLength=SendMessage(hwndEdit,EM_LINELENGTH,iLine,0)//获取指定行的字符数量
#include <windows.h> #define ID_EDIT 1 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); TCHAR szAppName[] = TEXT ("PopPad1") ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { 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 ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName, szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static HWND hwndEdit ; switch (message) { case WM_CREATE : hwndEdit = CreateWindow (TEXT ("edit"), NULL, WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 0, 0, 0, 0, hwnd, (HMENU) ID_EDIT, ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ; return 0 ; case WM_SETFOCUS : SetFocus (hwndEdit) ; return 0 ; case WM_SIZE : MoveWindow (hwndEdit, 0, 0, LOWORD (lParam), HIWORD (lParam), TRUE) ; return 0 ; case WM_COMMAND : if (LOWORD (wParam) == ID_EDIT) if (HIWORD (wParam) == EN_ERRSPACE || HIWORD (wParam) == EN_MAXTEXT) MessageBox (hwnd, TEXT ("Edit control out of space."), szAppName, MB_OK | MB_ICONSTOP) ; return 0 ; case WM_DESTROY : PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }
效果图如下:
列边框通知码
通知码 | 值 |
LBN_ERRSPACE | -2 |
LBN_SELCHANGE | 1 |
LBN_DBLCLK | 2 |
LBN_SELCANCEL | 3 |
LBN_SETFOCUS | 4 |
LBN_KILLFOCUS | 5 |
常用方法:
iCount=SendMessage(hwndList,LB_GETCOUNT,0,0);//获取总的项目数 iIndex=SendMessage(hwndList,LB_GETCURSEL,0,0);//获取第几行 iLength=SendMessage(hwndList,LB_GETTEXTLEN,iIndex,0);//获取指定行有多少个字符
下面看一段完整的代码,虽然有点恶心;
#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 the windows 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 ) { HDC hdc; PAINTSTRUCT ps; static HWND hwndButton; static int i,ii; static int cxClient,cyClient; static int cxChar,cyChar; static HWND hwndList; static int count; static int iIndex,iLength; TCHAR szBuffer[20]; static TCHAR szBuffer1[]=TEXT("hello"); static TCHAR szBuffer2[]=TEXT("I love cplusplus"); static TCHAR szBuffer3[]=TEXT("welcome to my blog"); static TCHAR szBuffer4[]=TEXT("Let's begin to learn windows programing"); static TCHAR szBuffer5[]=TEXT("After learning win32,we learn MFC"); switch(uMsg) { case WM_CREATE: cxChar=LOWORD(GetDialogBaseUnits()); cyChar=HIWORD(GetDialogBaseUnits()); hwndButton=CreateWindow( TEXT("BUTTON"),TEXT("按钮"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 0,0,0,0,hwnd,(HMENU)i,hInst,NULL); hwndList=CreateWindow(TEXT("listbox"),TEXT("please select"),WS_CHILD|WS_VISIBLE|LBS_STANDARD, cxChar,cyChar*3,cxChar*26+GetSystemMetrics(SM_CXVSCROLL), cyChar*5,hwnd,(HMENU)ii,hInst,NULL); SendMessage(hwndList,LB_ADDSTRING,0,(LPARAM)szBuffer1); SendMessage(hwndList,LB_ADDSTRING,0,(LPARAM)szBuffer2); SendMessage(hwndList,LB_ADDSTRING,0,(LPARAM)szBuffer5); SendMessage(hwndList,LB_ADDSTRING,0,(LPARAM)szBuffer4); SendMessage(hwndList,LB_ADDSTRING,0,(LPARAM)szBuffer3); return 0; case WM_SIZE: cxClient=LOWORD(lParam); cyClient=GET_Y_LPARAM(lParam); MoveWindow(hwndButton,cxClient/2-cxChar*5,cyClient/2-cyChar*5,cxChar*10,cyChar*4,TRUE); return 0; case WM_COMMAND: if(wParam==i) { MessageBox(NULL,TEXT("你点击了按钮"),TEXT("tips"),MB_OK); } if(LOWORD(wParam)==ii&&HIWORD(wParam)==LBN_SELCHANGE) { iIndex=SendMessage(hwndList,LB_GETCURSEL,0,0); iLength=SendMessage(hwndList,LB_GETTEXTLEN,iIndex,0)+1; wsprintf(szBuffer,TEXT("你选择第%d 条,长度为 %d "),iIndex+1,iLength); MessageBox(NULL,szBuffer,TEXT("tips"),MB_ICONERROR); } return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,uMsg,wParam,lParam); }
顶住,再来一个:
#include <windows.h> #define ID_LIST 1 #define ID_TEXT 2 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("Environ") ; 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) (COLOR_WINDOW + 1) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName, TEXT ("Environment List Box"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; } void FillListBox (HWND hwndList) { int iLength ; TCHAR * pVarBlock, * pVarBeg, * pVarEnd, * pVarName ; pVarBlock = GetEnvironmentStrings () ; // Get pointer to environment block while (*pVarBlock) { if (*pVarBlock != '=') // Skip variable names beginning with '=' { pVarBeg = pVarBlock ; // Beginning of variable name while (*pVarBlock++ != '=') ; // Scan until '=' pVarEnd = pVarBlock - 1 ; // Points to '=' sign iLength = pVarEnd - pVarBeg ; // Length of variable name // Allocate memory for the variable name and terminating // zero. Copy the variable name and append a zero. pVarName = calloc (iLength + 1, sizeof (TCHAR)) ; CopyMemory (pVarName, pVarBeg, iLength * sizeof (TCHAR)) ; pVarName[iLength] = '\0' ; // Put the variable name in the list box and free memory. SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) pVarName) ; free (pVarName) ; } while (*pVarBlock++ != '\0') ; // Scan until terminating zero } FreeEnvironmentStrings (pVarBlock) ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static HWND hwndList, hwndText ; int iIndex, iLength, cxChar, cyChar ; TCHAR * pVarName, * pVarValue ; switch (message) { case WM_CREATE : cxChar = LOWORD (GetDialogBaseUnits ()) ; cyChar = HIWORD (GetDialogBaseUnits ()) ; // Create listbox and static text windows. hwndList = CreateWindow (TEXT ("listbox"), NULL, WS_CHILD | WS_VISIBLE | LBS_STANDARD, cxChar, cyChar * 3, cxChar * 16 + GetSystemMetrics (SM_CXVSCROLL), cyChar * 5, hwnd, (HMENU) ID_LIST, (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE), NULL) ; hwndText = CreateWindow (TEXT ("static"), NULL, WS_CHILD | WS_VISIBLE | SS_LEFT, cxChar, cyChar, GetSystemMetrics (SM_CXSCREEN), cyChar, hwnd, (HMENU) ID_TEXT, (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE), NULL) ; FillListBox (hwndList) ; return 0 ; case WM_SETFOCUS : SetFocus (hwndList) ; return 0 ; case WM_COMMAND : if (LOWORD (wParam) == ID_LIST && HIWORD (wParam) == LBN_SELCHANGE) { // Get current selection. iIndex = SendMessage (hwndList, LB_GETCURSEL, 0, 0) ; iLength = SendMessage (hwndList, LB_GETTEXTLEN, iIndex, 0) + 1 ; pVarName = calloc (iLength, sizeof (TCHAR)) ; SendMessage (hwndList, LB_GETTEXT, iIndex, (LPARAM) pVarName) ; // Get environment string. iLength = GetEnvironmentVariable (pVarName, NULL, 0) ; pVarValue = calloc (iLength, sizeof (TCHAR)) ; GetEnvironmentVariable (pVarName, pVarValue, iLength) ; // Show it in window. SetWindowText (hwndText, pVarValue) ; free (pVarName) ; free (pVarValue) ; } return 0 ; case WM_DESTROY : PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }