2

我正在学习 Win32 API。我创建了一个空窗口并在其中添加了一个编辑框来输入一些文本。我可以在其中输入文字,但无法切换键盘语言。

Alt+Shift 等快捷键不起作用。如果我单击托盘中的语言图标,它会暂停几秒钟,然后最终会打开列表,但我的窗口将变为非活动状态,因此更改不会影响它。

如果我这样做,我可以改变语言:

  1. 按 Alt+Shift(或任何组合)
  2. 按 Alt、空格

与往常一样,第 2 步将导致弹出左上方的窗口菜单,但同时第 1 步也将变为现实。

我想,我没有处理某种消息,或者创建错误的窗口,但我找不到是什么错误。这是完整的代码:

#include <windows.h>

// handle of the edit box
HWND testEditBox;

// shows a message
void showMessage( LPSTR message ){
    MessageBox( GetActiveWindow(), message, "", MB_OK );
}

// a simple window procedure
LRESULT CALLBACK windowProc( HWND window, UINT message, WPARAM wparam, LPARAM lparam )
{
    switch( message )
    {
        case WM_CLOSE:
            DestroyWindow( window );
            break;
        case WM_DESTROY:
            PostQuitMessage( 0 );
            break;
        default:
            return DefWindowProc( window, message, wparam, lparam );
    }
    return 0;
}

int WINAPI WinMain( HINSTANCE instance, HINSTANCE prevInstance, LPSTR commandLine, int showMode )
{
    // create window class
    WNDCLASS windowClass;
    windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.style = 0;
    windowClass.hInstance = instance;
    windowClass.hCursor = LoadCursor( NULL, IDC_ARROW );
    windowClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    windowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    windowClass.lpszClassName = "testWindow";
    windowClass.lpfnWndProc = windowProc;

    if( !RegisterClass( &windowClass ) )
    {
        showMessage( "Some trouble, sir" );
        return GetLastError();
    }

    HWND window = CreateWindow
    (
        "testWindow",
        "Test Window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        400, 300,
        NULL, // no parent
        NULL, // no menu
        instance,
        NULL // no 'window-creation data'
    );

    if( !window ){
        showMessage( "Some trouble, sir" );
        return GetLastError();
    }

    ShowWindow( window, showMode );


    // add an EDIT box to our window
    testEditBox = CreateWindow
    (
        "EDIT",
        "input here",
        WS_CHILD,
        10, 10,
        200, 20,
        window,
        NULL,
        instance,
        NULL
    );
    ShowWindow( testEditBox, SW_SHOW );

    // start message loop
    MSG message;
    while( GetMessage( &message, window, 0, 0 ) > 0 )
    {
        TranslateMessage( &message );
        DispatchMessage( &message );
    }


    return message.wParam;
}
4

1 回答 1

0

问题出在一线GetMessage( &message, window, 0, 0 )。您仅接收window, not的testEditBox消息,因此不会发送一些消息。该行应该像GetMessage(&message, NULL, 0, 0).

当我按下并释放+时,这些是testEditBox您的应用程序中收到的消息:CtrlShift

WM_KEYDOWN nVirtKey:VK_CONTROL cRepeat:1 ScanCode:1D fExtended:0 fAltDown:0 fRepeat:0 fUp:0
WM_KEYDOWN nVirtKey:VK_SHIFT cRepeat:1 ScanCode:2A fExtended:0 fAltDown:0 fRepeat:0 fUp:0
WM_KEYUP nVirtKey:VK_SHIFT cRepeat:1 ScanCode:2A fExtended:0 fAltDown:0 fRepeat:1 fUp:1
WM_KEYUP nVirtKey:VK_CONTROL cRepeat:1 ScanCode:1D fExtended:0 fAltDown:0 fRepeat:1 fUp:1

将它们与修复后的消息进行比较:

WM_KEYDOWN nVirtKey:VK_CONTROL cRepeat:1 ScanCode:1D fExtended:0 fAltDown:0 fRepeat:0 fUp:0
WM_KEYDOWN nVirtKey:VK_SHIFT cRepeat:1 ScanCode:2A fExtended:0 fAltDown:0 fRepeat:0 fUp:0
WM_KEYUP nVirtKey:VK_SHIFT cRepeat:1 ScanCode:2A fExtended:0 fAltDown:0 fRepeat:1 fUp:1
WM_IME_NOTIFY dwCommand:IMN_CLOSESTATUSWINDOW dwData:00000000
WM_IME_NOTIFY dwCommand:IMN_OPENSTATUSWINDOW dwData:00000000
WM_INPUTLANGCHANGE charset:0 hkl:04090409 //this
WM_IME_NOTIFY dwCommand:IMN_SETCOMPOSITIONFONT dwData:00000000
WM_KEYUP nVirtKey:VK_CONTROL cRepeat:1 ScanCode:1D fExtended:0 fAltDown:0 fRepeat:1 fUp:1
于 2012-12-18T10:26:49.047 回答