gpt4 book ai didi

窗口处于事件状态时无法切换键盘语言

转载 作者:太空宇宙 更新时间:2023-11-04 00:08:43 24 4
gpt4 key购买 nike

我正在学习 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;
}

最佳答案

问题在于 GetMessage( &message, window, 0, 0 ) 行。您只接收 window 的消息,而不是 testEditBox,因此不会发送某些消息。该行应该类似于 GetMessage(&message, NULL, 0, 0)

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

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

关于窗口处于事件状态时无法切换键盘语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12204289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com