gpt4 book ai didi

python - 如何使用 python win32gui 启用制表符和箭头键

转载 作者:太空狗 更新时间:2023-10-29 17:51:25 25 4
gpt4 key购买 nike

我在主窗口中创建了几个按钮(窗口),但 Tab 键和箭头键不起作用。我的研究表明,对于 C++,在消息泵中使用 IsDialogMessage 创建了 TranslateMessage/DispatchMessage 的旁路,如下所示以允许此功能:

while(GetMessage(&Msg, NULL, 0, 0))
{
if(!IsDialogMessage(g_hToolbar, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}

但是,我在 CreateWindows 中使用 python 和 win32gui 模块,但我不知道如何绕过正常的消息捕获以允许自然处理键盘。我的代码与此类似:

from win32gui import *
from win32con import *

window_class = WNDCLASS()
hinst = window_class.hInstance = GetModuleHandle(None)
window_class.lpszClassName = 'ClassName'
window_class.style = CS_VREDRAW | CS_HREDRAW
window_class.hCursor = LoadCursor(0, IDC_ARROW)
window_class.hbrBackground = COLOR_WINDOW
window_class.lpfnWndProc = {}
classAtom = RegisterClass(window_class)

hwnd = CreateWindow(classAtom, "", WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION
| WS_SYSMENU | WS_MINIMIZEBOX | WS_EX_TOPMOST | WS_CLIPSIBLINGS,
0, 0, 140, 100, 0, 0, GetModuleHandle(None), None)
btn1_hwnd = CreateWindow("Button", "btn 1", WS_GROUP | WS_TABSTOP | WS_VISIBLE
| WS_CHILD | BS_DEFPUSHBUTTON | WS_CLIPSIBLINGS,
10, 10, 100, 20, hwnd, 0, GetModuleHandle(None), None)
btn2_hwnd = CreateWindow("Button", "btn 2", WS_GROUP | WS_TABSTOP | WS_VISIBLE
| WS_CHILD | BS_DEFPUSHBUTTON | WS_CLIPSIBLINGS,
10, 40, 100, 20, hwnd, 0, GetModuleHandle(None), None)

UpdateWindow(hwnd)
PumpMessages()

编辑:使用这段代码,创建了一个带有两个按钮的窗口,但不可能将焦点从一个按钮移动到另一个按钮,尽管它们都有 WS_TABSTOP 标志。

根据 MSDN IsDialogMessage specification ,上面的 C++ 片段就是解决方案。

When IsDialogMessage processes a message, it checks for keyboard messages and converts them into selections for the corresponding dialog box. For example, the TAB key, when pressed, selects the next control or group of controls, and the DOWN ARROW key, when pressed, selects the next control in a group.

Because the IsDialogMessage function performs all necessary translating and dispatching of messages, a message processed by IsDialogMessage must not be passed to the TranslateMessage or DispatchMessage function.

所以,基本上,问题是:IsDialogMessage 是否可以从 Python 的 win2gui 中使用,或者是否有一些解决方法?

最佳答案

我找到了 CreateDialogIndirect 的例子在 programcreek.com 上的使用这会创建一个与问题中的窗口类似的窗口,其中的选项卡可以正常工作。在这里,稍作修改:

import win32con
import win32gui
import win32api

parent_hwnd = None
msgs = {}
style=win32con.WS_BORDER|win32con.WS_VISIBLE|win32con.WS_CAPTION|win32con.WS_SYSMENU ## |win32con.DS_SYSMODAL
h=win32gui.CreateDialogIndirect(
win32api.GetModuleHandle(None),
[['One ugly dialog box !',(100,100,200,100),style,0],
['Button','Create', win32con.IDOK, (10,10,30,20),win32con.WS_VISIBLE|win32con.WS_TABSTOP|win32con.BS_HOLLOW|win32con.BS_DEFPUSHBUTTON],
['Button','Never mind', win32con.IDCANCEL, (45,10,50,20),win32con.WS_VISIBLE|win32con.WS_TABSTOP|win32con.BS_HOLLOW],
['Static','Desktop name:',71,(10,40,70,10),win32con.WS_VISIBLE],
['Edit','',72,(75,40,90,10),win32con.WS_VISIBLE]],
parent_hwnd, msgs)

win32gui.EnableWindow(h,True)
hcontrol = win32gui.GetDlgItem(h,72)
win32gui.EnableWindow(hcontrol,True)
win32gui.SetFocus(hcontrol)
win32gui.PumpMessages()

关于python - 如何使用 python win32gui 启用制表符和箭头键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34257523/

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