检测标签页中的按钮按下 winapi C++

问题描述 投票:0回答:1

我正在尝试在 Visual C++ 中检测标签页中的按钮按下

我尝试将选项卡控件设为子类,但选项卡消失了!

选项卡控件和按钮的相关代码:

    HWND initTab1(HWND hwndTab) {
        EnumChildWindows(hwndTab /* parent hwnd*/, DestoryChildCallback, NULL);//clear tab control
        HWND hwndButton1 = CreateWindow(L"BUTTON", L"Visual Studio 2022", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 105, 25, 50, 30, hwndTab, (HMENU)ID_VS2022, hInst, NULL);
        return hwndTab;
    }

    // attempt at subclassing
    LRESULT CALLBACK mySubClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
    {
        if (uMsg == WM_COMMAND)
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            if (wmId == ID_VS2022) {
                ShellExecute(hWnd, _T("open"), _T("C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\IDE\\devenv.exe"), NULL, NULL, SW_RESTORE);
            } else {
                return DefSubclassProc(hWnd, uMsg, wParam, lParam);
            }
        }
    }

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        static HINSTANCE hInstance;
        static HWND hwndTab = 0;
        TCITEM tie;
        RECT rcClient;
        INITCOMMONCONTROLSEX icex;
        icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
        icex.dwICC = ICC_TAB_CLASSES;
        TCHAR tabLBL1[256];
        GetClientRect(hWnd, &rcClient);
        switch (message)
        {
        case WM_CREATE:
        {
            hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
            hwndTab = CreateWindow(WC_TABCONTROL, L"", WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE, 0, 0, rcClient.right, rcClient.bottom, hWnd, NULL, hInstance, NULL);
 
            tie.mask = TCIF_TEXT | TCIF_IMAGE;
            tie.iImage = -1;
            wsprintf(tabLBL1, L"IDEs and Text Editors");
            tie.pszText = tabLBL1;
            TabCtrl_InsertItem(hwndTab, 0, &tie);
            wsprintf(tabLBL1, L"tab2");
            TabCtrl_InsertItem(hwndTab, 1, &tie);
            ShowWindow(initTab1(hwndTab), TRUE);
            return 0;
        }
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }

我是 C++ 新手 如果可能的话可以回答请包含代码示例

winapi visual-c++
1个回答
0
投票

我尝试将选项卡控件设为子类,但选项卡消失了!

那是因为您的

mySubClassProc()
没有为所有未处理的消息调用
DefSubclassProc()
。您仅为未处理的
WM_COMMAND
消息调用它。

DefSubclassProc()
移到
if (uMsg == WM_COMMAND)
块之外,例如:

LRESULT CALLBACK mySubClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    if (uMsg == WM_COMMAND)
    {
        int wmId = LOWORD(wParam);
        // Parse the menu selections:
        if (wmId == ID_VS2022) {
            ShellExecute(hWnd, _T("open"), _T("C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\IDE\\devenv.exe"), NULL, NULL, SW_RESTORE);
            return 0; // <-- add this!
        }
    }
    return DefSubclassProc(hWnd, uMsg, wParam, lParam); // <-- move here!
}
© www.soinside.com 2019 - 2024. All rights reserved.