0

我正在尝试使用 win32 api 在 C++ 中创建一个列表视图,但是 mdsn 上提供的代码给了我一个错误。

HWND CreateListView (HWND hwndParent) 
{
    INITCOMMONCONTROLSEX icex;           // Structure for control initialization.
    icex.dwICC = ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&icex);

    RECT rcClient;                       // The parent window's client area.

    GetClientRect (hwndParent, &rcClient); 

    // Create the list-view window in report view with label editing enabled.
    HWND hWndListView = CreateWindow(WC_LISTVIEW, //ERROR red line under create window
                                     L"",
                                     WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
                                     0, 0,
                                     rcClient.right - rcClient.left,
                                     rcClient.bottom - rcClient.top,
                                     hwndParent,
                                     (HMENU)IDM_CODE_SAMPLES, //ERROR IDM CODE SAMPLES undefined
                                     g_hInst, //ERROR
                                     NULL); 

    return (hWndListView);
}

这个例子是来自 mdsn 的海峡,我不知道为什么它不起作用。我得到 IDM_CODE_SAMPLES 未定义,并且 createwindow 有问题。请帮助我完成这项工作,这将非常有帮助。

4

3 回答 3

2

IDM_CODE_SAMPLES是您要分配给控件的 ID。您可以将符号定义为数值,也可以直接使用数值(例如,选择 100)。如果您想引用特定控件,则 ID 很有用,尽管它HWND与 ID 一样有用。

g_hInst大概是一个类型为 的全局变量HMODULE,从 初始化WinMain。如果您不想使用全局变量,则可以GetModuleHandle(nullptr)在其位置调用,前提是您编译的是 .exe 而不是 .dll。

在学习 C++ 中 Win32 编程简介时,您将获得很多有用的信息。

于 2020-05-02T11:00:09.973 回答
1

我现在收到一个错误(1 个未解决的外部问题)

我们可以从InitCommonControlsEx函数中找到。

确保加载公共控件 DLL (Comctl32.dll),并从 DLL 注册特定的公共控件类。

添加:

#include <commctrl.h>    
#pragma comment(lib,"Comctl32.lib")

最小代码示例:

#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>
#include <commctrl.h>

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

#define IDM_CODE_SAMPLES 101

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HWND CreateListView(HWND hwndParent);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[] = L"Sample Window Class";

    WNDCLASS wc = { };

    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
    );
    CreateListView(hwnd);
    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    // Run the message loop.

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        EndPaint(hwnd, &ps);
    }
    return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

HWND CreateListView(HWND hwndParent)
{
    INITCOMMONCONTROLSEX icex;           // Structure for control initialization.
    icex.dwICC = ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&icex);

    RECT rcClient;                       // The parent window's client area.

    GetClientRect(hwndParent, &rcClient);

    // Create the list-view window in report view with label editing enabled.    
    HWND hWndListView = CreateWindow(WC_LISTVIEW, //ERROR red line under create window
        L"",
        WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
        0, 0,
        rcClient.right - rcClient.left,
        rcClient.bottom - rcClient.top,
        hwndParent,
        (HMENU)IDM_CODE_SAMPLES, //ERROR IDM CODE SAMPLES undefined
        GetModuleHandle(nullptr), //ERROR
        NULL);

    return (hWndListView);
}
于 2020-05-04T05:31:50.213 回答
0

以防万一其他人在 SysListView32 周围遇到问题:

#include <Ole2.h>
OleInitialize(NULL);
#include <commctrl.h>
#pragma comment(lib, "Shlwapi.lib")
#pragma comment(lib, "comctl32.lib")

INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwICC = ICC_LISTVIEW_CLASSES;
InitCtrls.dwSize = sizeof(INITCOMMONCONTROLSEX);
BOOL bRet = InitCommonControlsEx(&InitCtrls);
...

供参考:带有花里胡哨的完整工作示例:https ://github.com/jjYBdx4IL/Win32-List-View

于 2021-05-02T13:34:09.397 回答