#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上通过