码迷,mamicode.com
首页 > Windows程序 > 详细

Win32窗口高级列表控件的使用②

时间:2021-01-29 11:52:03      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:style   selected   com   info   数据   mds   chm   set   pts   

1、一键清空列表框中的所有数据;
2、点击按钮选中列表框中的第2行;
3、遍历列表框;

#include <Windows.h>
#include <tchar.h>
#include <CommCtrl.h>
TCHAR szWindowClass[] = _T("WindowClass");
TCHAR szTitle[] = _T("Title");
HWND hButtonControl_1 = nullptr; UINT IDC_BUTTON_1 = 200;
HWND hButtonControl_2 = nullptr; UINT IDC_BUTTON_2 = 201;
HWND hButtonControl_3 = nullptr; UINT IDC_BUTTON_3 = 202;
HWND hListViewControl_1 = nullptr; UINT IDC_LISTVIEW_1 = 300;

LRESULT CALLBACK WindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)    
{
    switch (uMsg)
    {
    case WM_CREATE:
    {
        hButtonControl_1 = CreateWindow(WC_BUTTON, _T("一键清空列表框中的所有数据"),WS_CHILD |WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON, CW_USEDEFAULT, CW_USEDEFAULT,
            220, 25, hWnd, (HMENU)IDC_BUTTON_1, NULL, NULL);
        hButtonControl_2 = CreateWindow(WC_BUTTON, _T("点击按钮选中列表框中的第2行"), WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON, 220, 0,
            220, 25, hWnd, (HMENU)IDC_BUTTON_2, NULL, NULL);
        hButtonControl_3 = CreateWindow(WC_BUTTON, _T("遍历列表框"), WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON, 440, 0,
            220, 25, hWnd, (HMENU)IDC_BUTTON_3, NULL, NULL);
        hListViewControl_1 = CreateWindow(WC_LISTVIEW, _T(""), WS_CHILD | WS_VISIBLE | WS_BORDER |LVS_REPORT, 0, 25,
            220, 400, hWnd, (HMENU)IDC_LISTVIEW_1, NULL, NULL);
        LVCOLUMN ColumnInfo_1 = { 0 };
        ColumnInfo_1.mask |= LVCF_TEXT | LVCF_WIDTH;
        ColumnInfo_1.cx = 55;
        ColumnInfo_1.pszText = (LPTSTR)_T("第0列");
        ListView_InsertColumn(hListViewControl_1,0, &ColumnInfo_1);
        ColumnInfo_1 = { 0 };
        ColumnInfo_1.mask |= LVCF_TEXT | LVCF_WIDTH;
        ColumnInfo_1.cx = 55;
        ColumnInfo_1.pszText = (LPTSTR)_T("第1列");
        ListView_InsertColumn(hListViewControl_1, 1, &ColumnInfo_1);
        ColumnInfo_1 = { 0 };
        ColumnInfo_1.mask |= LVCF_TEXT | LVCF_WIDTH;
        ColumnInfo_1.cx = 55;
        ColumnInfo_1.pszText = (LPTSTR)_T("第2列");
        ListView_InsertColumn(hListViewControl_1, 2, &ColumnInfo_1);
        ColumnInfo_1 = { 0 };
        ColumnInfo_1.mask |= LVCF_TEXT | LVCF_WIDTH;
        ColumnInfo_1.cx = 55;
        ColumnInfo_1.pszText = (LPTSTR)_T("第3列");
        ListView_InsertColumn(hListViewControl_1, 3, &ColumnInfo_1);

        LVITEM RowInfo_1 = { 0 };
        RowInfo_1.mask |= LVIF_TEXT;
        RowInfo_1.pszText = (LPTSTR)_T("AAA");
        RowInfo_1.iItem = 0;
        ListView_InsertItem(hListViewControl_1, &RowInfo_1);
        RowInfo_1 = { 0 };
        RowInfo_1.mask |= LVIF_TEXT;
        RowInfo_1.pszText = (LPTSTR)_T("AAA");
        RowInfo_1.iItem = 1;
        ListView_InsertItem(hListViewControl_1, &RowInfo_1);
        RowInfo_1 = { 0 };
        RowInfo_1.mask |= LVIF_TEXT;
        RowInfo_1.pszText = (LPTSTR)_T("AAA");
        RowInfo_1.iItem = 2;
        ListView_InsertItem(hListViewControl_1, &RowInfo_1);
    }
        break;
    case WM_COMMAND:
    {
        UINT nControlID = LOWORD(wParam);
        UINT nNotificationCode = HIWORD(wParam);
        if (nNotificationCode == BN_CLICKED)
        {
            if (nControlID == IDC_BUTTON_1)
            {
                ListView_DeleteAllItems(hListViewControl_1);    //一键清空列表框中的所有数据;
            }
            if (nControlID == IDC_BUTTON_2)
            {
                ListView_SetItemState(hListViewControl_1, 1, LVIS_SELECTED, LVIS_SELECTED);    //点击按钮选中列表框中的第2行
            }
            if (nControlID == IDC_BUTTON_3)
            {
                for (int iItemIdx = ListView_GetNextItem(hListViewControl_1, -1, LVNI_ALL);
                    iItemIdx >= 0; 
                    iItemIdx = ListView_GetNextItem(hListViewControl_1, iItemIdx, LVNI_ALL))
                {
                    TCHAR szText[128] = {0};
                    ListView_GetItemText(hListViewControl_1, iItemIdx, 0, szText, 128);
                    MessageBox(hWnd, szText, _T(""), NULL);
                }
            }
        }
    }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
        break;
    }
    return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
    WNDCLASSEX wcex = { 0 };
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.lpfnWndProc = WindowProc;
    wcex.lpszClassName = szWindowClass;
    if (!RegisterClassEx(&wcex)) return 0;
    HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        800, 600, NULL, NULL, hInstance, NULL);
    if (!hWnd)return 0;
    ShowWindow(hWnd, SW_SHOW);
    MSG msg = { 0 };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

 

Win32窗口高级列表控件的使用②

标签:style   selected   com   info   数据   mds   chm   set   pts   

原文地址:https://www.cnblogs.com/SakuraQAQ/p/14340813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!