As we all know, the dialog box sometimes cannot capture the WM_CHAR message. For example, you want to change all the Edit controls in the dialog box to uppercase. We do not hesitate to write:
# Include
// Declare the Dialog Procedure
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
HWndEdit = 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;
}
Sorry, I am sure you will fail. Why is the problem on WM_CHAR? You can try it. When you do not move the cursor to the Edit Control, the dialog box can be captured.
WM_CHAR message, but once you move the cursor to the Edit Control, WM_CHAR cannot be captured.
In this case, how can we capture WM_CHAR? I want to program MFC in a small Case. I just need to reload PreTranslateMessage.
However, for Windows programming, it is a little troublesome to use APIs. Here I provide two methods to convert them into uppercase.
1) capture the WM_CHAR message. In this case, the WMC_CHAR is not captured in the dialog box. If you don't need to talk about it, provide the code. Let's take a look.
# Include
// Declare the Dialog Procedure
Bool callback DialogProc (HWND, UINT, WPARAM, LPARAM );
Lresult callback NewEditProc (HWND, UINT, WPARAM, LPARAM );
// Define a gloabal var
WNDPROC 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) NewEditProc );
Return TRUE;
Case WM_COMMAND:
Switch (LOWORD (wParam )){
Case IDOK:
Case IDCANCEL:
EndDialog (hwndDlg, LOWORD (wParam ));
}
Return TRUE;
}
Return FALSE;
}
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 earthy, but it is a good way to achieve the goal. Provide the original code.
# Include
// Declare the Dialog Procedure
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 of the above are passed on VC6.0