gpt4 book ai didi

c++ - 使我的 GUI 可通过键盘访问(Xp、Visual Studio、C++)

转载 作者:搜寻专家 更新时间:2023-10-31 02:00:53 27 4
gpt4 key购买 nike

我已经构建了一个带有按钮、按钮组、编辑、列表框等的 GUI,但现在我想知道如何通过键盘访问我的 gui,我的意思是,通过按 Tab 键更改焦点按钮。有没有人知道如何做到这一点?我使用的是 Windows Xp,GUI 是使用 Visual Studio 2008 在 C++ 上编写的。

非常感谢

更新:

INT APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

INITCOMMONCONTROLSEX ics;
ics.dwSize = sizeof(ics);
ics.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&ics);

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_PRUEBA, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_ACCEL_TABLE));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if ((!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) && (!IsDialogMessage(msg.hwnd, &msg)))
//if ((!IsDialogMessage(msg.hwnd, &msg)) & (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg) ))
//if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}

我的 GUI 中有很多控件,我应该把 WS_TABSTOP 放在所有控件中吗?如果我有一组按钮怎么办...我应该将 WS_TABSTOP 放在每个按钮和组中吗?仅在单个按钮中?

例如,我将粘贴我创建的组:

INT CrearControles(HWND hwnd, LPARAM lParam) {
HINSTANCE hInstance;
HFONT hfont;
HWND hctrl;
int i;

hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
hfont = (HFONT)GetStockObject( DEFAULT_GUI_FONT );

/* Insertar controles */
hctrl = CreateWindowEx(
0,
"BUTTON", /* Nombre de la clase */
"Rol", /* Texto del título */
BS_GROUPBOX | WS_CHILD | WS_VISIBLE | WS_GROUP , /* Estilo */
20, 15, /* Posición */
180, 100, /* Tamaño */
hwnd, /* Ventana padre */
(HMENU)GRUPO_ROL,/* Identificador del control */
hInstance, /* Instancia */
NULL); /* Sin datos de creación de ventana */
SendMessage(hctrl, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(TRUE, 0));

hctrl = CreateWindowEx(0, "BUTTON", "Receptor", BS_NOTIFY | BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 30, 35,
70, 25, hwnd, (HMENU)BOTON_RECEPTOR, hInstance, NULL);
SendMessage(hctrl, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(TRUE, 0));
SendDlgItemMessage(hwnd, BOTON_RECEPTOR, BM_SETCHECK, BST_CHECKED, 0);
SetFocus(hctrl);

hctrl = CreateWindowEx(0, "BUTTON", "Emisor", BS_NOTIFY | BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 30, 65,
70, 25, hwnd, (HMENU)BOTON_EMISOR, hInstance, NULL);
SendMessage(hctrl, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(TRUE, 0));


hctrl = CreateWindowEx(0, "STATIC", "Telefono", SS_SIMPLE | WS_CHILD | WS_VISIBLE, 150, 55, 100, 55, hwnd, (HMENU)LABEL_TELEFONO,
hInstance, NULL);
SendMessage(hctrl, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(TRUE, 0));
SetFocus(hctrl);

hctrl = CreateWindowEx(0, "EDIT", "", ES_READONLY | ES_LEFT | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP | ES_NUMBER , 115, 68,
80, 20, hwnd, (HMENU)EDIT_TELEFONO, hInstance, NULL);
SendMessage(hctrl, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(TRUE, 0));
SetFocus(hctrl);

return 1;
}

谢谢

最佳答案

如果您的 GUI 作为标准模态对话框运行,您应该免费获得控件之间的制表符和 Alt 键导航。

即:设置了 WS_TABSTOP 样式的控件,您应该可以使用 Tab 键切换到,定义了快捷键的控件(例如:标题为“&Do Something”的按钮应该可以通过 Alt+D 访问 - 而 D 应该下划线显示)。

如果您的窗口未作为标准模态对话框运行,要获得此行为,您的消息循环需要在分派(dispatch)每条消息之前调用 IsDialogMessage。

例如:

MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (!IsDialogMessage(m_hWndYourWindow, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

关于c++ - 使我的 GUI 可通过键盘访问(Xp、Visual Studio、C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1399062/

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