在选项卡控件 Win32 中添加不同的组件 [英] Adding Different Components in Tab Control Win32

查看:37
本文介绍了在选项卡控件 Win32 中添加不同的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Visual Studio 2019 中使用 win32 api 创建 Windows 桌面应用程序.我知道还有许多其他选项可用于构建 UI,如 MFC、XAMAL 和 C#,但我需要在 win32 中构建它.我已经在 win32 api 中学习了一些基础知识,但最近我正在研究 tabControll,在那里我遇到了一个问题或者我错过了一些东西.我正在创建两个选项卡并希望为它们添加不同的内容.我当前的代码正在工作并创建选项卡,但它在两个选项卡中添加了相同的内容.我应该如何以不同的方式定义每个选项卡的内容.

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

推荐答案

tab控件在tab页切换时触发WM_NOTIFY信号,所以我们只要处理WM_NOTIFY消息,我们可以控制tab控制消息.

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.

WM_NOTIFY 消息中:

wParam:标识发送的 WM_NOTIFY 消息的控件 ID.

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

lParam:指向 NMHDR 结构.

因此,我们可以通过判断NMHDR 结构.

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.

我们可以使用 TCN_SELCHANGE 来处理Tab 选项卡更改时的操作,并使用 TabCtrl_GetCurSel 获取当前标签的索引并定义当前标签的内容.

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.

示例如下:

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

这篇关于在选项卡控件 Win32 中添加不同的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆