win32 listview控件添加图标

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <windows.h>  
  3. #include <CommCtrl.h>  
  4. #include "resource.h"  
  5. #pragma comment(lib,"comctl32.lib")  
  6.   
  7. HINSTANCE g_hInst;  
  8. HWND    g_hMainWnd;  
  9. HWND hListView;  
  10.   
  11. void InsertListviewItem(int nPos, WCHAR * wszStr,WCHAR * wszSecond, WCHAR * wszThrid);  
  12. void CreateListView();  
  13.   
  14. INT_PTR CALLBACK DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)  
  15. {  
  16.     switch (uMsg)  
  17.     {  
  18.     case WM_INITDIALOG:  
  19.         g_hMainWnd = hWnd;        
  20.         CreateListView();  
  21.         break;  
  22.     case WM_COMMAND:  
  23.         break;        
  24.     case WM_DESTROY:  
  25.         PostQuitMessage(0);  
  26.         break;  
  27.     case WM_CLOSE:  
  28.         PostQuitMessage(0);  
  29.         break;  
  30.     default:  
  31.         return FALSE;  
  32.     }  
  33.     return  TRUE;  
  34. }  
  35. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)  
  36. {  
  37.     g_hInst = hInstance;  
  38.     g_hMainWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_CLIENT_DIALOG), GetDesktopWindow(), DlgProc);  
  39.     if (g_hMainWnd == 0)  
  40.     {  
  41.         return -1;  
  42.     }  
  43.     RECT wndRect;  
  44.     GetWindowRect(g_hMainWnd, &wndRect);  
  45.     int nScreenX = GetSystemMetrics(SM_CXSCREEN);  
  46.     int nScreeny = GetSystemMetrics(SM_CYSCREEN);  
  47.       
  48.     SetWindowPos(g_hMainWnd, HWND_TOP, nScreenX / 2 - (wndRect.right - wndRect.left) / 2,  
  49.         nScreeny / 2 - (wndRect.bottom - wndRect.top) / 2, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);  
  50.   
  51.     MSG msg;  
  52.     while (GetMessage(&msg, NULL, 0, 0))  
  53.     {  
  54.         TranslateMessage(&msg);  
  55.         DispatchMessage(&msg);  
  56.     }  
  57.     return 0;  
  58. }  
  59.   
  60. void CreateListView()  
  61. {     
  62.     hListView = GetDlgItem(g_hMainWnd, IDC_LISTView);  
  63.     LONG lStyle = GetWindowLong(hListView, GWL_STYLE);//获取当前窗口style  
  64.     lStyle &= ~LVS_TYPEMASK;   
  65.     lStyle |= LV_VIEW_DETAILS; //这个样式可以有好几种,LV_VIEW_DETAILS表示详细,LV_VIEW_ICON表示大图标,LV_VIEW_SMALLICON表示小图标,LV_VIEW_LIST表示列表,LV_VIEW_TILE表示平铺  
  66.       
  67.     ListView_SetExtendedListViewStyle(hListView, LVS_EX_FULLROWSELECT); //增加获取当行样式  
  68.       
  69.     SetWindowLong(hListView, GWL_STYLE, lStyle);//设置style  
  70.   
  71.     HMODULE hShell = LoadLibraryA("shell32.dll"); //windows的系统图标全部放在了shell32.dll这个动态链接库里,我们可以加载这个dll来获取图标  
  72.     HICON hIcon = LoadIcon(hShell, MAKEINTRESOURCE(17)); //17表示的是打印机的图标,至于其他图标的号码上网搜索一下就有了  
  73.     HIMAGELIST hImageList = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 0);  
  74.     ImageList_AddIcon(hImageList, hIcon);  
  75.     ListView_SetImageList(hListView, hImageList, LVSIL_SMALL); //当lStyle为LV_VIEW_SAMLLICON和LV_VIEW_TILE时,第三个参数应为LVSIL_NORMAL  
  76.     LVCOLUMN col;  
  77.     col.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;  
  78.     col.cx = 30;  
  79.     col.iSubItem = 0;  
  80.     col.pszText = L"ID";  
  81.       
  82.     ListView_InsertColumn(hListView, 0, &col);  
  83.     col.cx = 100;  
  84.     col.iSubItem = 0;  
  85.     col.pszText = L"SecTest";  
  86.     ListView_InsertColumn(hListView, 1, &col);  
  87.   
  88.     col.cx = 100;  
  89.     col.iSubItem = 0;  
  90.     col.pszText = L"ThiTest";  
  91.     ListView_InsertColumn(hListView, 2, &col);  
  92.   
  93.     InsertListviewItem(0, L"1", L"test", L"TEST");  
  94.     InsertListviewItem(1, L"2", L"test", L"TEST");  
  95.     InsertListviewItem(2, L"3", L"test", L"TEST");  
  96.     InsertListviewItem(3, L"4", L"test", L"TEST");  
  97.     InsertListviewItem(4, L"5", L"test", L"TEST");  
  98.     InsertListviewItem(5, L"6", L"test", L"TEST");  
  99.     InsertListviewItem(6, L"7", L"test", L"TEST");  
  100.     InsertListviewItem(7, L"8", L"test", L"TEST");    
  101. }  
  102.   
  103. void InsertListviewItem(int nPos, WCHAR * wszFirst,WCHAR * wszSecond,WCHAR * wszThrid)  
  104. {  
  105.     LVITEM lvi;  
  106.     lvi.mask = LVIF_TEXT | LVIF_IMAGE;  
  107.     lvi.iImage = 0;  
  108.     lvi.pszText = L"";  
  109.     lvi.cchTextMax = 1024;  
  110.   
  111.     lvi.iItem = nPos;  
  112.     lvi.iSubItem = 0;     
  113.     ListView_InsertItem(hListView, &lvi);  
  114.     ListView_SetItemText(hListView, nPos, 0, wszFirst);  
  115.     lvi.iSubItem = 1;     
  116.     ListView_SetItemText(hListView, nPos, 1, wszSecond);  
  117.     lvi.iSubItem = 2;     
  118.     ListView_SetItemText(hListView, nPos, 2, wszThrid);  
  119.     return;  
  120. }  

程序运行截图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值