捕获WM_CHAR消息时如何检测UNICODE字符?

时间:2019-02-23 20:17:19

标签: c winapi

所以,我的窗口过程当前正在捕获所有WM_CHAR消息,它可以很好地处理ASCII字符,但是当我尝试类似的操作时:

case 'ф':
    MessageBox(NULL, TEXT("Ф detected"), TEXT("key"), MB_OK);

由于某种原因它失败了。

我的方法有什么问题以及如何解决?

编辑:源代码:

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     switch (message)
     {

          case WM_CHAR:
               switch(wParam)
               {

                  case '3':
                    MessageBox(NULL, TEXT("'3' was pressed"), TEXT("key detected"), MB_OK);
                    return 0;
                  case 'ф':
                    MessageBox(NULL, TEXT( "ф was pressed"), TEXT("key detected"), MB_OK);
                    return 0;


                  default:
                      return 0;
               }

          case WM_DESTROY :
               PostQuitMessage (0) ;
               return 0 ;


     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

1 个答案:

答案 0 :(得分:1)

需要将非Unicode字符替换为switch-case中的Unicode字符:

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     switch (message)
     {

          case WM_CHAR:
               switch(wParam)
               {

                  case L'3':
                    MessageBox(NULL, TEXT("'3' was pressed"), TEXT("key detected"), MB_OK);
                    return 0;
                  case L'ф':
                    MessageBox(NULL, TEXT( "ф was pressed"), TEXT("key detected"), MB_OK);
                    return 0;


                  default:
                      return 0;
               }

          case WM_DESTROY :
               PostQuitMessage (0) ;
               return 0 ;


     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

'ф'L'ф'的值不同。 'ф'和其他不在ASCII表中的字符的值通常解析为'?'(63)