gpt4 book ai didi

c - Win32 创建 TreeView

转载 作者:太空宇宙 更新时间:2023-11-03 23:29:25 26 4
gpt4 key购买 nike

我下载了一个非常基本的示例 Win32 应用程序,并想向其中添加一个 TreeView 。这是我的 WinMain。

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = NULL;

if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}

hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"My Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

while(GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

return Msg.wParam;
}

它像普通窗口一样工作。

我已经阅读了很多关于创建 TreeView 的页面,但对于像我这样的菜鸟来说,这并不明显。我将 CreateWindowEx 函数中的类更改为 WC_TREEVIEW,因此我的 WinMain 现在看起来像这样:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = NULL;

/*if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}*/

hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
/*g_szClassName*/WC_TREEVIEW,
"My Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

while(GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

return Msg.wParam;
}

现在的问题是,窗口没有加载到我的 Windows 主题中,关闭按钮看起来不一样。另外,我的菜单没有显示。

这是创建 TreeView 的代码( TreeView 不显示):

struct treeView {
HWND hwnd;
TV_INSERTSTRUCT insert;
HTREEITEM parent;
HTREEITEM before;
HTREEITEM root;
};

case WM_CREATE:
{
struct treeView resourcesTreeView;

resourcesTreeView.hwnd = GetDlgItem(hwnd, ID_RESOURCES_TREE_VIEW);
resourcesTreeView.insert.hParent = NULL;
resourcesTreeView.insert.hInsertAfter = TVI_ROOT;
resourcesTreeView.insert.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
resourcesTreeView.insert.item.pszText = "Parent";
resourcesTreeView.insert.item.iImage = 0;
resourcesTreeView.insert.item.iSelectedImage = 1;
resourcesTreeView.parent = (HTREEITEM)SendDlgItemMessage(hwnd, ID_RESOURCES_TREE_VIEW, TVM_INSERTITEM, 0, (LPARAM)&resourcesTreeView.insert);
resourcesTreeView.root = resourcesTreeView.parent;
resourcesTreeView.before = resourcesTreeView.parent;
UpdateWindow(hwnd);
}

我已确保包括:

case WM_INITDIALOG:
{
/*INITCOMMONCONTROLSEX icc;
icc.dwSize = sizeof(icc);
icc.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&icc);*/
InitCommonControls();
}

最佳答案

WM_INITDIALOG 仅发送到对话框,但您正在手动创建窗口。在尝试创建树控件之前,您应该在 WinMain() 函数中调用 InitCommonControls()

像树这样的控件不能像顶层窗口那样工作,而这正是您想要做的。将您的窗口类恢复到原来的状态,以便您有一个工作窗口,然后在调用 ShowWindow() 之后添加以下:

RECT rc;
GetClientRect(hwnd, &rc);

HWND hwndTree = CreateWindowEx(
WS_EX_CLIENTEDGE,
WC_TREEVIEW,
0,
WS_CHILD | WS_VISIBLE,
0, 0, rc.right, rc.bottom,
hwnd, NULL, hInstance, NULL);

这应该为您提供一个树控件作为顶级窗口的子控件。然后,您可以通过向 hwndTree 发送消息来开始尝试向其中添加项目。

关于c - Win32 创建 TreeView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19303311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com