📄 listview.c
字号:
***********************************************************************/
HWND CreateListView (HINSTANCE hInstance, HWND hwndParent)
{
DWORD dwStyle; // Window style of the list view control
HWND hwndListView; // Handle to the list view control
HIMAGELIST himlSmall; // Handle to the small image list
HIMAGELIST himlLarge; // Handle to the large image list
INITCOMMONCONTROLSEX iccex; // INITCOMMONCONTROLSEX structure
// Initialize the INITCOMMONCONTROLSEX structure.
iccex.dwSize = sizeof (INITCOMMONCONTROLSEX);
// Load the list view and header control classes.
iccex.dwICC = ICC_LISTVIEW_CLASSES;
// Register the list view control and header classes from the common
// control dynamic-link library (DLL).
InitCommonControlsEx (&iccex);
// Assign the list view window style.
dwStyle = WS_TABSTOP | WS_CHILD | WS_BORDER | WS_VISIBLE |
LVS_AUTOARRANGE | LVS_REPORT | LVS_OWNERDATA;
// Create the list view control.
hwndListView = CreateWindowEx (
WS_EX_CLIENTEDGE, // Extended window style
WC_LISTVIEW, // Class name
TEXT(""), // Window name
dwStyle, // Window style
0, // Horizontal position of the window
0, // Vertical position of the window
0, // Window width
0, // Window height
hwndParent, // Handle to the parent window
(HMENU)ID_LISTVIEW, // Handle to the menu identifier
g_hInst, // Handle to the application instance
NULL); // Window-creation data
// If it fails in creating the window, return NULL.
if (!hwndListView)
return NULL;
// Resize the list view window.
ResizeListView (hwndListView, hwndParent);
// Create the large and small image lists.
himlSmall = ImageList_Create (16, 16, ILC_COLOR | ILC_MASK, 1, 0);
himlLarge = ImageList_Create (32, 32, ILC_COLOR | ILC_MASK, 1, 0);
if (himlSmall && himlLarge)
{
HICON hIcon;
// Load the small icon from the instance.
hIcon = LoadImage (g_hInst, MAKEINTRESOURCE(IDI_DISK), IMAGE_ICON,
16, 16, LR_DEFAULTCOLOR);
// Add the icon to the image list.
ImageList_AddIcon (himlSmall, hIcon);
// Load the small icon from the instance.
hIcon = LoadIcon (g_hInst, MAKEINTRESOURCE(IDI_DISK));
// Add the icon to the image list.
ImageList_AddIcon (himlLarge, hIcon);
// Assign the large and small image lists to the list view control.
ListView_SetImageList (hwndListView, himlSmall, LVSIL_SMALL);
ListView_SetImageList (hwndListView, himlLarge, LVSIL_NORMAL);
}
return hwndListView;
}
/***********************************************************************
FUNCTION:
InitListView
PURPOSE:
Initilaizes the list view control.
***********************************************************************/
BOOL InitListView (HWND hwndListView)
{
int index;
LV_COLUMN lvColumn;
TCHAR szString[5][20] = {TEXT("Main Column"),
TEXT("Column 1"),
TEXT("Column 2"),
TEXT("Column 3"),
TEXT("Column 4")};
// Empty the list in list view.
ListView_DeleteAllItems (hwndListView);
// Initialize the columns in the list view.
lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = 120;
// Insert the five columns in the list view.
for (index = 0; index < 5; index++)
{
lvColumn.pszText = szString[index];
ListView_InsertColumn (hwndListView, index, &lvColumn);
}
// Set the number of items in the list to ITEM_COUNT.
ListView_SetItemCount (hwndListView, ITEM_COUNT);
return TRUE;
}
/***********************************************************************
FUNCTION:
ResizeListView
PURPOSE:
Resizes the list view control.
***********************************************************************/
void ResizeListView (HWND hwndListView, HWND hwndParent)
{
RECT rect;
GetClientRect (hwndParent, &rect);
MoveWindow (hwndListView,
rect.left,
rect.top + CommandBar_Height (g_hwndCB),
rect.right - rect.left,
rect.bottom - rect.top - CommandBar_Height (g_hwndCB),
TRUE);
}
/***********************************************************************
FUNCTION:
ListViewNotify
PURPOSE:
Handles the WM_NOTIFY messages.
***********************************************************************/
LRESULT ListViewNotify (HWND hwnd, LPARAM lParam)
{
LPNMHDR lpnmh = (LPNMHDR) lParam; // Contains information about the
// notification message
HWND hwndListView = GetDlgItem (hwnd, ID_LISTVIEW);
// Handle of the list view control
switch (lpnmh->code)
{
case LVN_GETDISPINFO:
{
TCHAR szString[MAX_PATH];
LV_DISPINFO *lpdi = (LV_DISPINFO *) lParam;
// The message LVN_GETDISPINFO is sent by the list view control to
// its parent window. It is a request for the parent window to
// provide the information needed to display or sort a list view
// item.
if (lpdi->item.iSubItem)
{
if (lpdi->item.mask & LVIF_TEXT)
{
wsprintf (szString, TEXT("Item %d - Column %d"),
lpdi->item.iItem + 1, lpdi->item.iSubItem);
lstrcpy (lpdi->item.pszText, szString);
}
}
else
{
if (lpdi->item.mask & LVIF_TEXT)
{
wsprintf (szString, TEXT("Item %d"), lpdi->item.iItem + 1);
lstrcpy (lpdi->item.pszText, szString);
}
if (lpdi->item.mask & LVIF_IMAGE)
lpdi->item.iImage = 0;
}
return 0;
}
case LVN_ODCACHEHINT:
{
LPNMLVCACHEHINT lpCacheHint = (LPNMLVCACHEHINT)lParam;
// The message LVN_ODCACHEHINT is sent by the list view control
// when the contents of its display area have changed. For
// example, a list view control sends this notification when the
// user scrolls the control's display.
return 0;
}
case LVN_ODFINDITEM:
{
LPNMLVFINDITEM lpFindItem = (LPNMLVFINDITEM)lParam;
// The message LVN_ODFINDITEM is sent by the list view control
// when it needs the owner to find a particular callback item.
// Return -1 if the item is not found.
return 0;
}
}
return 0;
}
/***********************************************************************
FUNCTION:
SwitchView
PURPOSE:
Switches the window style of the list view control.
***********************************************************************/
void SwitchView (HWND hwndListView, DWORD dwView)
{
DWORD dwStyle = GetWindowLong (hwndListView, GWL_STYLE);
SetWindowLong (hwndListView, GWL_STYLE,
(dwStyle & ~LVS_TYPEMASK) | dwView);
ResizeListView (hwndListView, GetParent (hwndListView));
}
/***********************************************************************
FUNCTION:
UpdateMenu
PURPOSE:
Checks and unchecks the menu items.
***********************************************************************/
void UpdateMenu (HWND hwndListView, HMENU hMenu)
{
UINT uID;
DWORD dwStyle;
// Uncheck all of the menu items.
CheckMenuItem (hMenu, IDM_LARGE_ICONS, MF_BYCOMMAND | MF_UNCHECKED);
CheckMenuItem (hMenu, IDM_SMALL_ICONS, MF_BYCOMMAND | MF_UNCHECKED);
CheckMenuItem (hMenu, IDM_LIST, MF_BYCOMMAND | MF_UNCHECKED);
CheckMenuItem (hMenu, IDM_REPORT, MF_BYCOMMAND | MF_UNCHECKED);
// Check the appropriate view menu item.
dwStyle = GetWindowLong (hwndListView, GWL_STYLE);
switch (dwStyle & LVS_TYPEMASK)
{
case LVS_ICON:
uID = IDM_LARGE_ICONS;
break;
case LVS_SMALLICON:
uID = IDM_SMALL_ICONS;
break;
case LVS_LIST:
uID = IDM_LIST;
break;
case LVS_REPORT:
uID = IDM_REPORT;
break;
}
CheckMenuRadioItem (hMenu, IDM_LARGE_ICONS, IDM_REPORT, uID,
MF_BYCOMMAND | MF_CHECKED);
}
// END OF LISTVIEW.C
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -