0

I am creating a Windows desktop application using win32 api in visual studio 2019. I know there are many other options avialable to build UI like MFC, XAMAL and C#, but i needed to build it in win32. I have learnt some basics in win32 api but recently i was working on tabControll and there i got an issue or i missed some thing. I am creating two tabs and want to add different content withing them. My current code is working and creating the tabs but it is adding same content in both tabs. How should i define each tab's content differently.

void createTabView(HWND hWnd) {
    RECT rcClient;
    INITCOMMONCONTROLSEX icex;
    static HWND hwndTab_1_1_1;

    HWND hwndTab;
    TCITEM tie;
    int i;
    TCHAR achTemp[256]; 

    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_TAB_CLASSES;
    GetClientRect(hWnd, &rcClient);

    hwndTab = CreateWindow(WC_TABCONTROL, L"",
        WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
        0, 0, rcClient.right, rcClient.bottom,
        hWnd, NULL, hInst, NULL);

    // Add tabs for each day of the week. 
    tie.mask = TCIF_TEXT | TCIF_IMAGE;
    tie.iImage = -1;

    tie.pszText = tabLBL1;
    TabCtrl_InsertItem(hwndTab, 1, &tie);
    tie.pszText = tabLBL2;
    TabCtrl_InsertItem(hwndTab, 2, &tie);

    SendMessage(hwndTab, WM_SETFONT,
        reinterpret_cast<WPARAM>(GetStockObject(DEFAULT_GUI_FONT)), 0);

    HWND hwndStatic = CreateWindow(WC_STATIC, L"",
        WS_CHILD | WS_VISIBLE | WS_BORDER,
        200, 200, 100, 100,        // Position and dimensions; example only.
        hwndTab, NULL, hInst,    // g_hInst is the global instance handle
        NULL);}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Sime DX
  • 23
  • 3

2 Answers2

0

Not sure if this is the solution, but the iItem parameter to TabCtrl_InsertItem is zero-based. Try:

tie.pszText = tabLBL1;
TabCtrl_InsertItem(hwndTab, 0, &tie);
tie.pszText = tabLBL2;
TabCtrl_InsertItem(hwndTab, 1, &tie);

Also (and I'm not sure if this is relevant), you look like you intended to call InitCommonControlsEx in your code but fail to do so.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
0

The tab control triggers the WM_NOTIFY signal when the tab page is switched, so as long as we process the WM_NOTIFY message, we can control the tab control message.

In the WM_NOTIFY message:

wParam: a control ID that identifies the WM_NOTIFY message sent.

lParam: a pointer to the NMHDR structure.

Therefore, we can determine the notification code sent by the Tab control in the WM_NOTIFY message processing program by judging the code value in the NMHDR structure.

We can use TCN_SELCHANGE to handle the operation when the Tab tab changes, and use TabCtrl_GetCurSel to get the index of the current tab and define the content of the current tab.

Here is the sample:

#include <Windows.h>
#include <commctrl.h>
LRESULT CALLBACK WndProc(HWND, UINT,WPARAM,LPARAM);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("windows");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc; 
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;
    if (!RegisterClass(&wndclass))
    { 
        MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
    }
    hwnd = CreateWindow(szAppName,
        TEXT("the hello program"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL);

    ShowWindow(hwnd,iCmdShow);
    UpdateWindow(hwnd);

    while (GetMessageW(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message,WPARAM wParam,LPARAM lParam)
{
    static HINSTANCE hInstance;
    static HWND hwndTab = 0 , hwndStatic = 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, "",
            WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
            0, 0, rcClient.right, rcClient.bottom,
            hwnd, NULL, hInstance, NULL);

        // Add tabs for each day of the week. 
        tie.mask = TCIF_TEXT | TCIF_IMAGE;
        tie.iImage = -1;
        wsprintf(tabLBL1, "tab1");
        tie.pszText = tabLBL1;
        TabCtrl_InsertItem(hwndTab, 0, &tie);
        wsprintf(tabLBL1, "tab2");
        TabCtrl_InsertItem(hwndTab, 1, &tie);
        SendMessage(hwndTab, WM_SETFONT,
            reinterpret_cast<WPARAM>(GetStockObject(DEFAULT_GUI_FONT)), 0);

        hwndStatic = CreateWindow(WC_STATIC, "",
            WS_CHILD | WS_VISIBLE | WS_BORDER,
            200, 200, 100, 100,        // Position and dimensions; example only.
            hwndTab, NULL, hInstance,    // g_hInst is the global instance handle
            NULL);
        ShowWindow(hwndStatic,TRUE);
        return 0;
    }

    case WM_DESTROY:        
        PostQuitMessage(0);
        return 0;
    case WM_NOTIFY:
        if (((LPNMHDR)lParam)->code == TCN_SELCHANGE)
        {
            int tabID = TabCtrl_GetCurSel(hwndTab);
            switch (tabID)
            {
            case 0:
                ShowWindow(hwndStatic, TRUE);
                break;
            case 1:
                ShowWindow(hwndStatic, FALSE);
                break;
            default:
                break;
            }
        }
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}
Zeus
  • 3,703
  • 3
  • 7
  • 20
  • Hi,if this answer did help to you, please feel free to mark it to help people with the same issue, and let me know if you have any problem. – Zeus Jul 07 '20 at 02:12
  • Hi, can you look into this my code. https://pastebin.com/kuTeugNM i think WM_NOTIFY IS NOT receiving any notification. – Sime DX Jul 08 '20 at 13:22
  • Maybe you should add WM_NOTIFY message in WndProc function instead of createTabView. And it seems that you haven't set createTabView as a callback function of a certain control. – Zeus Jul 09 '20 at 01:25
  • I have added message in WM_NOTIFY in WindProc function and it works perfectly now. Thanks – Sime DX Jul 13 '20 at 21:03