Search code examples
winapivisual-c++

Detect Button Press in Tab Page winapi C++


i am trying to Detect a Button Press in a Tab Page in visual c++

i tried making the tab control a subclass but the tabs disappeared!

relevant code for tab control and buttons:

    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;
    }

i am new to C++ if possible could answers please contain code examples


Solution

  • i tried making the tab control a subclass but the tabs disappeared!

    That is because your mySubClassProc() is not calling DefSubclassProc() for all unhandled messages. You are calling it only for unhandled WM_COMMAND messages.

    Move the DefSubclassProc() outside of your if (uMsg == WM_COMMAND) block, eg:

    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!
    }