3

我下载了一个非常基本的示例 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();
}
4

1 回答 1

3

WM_INITDIALOG仅发送到对话框,但您正在手动创建窗口。在尝试创建树控件之前,您应该InitCommonControls()在函数中调用 to。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

于 2013-10-10T18:30:48.830 回答