发送消息TVM_EDITLABEL失败

时间:2020-06-14 13:47:10

标签: winapi

TreeView_EditLabel(tv, item)失败(返回零)。

item有效,并且是树视图中的唯一项。

出什么问题了?有一些先决条件吗?还是不起作用?

我在Windows 10上。

这是一个最小的可复制示例

#include <windowsx.h>
#include <CommCtrl.h>
#include <assert.h>

#pragma comment(lib, "Comctl32.lib")

HWND hTv;
HTREEITEM hItem;
auto className = L"someclass";

ATOM                MyRegisterClass(HINSTANCE hInstance);
void                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
    InitCommonControls();
    MyRegisterClass(hInstance);
    InitInstance(hInstance, nCmdShow);
    MSG msg;
    while (GetMessage(&msg, nullptr, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex{};

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.lpszClassName = className;

    return RegisterClassExW(&wcex);
}
void InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd = CreateWindowW(className, L"", WS_OVERLAPPEDWINDOW, 400, 400, 400, 400, nullptr, nullptr, hInstance, nullptr);
    assert(hWnd);

    hTv = CreateWindowW(WC_TREEVIEW, L"Tree View", WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES, 0, 0, 300, 300, hWnd, 0, 0, NULL);
    assert(hTv);
    {
        TVITEM tvi{};
        TVINSERTSTRUCT tvins{};
        tvi.mask = TVIF_TEXT;

        wchar_t name[] = L"item";
        tvi.pszText = name;

        tvins.item = tvi;
        hItem = TreeView_InsertItem(hTv, &tvins);
    }
    ShowWindow(hWnd, nCmdShow);
    SetFocus(hTv);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_NOTIFY:
    {
        switch (reinterpret_cast<LPNMHDR>(lParam)->code)
        {
        case NM_CLICK:
            assert(TreeView_EditLabel(hTv, hItem));
            break;
        }
        break;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

树视图控件需要设置TVS_EDITLABELS控件样式以支持编辑树视图项的标签。

您需要替换树视图控件创建代码

hTv = CreateWindowW(
    WC_TREEVIEW,
    L"Tree View",
    WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES,
    0, 0, 300, 300,
    hWnd,
    0,
    0,
    NULL);

与此:

hTv = CreateWindowW(
    WC_TREEVIEW,
    L"Tree View",
    WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES | TVS_EDITLABELS,
    0, 0, 300, 300,
    hWnd,
    0,
    0,
    NULL);

tree-view controls overview概述在标题为tree-view label edition的部分下提供了更多详细信息。