Win32 GUI编程之三--------ListView控件之OwnerData

dfasdfs

// Windows 头文件:
#include <windows.h>

// C 运行时头文件
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <stdio.h>
#include<CommCtrl.h>
#include<Strsafe.h>

struct RndItem
{
    int   iIcon;                 // Bitmap assigned to this item.
    UINT  state;                 // Item state value.
    TCHAR Title[1000];    // BUFFER_SIZE is a user-defined macro value.
    TCHAR SubText1[1000]; // Text for the label of the first sub-item.
    TCHAR SubText2[1000]; // Text for the label of the second item.
};

// 全局变量:
HINSTANCE hInst;								// 当前实例
TCHAR *szTitle=TEXT("HelloWorld");				// 标题栏文本
TCHAR *szWindowClass=TEXT("MainWin");			// 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR  lpCmdLine,int  nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此放置代码。
    MSG msg;
    //初始化控件库
    InitCommonControls();
    //注册窗口类
    MyRegisterClass(hInstance);
    // 执行应用程序初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }
    // 主消息循环:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    /**窗口类**/
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.hInstance		= hInstance;
    wcex.lpszClassName	= szWindowClass;
    wcex.lpfnWndProc	= WndProc;
    wcex.style			= CS_HREDRAW | CS_VREDRAW;
    /**窗口特性**/
    wcex.hIcon			= LoadIcon (NULL, IDI_APPLICATION);
    wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
    wcex.lpszMenuName	= NULL;
    wcex.cbClsExtra		= 0;
    wcex.cbWndExtra		= 0;
    /**窗口背景**/
    wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);

    return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;

    hInst = hInstance; // 将实例句柄存储在全局变量中

    hWnd = CreateWindow(szWindowClass,
                        szTitle,
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,
                        0,
                        CW_USEDEFAULT,
                        0,
                        NULL,
                        NULL,
                        hInstance,
                        NULL);
    if (!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return TRUE;
}

//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    DWORD       dwStyle;
    static HWND  hwndListView;
    LV_COLUMN   lvColumn;
    LV_ITEM     lvItem;
    switch (message)
    {
    case WM_CREATE:
        dwStyle =   WS_TABSTOP | WS_CHILD | WS_BORDER | WS_VISIBLE |LVS_AUTOARRANGE |LVS_REPORT | LVS_OWNERDATA;
        hwndListView = CreateWindowEx(   WS_EX_CLIENTEDGE,          // ex style
                                         WC_LISTVIEW,              // class name - defined in commctrl.h
                                         TEXT(""),                        // dummy text
                                         dwStyle,                   // style
                                         0,                         // x position
                                         0,                         // y position
                                         600,                         // width
                                         400,                         // height
                                         hWnd,                // parent
                                         NULL,        // ID
                                         hInst,                   // instance
                                         NULL);// no extra data
        /***添加表头***/
        lvColumn.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
        lvColumn.cx = 100;//列宽,可以为每个列指定
        // 第一列
        lvColumn.iSubItem = 0;//用于控制添加的顺序,默认从前向后添加
        lvColumn.pszText = TEXT("姓名");//列标题
        ListView_InsertColumn(hwndListView, 0, &lvColumn);
        // 第二列
        lvColumn.iSubItem = 1;//子项索引
        lvColumn.pszText = TEXT("年龄");
        ListView_InsertColumn(hwndListView, 1, &lvColumn);
        // 第三列
        lvColumn.iSubItem = 2;
        lvColumn.pszText = TEXT("地址");
        ListView_InsertColumn(hwndListView, 2, &lvColumn);
        //初始化
        //ListView_DeleteAllItems(hwndListView);
        ListView_SetItemCount(hwndListView, 100000);//设置项目数,分配内存使用
        break;
    case WM_NOTIFY:
        NMLVDISPINFO* plvdi;
        switch (((LPNMHDR) lParam)->code)
        {
        case LVN_GETDISPINFO:

            plvdi = (NMLVDISPINFO*)lParam;

            switch (plvdi->item.iSubItem)
            {
            case 0:
                plvdi->item.pszText = TEXT("aaa");
                break;

            case 1:
                plvdi->item.pszText = TEXT("bbb");
                break;

            case 2:
                plvdi->item.pszText = TEXT("ccc");
                break;

            default:
                break;
            }

            break;

        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}





dffasdf


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值