我已经构建了一个带有按钮、按钮组、编辑、列表框...等的 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 作为标准模式对话框运行,您应该可以免费在控件之间使用 Tab 键和 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/