如何使對話方塊中接收到WM_CHAR訊息(Windows編程)

來源:互聯網
上載者:User
 我們大家都知道,對話方塊是有的時候捕獲不到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上通過



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.