如何用C++代码创建tabcontrol
时间: 2024-05-16 16:18:36 浏览: 117
tabcontrol_demo.rar_Tab 颜色_tab_tab c++_tabcontrol_demo_tab控件
5星 · 资源好评率100%
TabControl 是 Windows 窗体控件之一,用于显示多个选项卡,每个选项卡可以包含不同的控件。下面是一个简单的 C++ 代码示例,用于创建一个包含两个选项卡的 TabControl:
```c++
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("TabControl");
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);
return 0;
}
// 创建主窗口
hwnd = CreateWindow(szAppName, TEXT("TabControl Demo"),
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT,
400, 300, NULL, NULL, hInstance, NULL);
// 创建 TabControl
HWND hwndTab = CreateWindowEx(0, WC_TABCONTROL, NULL,
WS_CHILD | WS_VISIBLE,
0, 0, 400, 300,
hwnd, NULL, hInstance, NULL);
// 添加选项卡
TCITEM tie;
tie.mask = TCIF_TEXT;
tie.pszText = TEXT("Tab 1");
TabCtrl_InsertItem(hwndTab, 0, &tie);
tie.pszText = TEXT("Tab 2");
TabCtrl_InsertItem(hwndTab, 1, &tie);
// 显示窗口
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
// 消息循环
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
```
在上面的代码中,首先注册一个窗口类,然后创建一个主窗口和一个 TabControl。接下来,添加两个选项卡到 TabControl 中,并启动消息循环以等待用户交互。当用户关闭窗口时,程序会发送 WM_DESTROY 消息,退出应用程序。
阅读全文