zoukankan      html  css  js  c++  java
  • Win32 SDK创建ListView控件

    相关链接:
    http://www.codeproject.com/KB/combobox/listview.aspx   CodeProject是个非常不错的网站,让我们可以吸收外国同行的编程经验。


    Win32 SDK利用ListView控件。
    为了使用ListView控件,我们需要初始化公共控件库,我们需要在程序刚刚启动时调用
    InitCommonControls() 函数,如果发生链接错误,说明我们没有链接拥有该函数的库文件。它们对应的
    头和库 DLL分别为 #include <commctrl.h> comctl32.lib comctl32.dll
    为了使用这个控件 我们就需要知道它的窗口类,利用Spy++等文件可以找到指定进程窗口的窗口类,
    而一个ListView控件也是一个子窗口,所以我们可以得到它的类名为syslistview32,其他的控件,
    我们只需要按照同样的道理来得到类名即可。
    有了类名还不够,我们还需要知道每种控件的风格,比如listView控件有以下的风格LVS_REPORT | LVS_SHOWSELALWAYS, 它表示要产生报表和总是显示。为了得到控件的风格,我们可以通过MSDN中MFC中的ListView风格来作参考。有了窗口类和风格,我们利用CreateWindow就可以创建并得到
    这个控件的句柄了。有了句柄,我们就可以随便控制了,具体要怎么看你自己的了。

    此外为了向ListView内插入项和列,我们需要两个结构体。
    LVITEM和LVCOLUMN

    它们的定义分别为
    typedef struct _LVITEM { 
    UINT mask;
    int iItem;
    int iSubItem;
    UINT state;
    UINT stateMask;
    LPTSTR pszText;
    int cchTextMax;
    int iImage;
    LPARAM lParam;
    #if (_WIN32_IE >= 0x0300)
    int iIndent;
    #endif
    #if (_WIN32_IE >= 0x560)
    int iGroupId;
    UINT cColumns; // tile view columns
    PUINT puColumns;
    #endif

    typedef struct _LVCOLUMN {
    UINT mask;
    int fmt;
    int cx;
    LPTSTR pszText;
    int cchTextMax;
    int iSubItem;
    #if (_WIN32_IE >= 0x0300)
    int iImage;
    int iOrder;
    #endif
    } LVCOLUMN, *LPLVCOLUMN;

    } LVITEM, *LPLVITEM;
    有了这两个结构体,我们就可以利用SendMessage来给ListView控件发送消息来为它添加项和列。
    我们分别通过下面两个消息来添加项和列。
    SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);
    SendMessage(hButton, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn);

    LVM_INSERTITEM表示添加项
    LVM_INSERTCOLUMN表示添加列。
    为了更好的查找关于ListView的消息,我们只需要在网上或MSDN 里查找 LVM_XXXXXX 就可以找到
    相关的消息了。最好自己整理出一份关于ListView的全部消息。

    WindowFrame &wndFrame = WindowFrame::Instance();
       
        GetWindowRect(hWnd, &rect);
        cx = rect.right-rect.left;
        cy = rect.bottom-rect.top;

        switch(msg)
        {
        case WM_CREATE:
            {
                rowIndex = 7;
                hButton = CreateWindow("syslistview32", "",
                                         WS_VISIBLE|WS_CHILD|WS_BORDER|
                                        LVS_REPORT | LVS_SHOWSELALWAYS,
                                        10, 20,
                                        cx-30,
                                        cy-100,
                                        hWnd, NULL, wndFrame.getInstance(), NULL);
                // 添加数据
                LV_ITEM item;       // 项
                LV_COLUMN colmn;     // 列
                ZeroMemory(&item, sizeof(LV_ITEM));
                ZeroMemory(&colmn, sizeof(LV_COLUMN));
               
                colmn.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM; // 风格
                colmn.cx = 0x28;
                colmn.pszText = "进程名"; // 文字
                colmn.cx = 0x42;         // 后面列
                SendMessage(hButton, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn);
                colmn.pszText = "内存使用";
                SendMessage(hButton, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn);
                colmn.pszText = "ID";
                SendMessage(hButton, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn);

                // 添加一些行项
                item.mask = LVIF_TEXT;       // 文字
                item.cchTextMax = MAX_PATH;       // 文字长度
                item.iItem = 0;
                item.iSubItem = 0;
                item.pszText = "中国";
                SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);
                item.pszText = "日本";
                SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);
                item.pszText = "德国";
                SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);
                item.pszText = "俄国";
                SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);
                item.pszText = "美国";
                SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);
                item.pszText = "英国";
                SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);
                item.pszText = "法国";
                SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);
            }
            break;
  • 相关阅读:
    【SQL】182. Duplicate Emails
    【SQL】181. Employees Earning More Than Their Managers
    【SQL】180. Consecutive Numbers
    【SQL】178. Rank Scores
    【SQL】177. Nth Highest Salary
    如何处理postman Self-signed SSL certificate blocked错误
    Radio checked 属性
    转成百分比
    内建函数
    队列
  • 原文地址:https://www.cnblogs.com/lzjsky/p/1886184.html
Copyright © 2011-2022 走看看