1 #win32sdk编程积累经验# 2 3 ## ListView ## 4 - 创建imagelist 5 6 HIMAGELIST hi; 7 HBITMAP hBmp = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BMP_PC)); 8 hi =ImageList_Create(32,32,ILC_COLOR32|ILC_MASK,1,1); 9 ImageList_AddMasked(hi,hBmp,RGB(0,0,0)); 10 11 12 13 - 创建 listveiw并设置风格 14 15 DWORD dwStyle =WS_BORDER|LVS_ALIGNTOP| LVS_REPORT | LVS_SINGLESEL |WS_VISIBLE |WS_CHILD |LVS_SHOWSELALWAYS;//LVS_OWNERDRAWFIXED 16 17 hMAINHostList = CreateWindowEx(WS_EX_WINDOWEDGE, WC_LISTVIEW,NULL,dwStyle,0,0,0,0,hwnd,(HMENU)IDC_LISTVIEW_HOSTLIST,hInst,NULL); 18 19 SetTheme(hMAINHostList, L"ListView", L"Explorer", NULL); 20 21 ListView_SetExtendedListViewStyleEx(hMAINHostList,LVS_EX_FULLROWSELECT,LVS_EX_FULLROWSELECT); 22 - 插入列 23 24 static WCHAR* wszArryHostListColNames[] = {L"col1",L"col2",...}; 25 LV_COLUMN lvColumn; 26 lvColumn.mask = LVCF_FMT|LVCF_WIDTH|LVCF_TEXT ; 27 lvColumn.fmt = LVCFMT_LEFT; 28 lvColumn.cx = 150 ; 29 for (int i=0;i < 8;i++) 30 { 31 lvColumn.pszText = wszArryHostListColNames[i]; 32 SendMessage(hMAINHostList,LVM_INSERTCOLUMN,(WPARAM)i,(LPARAM)&lvColumn); 33 } 34 // 设置imagelist 35 ListView_SetImageList(hMAINHostList,hi,LVSIL_STATE); 36 - 插入行,并设置item 37 38 LVITEM lvItem={0}; 39 lvItem.mask = LVIF_TEXT |LVIF_PARAM |LVIF_STATE; 40 lvItem.iItem = 0; 41 //lvItem.iSubItem = 0; 42 lvItem.lParam = (LPARAM)lparam; 43 lvItem.state = INDEXTOSTATEIMAGEMASK(1); // 图标 44 lvItem.pszText = szText ; 45 46 // col 0 47 row = ListView_InsertItem(hListView,&lvItem); 48 // SetItemData (mask = LVIF_PARAM ; lvItem.lParam = (LPARAM)lparam;) 49 ListView_SetItem(hM4sterHostList,&lvItem); 50 // col 1 51 ListView_SetItemText(hListView,row,1,szText); 52 53 - 消息事件 54 55 LRESULT OnNotify(HWND hwnd,int idfrm,NMHDR* pnmhdr) 56 { 57 if (idfrm == IDC_LISTVIEW_HOSTLIST ) 58 { 59 LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pnmhdr); 60 iHostListSelId = pNMItemActivate->iItem ; 61 62 if ( pnmhdr->code == NM_RCLICK ) 63 { 64 65 if (pNMItemActivate && pNMItemActivate->iItem >-1) 66 { 67 POINT pt ={0}; 68 GetCursorPos(&pt); 69 HMENU hMenu = GetSubMenu(LoadMenu(hInst,MAKEINTRESOURCE(IDR_MENU_POP)),0); 70 71 TrackPopupMenu(hMenu,0,pt.x,pt.y,0,hwnd,NULL); 72 73 } 74 75 }else if (pnmhdr->code == NM_DBLCLK ) 76 { 77 78 } 79 } 80 81 return false; 82 } 83 - 其他 84 85 86 ## status bar ## 87 - 创建控件 88 89 static int iArryStatusBarParts[] = {150 ,250}; 90 // 1. crate bar 91 hMAINStatusBar = CreateStatusWindow(WS_BORDER|WS_CHILD|WS_VISIBLE|CCS_BOTTOM|CCS_ADJUSTABLE,L"",hwnd,IDC_STATUSBAR_MAIN); 92 // 2. set parts 93 SendMessage(hMAINStatusBar,SB_SETPARTS,sizeof(iArryStatusBarParts),(LPARAM)iArryStatusBarParts); 94 95 //SendMessage(hMAINStatusBar,SB_SETICON,0,(LPARAM)LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON_RED16))); 96 // 3. set text and icon 97 98 SendMessage(hMAINStatusBar,SB_SETTEXT,0,(LPARAM)L"1970/01/01 00:00:00 "); 99 SendMessage(hMAINStatusBar,SB_SETTEXT,1,(LPARAM)L" "); 100 101 ## treeview ## 102 - 创建控件 103 104 // 创建 imaglist 105 HIMAGELIST hi; 106 hi =ImageList_Create(24,24,ILC_COLOR24|ILC_MASK,1,13); 107 int ret ; 108 HBITMAP hBmp = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BMP_INFO24)); 109 ret =ImageList_AddMasked(hi,hBmp,RGB(0,0,0)); 110 // 添加图标 111 hBmp = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BMP_SHELL24)); 112 ret =ImageList_AddMasked(hi,hBmp,RGB(0,0,0)); 113 hBmp = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BMP_FILE24)); 114 ret =ImageList_AddMasked(hi,hBmp,RGB(0,0,0)); 115 116 117 DWORD dwStryle = WS_VISIBLE | WS_CHILD | TVS_HASLINES|TVS_SHOWSELALWAYS/*|TVS_LINESATROOT*/;// remove root-line style 118 HWND hTree = CreateWindow(WC_TREEVIEW,L"Control Station tree",dwStryle,0,0,0,0,hParent,(HMENU)IDC_CS_TREE,hInst,NULL); 119 if (hTree) 120 { 121 122 TreeView_SetImageList(hTree,hi,TVSIL_NORMAL); 123 124 WCHAR* lpwszTreeNames[] = {L"tree1",L"tree2",L"tree3"}; 125 126 TVINSERTSTRUCT ti = {0}; 127 128 ti.item.mask=TVIF_TEXT|TCIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_PARAM; 129 ti.hParent=NULL; 130 ti.hInsertAfter=TVI_ROOT; 131 132 for (int i=0;i<3;i++) 133 { 134 ti.item.lParam = i; 135 ti.item.iSelectedImage= ti.item.iImage=i; 136 137 ti.item.pszText=lpwszTreeNames[i]; 138 TreeView_InsertItem(hTree,&ti); 139 } 140 } 141 - 事件消息 142 143 LRESULT OnNotify(HWND hwnd,int idfrm,NMHDR* pnmhdr) 144 { 145 146 if (idfrm == IDC_CS_TREE) 147 { 148 if ( pnmhdr->code == TVN_SELCHANGED ) 149 { 150 // 处理 151 152 } 153 154 } 155 156 return FALSE ; 157 } 158 159 - 其他 160 161 // 获取treeveiw当前选中的节点 162 HTREEITEM hSelected ; 163 hSelected = TreeView_GetNextItem(hTree,hSelected,TVGN_CARET); 164 // 获取 treeview的item的 Text\lparam 165 WCHAR szText[max_path]; 166 TVITEM tvi = {0}; 167 tvi.mask = TVIF_TEXT |TVIF_PARAM;// GetTreeItemText \ GetTreeItemData 都可以通过mask指定获取 168 tvi.pszText = szText; 169 tvi.cchTextMax = MAX_PATH; 170 tvi.hItem = hSelected; 171 TreeView_GetItem(hTree,&tvi); 172 173 174 // treeview has childrens 判断是否有子节点(未测试) 175 TV_ITEM tvitem = {0}; 176 tvitem.mask = TVIF_CHILDREN; 177 tvitem.hItem = hSel ; 178 TreeView_GetItem(hDirTree,&tvitem); 179 if (tvitem.cChildren > 0 ) 180 { 181 182 } 183 184 // 删除TREEVIEW的子节点 185 VOID RemoveTreeviewSubItems(HWND hTree, HTREEITEM hItem) 186 { 187 HTREEITEM hSubs = TreeView_GetChild(hTree,hItem); 188 HTREEITEM hSub= 0; 189 190 while (hSubs) 191 { 192 hSub = hSubs ; 193 hSubs = TreeView_GetNextSibling(hTree,hSubs); 194 TreeView_DeleteItem(hTree,hSub); 195 } 196 197 } 198 199 // 展开 Treeview节点 200 TreeView_Expand(lpcs->hDirTree,hTreeItem,TVE_EXPAND); 201 202 203 - win32 Treeview 值得参看下 http://blog.csdn.net/c395565746c/article/details/5679357 204 - 加上[+] [-]图标 TVS_HASBUTTONS http://blog.csdn.net/lifu119/article/details/7024268 205 206 ## 托盘图标 ## 207 - 创建 208 209 // GUID gidTrayNotify = {0}; 这2个都是全局的 210 // static NOTIFYICONDATA nid; 211 212 gidTrayNotify.Data1 = 0xabcd; 213 gidTrayNotify.Data2 = 0x1234; 214 gidTrayNotify.Data3 = 0x2345; 215 memset(gidTrayNotify.Data4,0xab,8); 216 217 218 219 hMAINTrayMenu = LoadMenu(hInst,MAKEINTRESOURCE(IDR_MENU_TRAY)); 220 hMAINTrayMenu = GetSubMenu(hMAINTrayMenu,0); 221 222 nid.cbSize = sizeof(NOTIFYICONDATA); 223 nid.hWnd = hwnd ; 224 225 nid.uID = IDC_TRAYNOTIFY ; 226 nid.uFlags = NIF_MESSAGE | NIF_ICON |NIF_TIP |NIF_INFO |NIF_GUID; 227 nid.guidItem = gidTrayNotify ; 228 nid.hBalloonIcon= nid.hIcon = LoadIcon(hInst,MAKEINTRESOURCE(IDI_MAIN)); 229 nid.uCallbackMessage = WMU_NOTIFY ; 230 static WCHAR lpwszNotifyTip[] = L"MAIN v1.0"; 231 wcscpy_s(nid.szTip,lpwszNotifyTip);; 232 233 nid.uTimeout = 3000; 234 nid.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON; 235 //nid.uVersion = NOTIFYICON_VERSION_4 ; 236 Shell_NotifyIcon(NIM_ADD,&nid); 237 - 修改状态 238 239 // 这里可以删减的 自行测试删减代码 240 // 弹气泡 或者 只修改tip信息,需要自行改动 241 nid.cbSize = sizeof(NOTIFYICONDATA); 242 243 nid.uFlags = NIF_MESSAGE | NIF_ICON |NIF_TIP |NIF_INFO |NIF_GUID; 244 nid.guidItem = gidTrayNotify ; 245 nid.hBalloonIcon= nid.hIcon = LoadIcon(hInst,MAKEINTRESOURCE(IDI_MAIN)); 246 nid.uCallbackMessage = WMU_NOTIFY ; 247 wcscpy_s(nid.szInfoTitle,L"MAIN Msg "); 248 wcscpy_s(nid.szInfo,szBuffer); 249 static WCHAR lpwszNotifyTip[] = L"MAIN v1.0"; 250 wcscpy_s(nid.szTip,lpwszNotifyTip);; 251 nid.uTimeout = 1000; 252 nid.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON; 253 254 Shell_NotifyIcon(NIM_MODIFY,&nid); 255 256 - 删除 257 258 nid.uFlags = NIF_GUID ; 259 nid.guidItem = gidTrayNotify ; 260 Shell_NotifyIcon(NIM_DELETE,&nid); 261 262 - 消息事件 263 264 // 响应 自定义消息 265 case WMU_NOTIFY: // custom msg : traynotify 266 { 267 MAIN_OnTrayNotify(hWnd,lParam); 268 } 269 break; 270 271 // 处理过程 272 void MAIN_OnTrayNotify(HWND hWnd, LPARAM lParam) 273 { 274 static POINT pos; 275 switch(lParam) 276 { 277 case WM_RBUTTONDOWN: 278 { 279 GetCursorPos(&pos); 280 SetForegroundWindow(hWnd); 281 TrackPopupMenu(hMAINTrayMenu,TPM_RIGHTBUTTON,pos.x,pos.y,0,hWnd,NULL); 282 } 283 break; 284 case WM_LBUTTONDBLCLK: 285 { 286 ShowWindow(hWnd,SW_SHOW); 287 288 } 289 break; 290 291 } 292 } 293 294 295 296 ## 创建dialog的几种姿势## 297 - CreateDialogParam 不模态,带参数创建diaglog; 在OnInitDialog 中lparam就是传入的参数 298 - CreateDialog 不模态 299 - DialogBox 模态 300 - 以上都存在漂浮于主窗口的缺点,不过够用了 301 - FILETIME 2 SYSTEMTIME 2 LOCALTIME 302 303 BOOL FileTimeToSystemTime( 304 const FILETIME* lpFileTime, 305 LPSYSTEMTIME lpSystemTime); 306 FileTimeToSystemTime 307 LocalFileTimeToFileTime 308 FileTimeToLocalFileTime
1 ## 封装的一些win32小函数 ## 2 以下函数很多没有测试,只是给出思路。以后用到在改进 3 4 - CheckMenuItemX 设置菜单item上的勾选:当开发中来表示某些功能已经开启,通过设置勾选来提示用户 5 6 void CheckMenuItemX(HMENU hMenu,UINT id, bool bCheck) 7 { 8 CheckMenuItem(hMenu,id,MF_BYCOMMAND | bCheck ? MF_CHECKED : MF_UNCHECKED); 9 } 10 11 - LVSetItemData 给ListView项绑定上数据:在开发中我们给某项绑定上数据后,可以在选中此项后轻松获取此项的身份(data);以及自绘时,根据绑定上的数据绘制各个项。 12 13 void LVSetItemData(HWND hCtrl,int iItem,LPARAM lpData) 14 { 15 LVITEM lvit = {0} ; 16 lvit.mask = LVIF_PARAM ; 17 lvit.iItem = iItem ; 18 lvit.lParam = (LPARAM)lpData ; 19 20 ListView_SetItem(hCtrl,&lvit); 21 } 22 23 - LVGetItemData 和 LVSetItemData 对应的作用 24 25 LPARAM LVGetItemData(HWND hctrl,int iItem) 26 { 27 LVITEM lvit = {0} ; 28 lvit.mask = LVIF_PARAM ; 29 lvit.iItem = iItem ; 30 ListView_SetItem(hCtrl,&lvit); 31 return lvit.lParam ; 32 }
1 - listview 使用分组和缩进 2 3 //创建listview 4 HWND listview1 = CreateWindowEx(WS_EX_STATICEDGE, TEXT("SysListView32"), NULL, WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL, 10, 10, 400, 400, hwnd, (HMENU)1, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); 5 ListView_SetExtendedListViewStyle(listview1, LVS_EX_FULLROWSELECT | LVS_EX_SUBITEMIMAGES);//设置listview扩展风格 6 SendMessage(listview1, WM_SETFONT, (WPARAM)GetStockObject(17), 0); 7 8 SetWindowTheme(listview1, L"Explorer", NULL); 9 SendMessage(listview1, LVM_ENABLEGROUPVIEW, TRUE, 0);//启用分组支持 10 11 RECT rcThis; 12 GetClientRect(hwnd,&rcThis); 13 MoveWindow(listview1,0,0,rcThis.right,rcThis.bottom,TRUE); 14 15 16 LVGROUP group1; 17 group1.cbSize = LVGROUP_V5_SIZE; 18 group1.mask = LVGF_HEADER | LVGF_GROUPID; 19 group1.pszHeader = L"组1"; 20 group1.iGroupId = 0; 21 ListView_InsertGroup(listview1, -1, &group1); 22 23 group1.pszHeader = L"组2 - Hello World"; 24 group1.iGroupId = 1; 25 ListView_InsertGroup(listview1, -1, &group1); 26 27 //创建图片列表 28 HIMAGELIST imglist1 = ImageList_Create(16, 16, ILC_COLOR32, 0, 0); 29 ImageList_AddIcon(imglist1, LoadIcon((HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE), MAKEINTRESOURCE(IDI_ICON1))); 30 ImageList_AddIcon(imglist1, LoadIcon((HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE), MAKEINTRESOURCE(IDI_ICON2))); 31 HICON ico; 32 ExtractIconEx(L"C:\\Program Files\\Microsoft Visual Studio 14.0\\Common7\\Tools\\spyxx.exe", 0, &ico, NULL, 1); 33 ImageList_AddIcon(imglist1, ico); 34 ListView_SetImageList(listview1, imglist1, LVSIL_SMALL); 35 36 LV_COLUMN list1; 37 LVITEM item1 ; 38 //内存清零 39 ZeroMemory(&list1, sizeof(LVCOLUMN)); 40 ZeroMemory(&item1, sizeof(LVITEM)); 41 42 //创建列 43 list1.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;//掩码 44 list1.fmt = LVCFMT_LEFT;//左对齐 45 list1.cx = 100;//列宽 46 list1.pszText = L"列1"; 47 SendMessage(listview1, LVM_INSERTCOLUMN, 0, (LPARAM)&list1);//创建列 48 list1.pszText = L"list2"; 49 list1.cx = 200; 50 SendMessage(listview1, LVM_INSERTCOLUMN, 1, (LPARAM)&list1); 51 list1.pszText = L"hello world"; 52 SendMessage(listview1, LVM_INSERTCOLUMN, 2, (LPARAM)&list1); 53 list1.pszText = L"happy"; 54 SendMessage(listview1, LVM_INSERTCOLUMN, 3, (LPARAM)&list1); 55 56 //创建项目 57 item1.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_INDENT | LVIF_GROUPID; 58 item1.pszText = L"项目"; 59 item1.iItem = 0;//项目号 60 item1.iImage = 0;//图片号 61 item1.iIndent = 0; 62 item1.iGroupId = 0; 63 SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1); 64 item1.iItem = 1; 65 item1.iImage = 0; 66 SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1); 67 item1.pszText = L"item 3"; 68 item1.iItem = 2; 69 item1.iImage = 1; 70 item1.iIndent = 1;//缩进一个图像单位 71 SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1); 72 item1.pszText = L"第四个"; 73 item1.iItem = 3; 74 item1.iImage = 2; 75 item1.iIndent = 0; 76 SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1); 77 78 item1.iGroupId = 1; 79 item1.pszText = L"我在组2内"; 80 item1.iItem = 4; 81 item1.iImage = 1; 82 item1.iIndent = 0; 83 SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1); 84 item1.pszText = L"item 5"; 85 item1.iItem = 2; 86 item1.iImage = 0; 87 item1.iIndent = 1; 88 SendMessage(listview1, LVM_INSERTITEM, 0, (LPARAM)&item1); 89 90 //创建子项目 91 item1.mask = LVIF_TEXT | LVIF_IMAGE; 92 item1.iItem = 1; 93 item1.iSubItem = 1; 94 item1.iImage = -1; 95 item1.pszText = TEXT("子项目"); 96 SendMessage(listview1, LVM_SETITEM, 0, (LPARAM)&item1); 97 item1.iItem = 1; 98 item1.iSubItem = 2; 99 item1.pszText = TEXT("子项目"); 100 SendMessage(listview1, LVM_SETITEM, 0, (LPARAM)&item1); 101 item1.iItem = 1; 102 item1.iSubItem = 3; 103 item1.pszText = TEXT("子项目"); 104 SendMessage(listview1, LVM_SETITEM, 0, (LPARAM)&item1); 105 106 - 启用高版本的comctrl支持 107 108 #pragma comment(linker,"\"/manifestdependency:type='win32' \ 109 name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ 110 processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")