How to get WM in the dialog

xiaoxiao2021-03-06  155

We all know that the dialog box is sometimes caught in the WM_CHAR message. For example, you want to make all the EDIT controls in the dialog box to top. We don't hesitate to write: #include

// Declare the dialog processure

Bool Callback DialogProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // pointer to command line int nCmdShow // show state of window) {int ret = DialogBoxParam (hInstance, MAKEINTRESOURCE ( IDD_DIALOG1), NULL, DIALOGPROC, 0); RETURN (RET! = IDOK);

BOOL CALLBACK DialogProc (HWND hwndDlg, // handle to dialog box UINT uMsg, // message WPARAM wParam, // first message parameter LPARAM lParam // second message parameter) {HWND hWndEdit; switch (uMsg) {case WM_INITDIALOG: // Get A Handle to the Edit Control HWndIt = Getdlgitem (HWNDDLG, IDC_EDIT1); RETURN TRUE;

break; case WM_CHAR: wParam = toupper (wParam); break; case WM_COMMAND: switch (LOWORD (wParam)) {case IDOK: case IDCANCEL: // Close the dialog EndDialog (hwndDlg, LOWORD (wParam));} return TRUE; } Return false;} Unfortunately, I will definitely tell you that you will fail, why, the problem is on WM_CHAR, you can try it, when you don't move the cursor to the Edit control, the dialog can capture WM_CHAR message, but once you move the cursor to the EDIT control, you can't capture WM_CHAR.

This happens, what is the method to capture WM_CHAR? I want to programmatically, small case, just overload PretranslateMessage. But for Windows programming, use the API to write a bit trouble, here I offer 2 methods The purpose of achieving the uppercase. 1) Capture the WM_CHAR message, in fact, not the truly capture of the dialog, the truly capture of WMC_CHAR. Do not say, or provide the code, everyone go to see.

#include

// Declare the dialog processure

Bool Callback DialogProc (HWND, UINT, WPARAM, LPARAM);

LResult Callback NeweditProc (HWND, UINT, WPARAM, LPARAM);

// Define a gloabal varWNDPROC g_Edit; int WINAPI WinMain (HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // pointer to command line int nCmdShow // show state of window) {int ret = DialogBoxParam (hInstance, MAKEINTRESOURCE (IDD_DIALOG1), NULL, DialogProc, 0); return (ret = IDOK!);} BOOL CALLBACK DialogProc (HWND hwndDlg, // handle to dialog box UINT uMsg, // message WPARAM wParam, / / first message parameter LPARAM lParam // second message parameter) {HWND hWndEdit; switch (uMsg) {case WM_INITDIALOG: hWndEdit = GetDlgItem (hwndDlg, IDC_EDIT1); // Subclass the Edit control g_Edit = (WNDPROC) SetWindowLong (hWndEdit, GWL_WNDPROC, (Long); return true; case wm_command: switch (loword (wparam)) {copy IDOK: Case IDCancel: EndDialog (hwnddlg, loword (wparam));} return true;

LRESULT CALLBACK NewEditProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {TCHAR chCharCode; switch (message) {case WM_CHAR: wParam = toupper (wParam); break;} return CallWindowProc (g_Edit, hwnd, message, wParam, lParam }

2) The second method is a bit soil, but the purpose is a good way. Or the original code is provided.

#include

// Declare the dialog processure

Bool Callback DialogProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // pointer to command line int nCmdShow // show state of window) {int ret = DialogBoxParam (hInstance, MAKEINTRESOURCE ( IDD_DIALOG1), NULL, DIALOGPROC, 0); RETURN (RET! = IDOK);

BOOL CALLBACK DialogProc (HWND hwndDlg, // handle to dialog box UINT uMsg, // message WPARAM wParam, // first message parameter LPARAM lParam // second message parameter) {HWND hWndEdit; switch (uMsg) {case WM_INITDIALOG: hWndEdit = GetDlgItem (hwndDlg, IDC_EDIT1); return TRUE; case WM_COMMAND: switch (LOWORD (wParam)) {case IDC_EDIT1: if (HIWORD (wParam) == EN_CHANGE) {TCHAR szString [100] = {0}; GetDlgItemText (hwndDlg, IDC_EDIT1, Szstring, 99); int Nlen = 0; int index = 0; Nlen = lstrlen (szstring); for (index = 0; index {

IF (szstring [index] <= 'z' && szstring [index]> = 'a')

{

Szstring [index] - = 32;

}

}

Szstring [NLEN] = 0;

SendMessage (Getdlgitem (Hwnddlg, Idc_edit1), WM_SETTEXT, (WPARAM) 0, (LPAram) (LPCSTR) szstring;

SendMessage (Getdlgitem (Hwnddlg, IDC_EDit1), EM_SETSEL, LSTRLEN (SZString), LSTRLEN (SZString);

}

Break;

Case IDOK:

Case IDCANCEL:

EndDialog (Hwnddlg, Loword (WPARAM));

}

Return True;

}

Return False;

}

All above is passed on VC6.0

转载请注明原文地址:https://www.9cbs.com/read-128425.html

New Post(0)