我下载了一个非常基本的示例Win32应用程序,并希望为其添加树视图。这是我目前的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;
}
它像普通窗口一样工作。
我已经阅读了很多关于创建树视图的页面,但对于像我这样的菜鸟来说,它并不是很明显。我将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主题中,关闭按钮看起来不同。此外,我的菜单没有显示。
以下是创建树视图的代码(树视图未显示):
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();
}
答案 0 :(得分:2)
WM_INITDIALOG
仅发送到对话框,但您手动创建窗口。在尝试创建树控件之前,应该在InitCommonControls()
函数中调用WinMain()
。
像树一样的控件不能像顶级窗口那样正常运行。把你的窗口类放回原来的样子,这样你就有了一个工作窗口,然后在调用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
发送消息来开始为其添加项目。