我們大家都知道,對話方塊是有的時候捕獲不到WM_CHAR訊息的.比如,你想使對話方塊裡的Edit控制項所鍵入的全部變為大寫.我們毫不猶豫的寫到:
#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;
}
很遺憾,我敢肯定地告訴你你將會失敗,為什麼,問題就是出在WM_CHAR上,你可以試一試,當你不把游標移動到Edit控制項時,對話方塊可以捕獲到
WM_CHAR訊息,但是一旦你把游標移動到Edit控制項時,就捕獲不到WM_CHAR了.
出現了這種情況,有什麼方法可以捕獲到WM_CHAR呢?我想對於MFC編程,小Case了,只需重載PreTranslateMessage.
可是對於Windows編程,利用API來寫有點麻煩,這裡我提供2種方法來達到變為大寫的目的.
1)捕獲WM_CHAR訊息,在這裡其實不是對話方塊真正的捕獲WMC_CHAR.多話不說,還是提供代碼吧,大家自己去看.
#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)第二種方法有點土了,不過達到目的就是好方法.還是提供原代碼吧.
#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;
}
以上均在VC6.0上通過