2016-05-19 30 views
0

以下是該窗口包含3套控制我WINAPI程序:製表鍵子不工作的控制

  1. ,在窗口中直接組別創建
  2. 的控件控制
  3. 在第2組

控件當我按Tab鍵,它只在遍歷SET1。 爲什麼我不能通過Tab鍵切換到group1和group2的子控件?

#include <Windows.h> 

LRESULT __stdcall WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { 
    switch (message) 
    { 
    case WM_CTLCOLORSTATIC: 
    { 
     HDC hdcStatic = (HDC)wParam; 
     SetTextColor(hdcStatic, RGB(0, 0, 255)); 
     SetBkMode(hdcStatic, TRANSPARENT); 
     return (LRESULT)GetStockObject(NULL_BRUSH); 
    } 
    case WM_DESTROY: 
     PostQuitMessage(0); 
     break; 
    default: 
     return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
    return 0; 
} 
//////////////////////////////////////////////////////////////////////////// 
void CreateEdit(const HWND &parent, const int &x, const int &y, const int &id) { 
    CreateWindow(L"EDIT", L"", WS_BORDER | WS_CHILD | WS_VISIBLE | WS_TABSTOP, x, y, 200, 20, parent, (HMENU)id, NULL, NULL); 
} 
//////////////////////////////////////////////////////////////////////////// 
int __stdcall wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int  nCmdShow) 
{ 
    WNDCLASSEX wndclass{}; 

    wndclass.cbSize = sizeof(wndclass); 
    wndclass.style = CS_HREDRAW | CS_VREDRAW; 
    wndclass.lpfnWndProc = WndProc; 
    wndclass.hInstance = hInstance; 
    wndclass.hbrBackground = CreateSolidBrush(RGB(255, 128, 255)); 
    wndclass.lpszClassName = L"test"; 

    RegisterClassEx(&wndclass); 

    HWND hWndMainWindow = CreateWindow(
     wndclass.lpszClassName, 
     L"test", 
     WS_EX_OVERLAPPEDWINDOW | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, 
     100, 100, 500, 500, 
     NULL, NULL, hInstance, NULL); 

    ::ShowWindow(hWndMainWindow, SW_SHOW); 
    ::UpdateWindow(hWndMainWindow); 

    //Creating Controls~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    int x = 10, y = 20, id = 100; 
    HWND g1 = CreateWindow(L"button", L"Group1", WS_CHILD | WS_VISIBLE | BS_GROUPBOX | WS_TABSTOP | WS_GROUP, 0, 50, 220, 200, hWndMainWindow, (HMENU)++id, NULL, NULL); 
    //Controls in Group1 
    CreateEdit(g1, x, (y += 30), ++id); 
    CreateEdit(g1, x, (y += 30), ++id); 
    CreateEdit(g1, x, (y += 30), ++id); 
    CreateEdit(g1, x, (y += 30), ++id); 

    HWND g2 = CreateWindow(L"button", L"Group2", WS_CHILD | WS_VISIBLE | BS_GROUPBOX | WS_TABSTOP | WS_GROUP, 260, 50, 220, 200, hWndMainWindow, (HMENU)++id, NULL, NULL); 
    y = 20; 
    //Controls in Group2 
    CreateEdit(g2, x, (y += 30), ++id); 
    CreateEdit(g2, x, (y += 30), ++id); 
    CreateEdit(g2, x, (y += 30), ++id); 
    CreateEdit(g2, x, (y += 30), ++id); 

    //The controls that create directly in the main window 
    y = 270; 
    CreateWindow(L"static", L"Main Window Controls", WS_CHILD | WS_VISIBLE, x, y, 200, 30, hWndMainWindow, (HMENU)++id, NULL, NULL); 

    CreateEdit(hWndMainWindow, x, (y += 30), ++id); 
    CreateEdit(hWndMainWindow, x, (y += 30), ++id); 
    CreateEdit(hWndMainWindow, x, (y += 30), ++id); 
    CreateEdit(hWndMainWindow, x, (y += 30), ++id); 
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

    MSG msg; 

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

    return (int)msg.wParam; 
} 
+0

你確定焦點不動嗎?您正在使用'NULL_BRUSH'來渲染您的編輯控件,也許您沒有看到焦點矩形移動。 – IInspectable

+0

焦點僅移動到主窗口的控件而不移動到組的控件。 – SaeidMo7

+1

我不是100%肯定的,但我不認爲groupboxes應該是他們似乎包含的控件的父母。由於WS_GROUP風格的作用,我得到了這個想法。 – Stuart

回答

2

託管「組1」和「組2」的兩個子窗口都需要用WS_EX_CONTROLPARENT樣式標記爲「控制父母」。這種風格是指:

本身包含應參加 對話框導航子窗口的窗口。如果執行導航 操作,如處理TAB鍵,方向鍵或鍵盤 助記符

更改您的代碼來創建這些窗口時,這種風格被指定,對話管理 遞歸到該窗口的孩子。如下:

HWND g1 = CreateWindowEx(WS_EX_CONTROLPARENT, L"button", L"Group1", WS_CHILD | WS_VISIBLE | BS_GROUPBOX | WS_TABSTOP | WS_GROUP, 0, 50, 220, 200, hWndMainWindow, (HMENU)++id, NULL, NULL); 
HWND g2 = CreateWindowEx(WS_EX_CONTROLPARENT, L"button", L"Group2", WS_CHILD | WS_VISIBLE | BS_GROUPBOX | WS_TABSTOP | WS_GROUP, 260, 50, 220, 200, hWndMainWindow, (HMENU)++id, NULL, NULL); 

(順便說一句,這似乎是錯誤的使您的Windows組框的孩子他們不需要是 - 你可以放置它們的內部,但讓他們的兄弟姐妹它代替小組。專門設計用於這種方式 - 我不知道我們如何將它作爲兒童與控件一起工作。)

+0

謝謝,它工作。 – SaeidMo7

1

既然你沒有創建一個對話框,你的主窗口應與WS_EX_CONTROLPARENT樣式創建。

確保您只將此樣式放置在主窗口上。孩子的窗戶不應該使用這種風格。

+0

我只對主窗口使用了WS_EX_CONTROLPARENT風格,其他控件沒有這種風格,但它還沒有工作。 – SaeidMo7

+1

@ SaeidMo7使用['CreateWindowEx'](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632680(v = vs.85).aspx)來使用擴展窗口樣式(第一個參數),它們不會在'CreateWindow'上工作。 – BeyelerStudios

+1

它不是需要它的主窗口,而是託管「組1」和「組2」的兩個子窗口。 –