如何使对话框中接收到WM_CHAR消息(Windows编程)

 我们大家都知道,对话框是有的时候捕获不到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上通过


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值