一派護法 十九級 |
核心代码备份: 【头文件 Dlg1.h】 void Dlg1_Show(HINSTANCE hInst, HWND hWnd); INT_PTR CALLBACK Dlg1(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); void Dlg1_Init(HWND hDlg); void Dlg1_UpdateView(int view); 【源文件 Dlg1.cpp】 #include "stdafx.h" #include <Windows.h> #include <Commctrl.h> #include "resource.h" #include "Dlg1.h"
struct { HWND hListBox; HWND hRadio1; HWND hRadio2; HWND hRadio3; HWND hRadio4; HWND hRadio5; } ctrls;
void Dlg1_Show(HINSTANCE hInst, HWND hWnd) { DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, Dlg1); }
INT_PTR CALLBACK Dlg1(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: Dlg1_Init(hDlg); return (INT_PTR)TRUE; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = LOWORD(wParam); switch (wmId) { case IDOK: case IDCANCEL: EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; case IDC_RADIO1: Dlg1_UpdateView(LV_VIEW_ICON); break; case IDC_RADIO2: Dlg1_UpdateView(LV_VIEW_SMALLICON); break; case IDC_RADIO3: Dlg1_UpdateView(LV_VIEW_LIST); break; case IDC_RADIO4: Dlg1_UpdateView(LV_VIEW_DETAILS); break; case IDC_RADIO5: Dlg1_UpdateView(LV_VIEW_TILE); break; } break; } return (INT_PTR)FALSE; }
void Dlg1_Init(HWND hDlg) { LV_COLUMN lvc; LV_ITEM lvi; HINSTANCE hShellInst; int i, len; HIMAGELIST hImgListSm, hImgListLg; HICON hIcon;
SIZE size = {200, 50}; LVTILEVIEWINFO tileViewInfo;
LPWSTR columnNames[] = {L"Name", L"Price", L"Number", L"Available"}; const int columnSizes[] = {230, 80, 80, 80}; LPWSTR itemNames[] = {L"Computer", L"Printer", L"Trash", L"Magnifier", L"Lock", L"Hard Drive", L"Scissors", L"Floppy", L"Picture Frame", L"Noteboard", L"Fax", L"Tray", L"Camera", L"CD", L"TF Card"}; const int itemImageIds[] = {16, 17, 32, 23, 48, 166, 16762, 16761, 63001, 16763, 1005, 327, 309, 302, 307};
ctrls.hListBox = GetDlgItem(hDlg, IDC_LIST1); ctrls.hRadio1 = GetDlgItem(hDlg, IDC_RADIO1); ctrls.hRadio2 = GetDlgItem(hDlg, IDC_RADIO2); ctrls.hRadio3 = GetDlgItem(hDlg, IDC_RADIO3); ctrls.hRadio4 = GetDlgItem(hDlg, IDC_RADIO4); ctrls.hRadio5 = GetDlgItem(hDlg, IDC_RADIO5);
// Init view tileViewInfo.cbSize = sizeof(tileViewInfo); tileViewInfo.dwFlags = LVTVIF_FIXEDSIZE; tileViewInfo.dwMask = LVTVIM_COLUMNS | LVTVIM_TILESIZE; tileViewInfo.cLines = 2; // Maximum number of text lines in each item label, not counting the title tileViewInfo.sizeTile = size; // Size of an individual tile ListView_SetTileViewInfo(ctrls.hListBox, &tileViewInfo); SendMessage(ctrls.hRadio5, BM_SETCHECK, BST_CHECKED, NULL); Dlg1_UpdateView(LV_VIEW_TILE);
// Add columns lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM; len = sizeof(columnSizes) / sizeof(int); for (i = 0; i < len; i++) { lvc.iSubItem = i; lvc.pszText = columnNames[i]; lvc.cx = columnSizes[i]; lvc.fmt = LVCFMT_LEFT; ListView_InsertColumn(ctrls.hListBox, i, &lvc); }
// Create ImageList hImgListSm = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_COLOR32, 1, 0); hImgListLg = ImageList_Create(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), ILC_COLOR32, 1, 0); hShellInst = GetModuleHandle(L"shell32.dll"); len = sizeof(itemImageIds) / sizeof(int); for (i = 0; i < len; i++) { hIcon = LoadIcon(hShellInst, MAKEINTRESOURCE(itemImageIds[i])); ImageList_AddIcon(hImgListLg, hIcon); ImageList_AddIcon(hImgListSm, hIcon); DestroyIcon(hIcon); } ListView_SetImageList(ctrls.hListBox, hImgListLg, LVSIL_NORMAL); ListView_SetImageList(ctrls.hListBox, hImgListSm, LVSIL_SMALL);
// Add items lvi.mask = LVIF_IMAGE | LVIF_TEXT; lvi.iSubItem = 0; for (i = 0; i < len; i++) { lvi.pszText = itemNames[i]; lvi.iImage = i; lvi.iItem = i; ListView_InsertItem(ctrls.hListBox, &lvi); } }
void Dlg1_UpdateView(int view) { ListView_SetView(ctrls.hListBox, view); } 【主cpp源文件(部分)】 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc;
LPCWSTR welcomeStr = L"Welcome to Arslanbar!"; LPCWSTR str2 = L"Please double-click to open the dialog...";
switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; case ID_FILE_SHOWITEMS: case ID_ACCELERATOR32772: Dlg1_Show(hInst, hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... TextOut(hdc, 4, 4, welcomeStr, wcslen(welcomeStr)); TextOut(hdc, 4, 20, str2, wcslen(str2)); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_LBUTTONDBLCLK: Dlg1_Show(hInst, hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
|