Creating a List View Control// CreateListView - creates a list view control.
// Returns the handle of the new control if successful or NULL
// otherwise.
// hwndParent - handle of the control's parent window
// pfData - file containing list view items
HWND WINAPI CreateListView(HWND hwndParent, FILE *pfData)
{
HWND hwndLV;
// Force the common controls DLL to be loaded.
InitCommonControls();
// Create the list view window.
hwndLV = CreateWindow(WC_LISTVIEW, "", WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
hwndParent, NULL, g_hinst, NULL);
if (hwndLV == NULL)
return NULL;
// Call application-defined functions to initialize the
// image lists, add columns, and add some items.
if (!InitListViewImageLists(hwndLV) ||
!InitListViewColumns(hwndLV) ||
!InitListViewItems(hwndLV, pfData)) {
DestroyWindow(hwndLV); return FALSE;
}
return hwndLV; // return the control's handle
} Initializing the Image Lists for a List View Control// InitListViewImageList - creates image lists for a list view.
// Returns TRUE if successful or FALSE otherwise.
// hwndLV - handle of the list view control
BOOL WINAPI InitListViewImageLists(HWND hwndLV)
{
HICON hiconItem; // icon for list view items
HIMAGELIST himlLarge; // image list for icon view
HIMAGELIST himlSmall; // image list for other views
// Create the full-sized and small icon image lists.
himlLarge = ImageList_Create(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), TRUE, 1, 1);
himlSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON), TRUE, 1, 1);
// Add an icon to each image list.
hiconItem = LoadIcon(g_hinst, MAKEINTRESOURCE(IDI_ITEM));
ImageList_AddIcon(himlLarge, hiconItem);
ImageList_AddIcon(himlSmall, hiconItem);
DeleteObject(hiconItem);
// Assign the image lists to the list view control.
ListView_SetImageList(hwndLV, himlLarge, LVSIL_NORMAL); ListView_SetImageList(hwndLV, himlSmall, LVSIL_SMALL);
return TRUE;
} Adding Columns to a List View Control// InitListViewColumns - adds columns to a list view control.
// Returns TRUE if successful or FALSE otherwise.
// hwndLV - handle of the list view control
BOOL WINAPI InitListViewColumns(HWND hwndLV)
{
extern char g_achTemp[256]; // temporary buffer
LV_COLUMN lvc;
int iCol;
// Initialize the LV_COLUMN structure.
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvc.fmt = LVCFMT_LEFT;
lvc.cx = 100;
lvc.pszText = g_achTemp;
// Add the columns.
for (iCol = 0; iCol < C_COLUMNS; iCol++) {
lvc.iSubItem = iCol;
LoadString(g_hinst, IDS_FIRSTCOLUMN + iCol,
g_achTemp, sizeof(g_achTemp));
if (ListView_InsertColumn(hwndLV, iCol, &lvc) == -1)
return FALSE;
}
return TRUE;
} Adding Items to a List View Control#define C_COLUMNS 6
typedef struct myitem_tag {
LPSTR aCols[C_COLUMNS];
} MYITEM; // InitListViewItems - adds items and subitems to a list view.
// Returns TRUE if successful or FALSE otherwise.
// hwndLV - handle of the list view control
// pfData - text file containing list view items with columns
// separated by semicolons
BOOL WINAPI InitListViewItems(HWND hwndLV, FILE *pfData)
{
extern char g_achTemp[256]; // temporary buffer
PSTR pszStart;
PSTR pszEnd;
int iItem;
int iSubItem;
LV_ITEM lvi;
// Initialize LV_ITEM members that are common to all items.
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
lvi.state = 0;
lvi.stateMask = 0;
lvi.pszText = LPSTR_TEXTCALLBACK; // app. maintains text
lvi.iImage = 0; // image list index
// Read each line in the specified file.
for (iItem = 0;
fgets(g_achTemp, sizeof(g_achTemp), pfData);
iItem++) {
// Allocate an application-defined structure to store the // item label and the text of each subitem.
MYITEM *pItem = LocalAlloc(LPTR, sizeof(MYITEM));
// Copy the first string (the label).
pszEnd = strchr(g_achTemp, ';');
*pszEnd = '\0';
pItem->aCols[0] = DupString(g_achTemp);
// Copy subsequent strings (subitems).
for (iSubItem = 1;
iSubItem < C_COLUMNS && pszEnd != NULL;
iSubItem++) {
pszStart = pszEnd + 1; if ((pszEnd = strchr(pszStart, ';')) != NULL)
*pszEnd = '\0';
pItem->aCols[iSubItem] = DupString(pszStart);
}
// Initialize item-specific LV_ITEM members.
lvi.iItem = iItem;
lvi.iSubItem = 0;
lvi.lParam = (LPARAM) pItem; // item data
// Add the item.
ListView_InsertItem(hwndLV, &lvi);
// There is no need to set the text of the subitems. They // default to LPSTR_TEXTCALLBACK.
}
return TRUE;
}
// DupString - allocates a copy of a string.
// lpsz - address of the null-terminated string to copy
LPSTR DupString(LPSTR lpsz)
{
int cb = lstrlen(lpsz) + 1;
LPSTR lpszNew = LocalAlloc(LMEM_FIXED, cb);
if (lpszNew != NULL)
CopyMemory(lpszNew, lpsz, cb);
return lpszNew;
}
// Returns the handle of the new control if successful or NULL
// otherwise.
// hwndParent - handle of the control's parent window
// pfData - file containing list view items
HWND WINAPI CreateListView(HWND hwndParent, FILE *pfData)
{
HWND hwndLV;
// Force the common controls DLL to be loaded.
InitCommonControls();
// Create the list view window.
hwndLV = CreateWindow(WC_LISTVIEW, "", WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
hwndParent, NULL, g_hinst, NULL);
if (hwndLV == NULL)
return NULL;
// Call application-defined functions to initialize the
// image lists, add columns, and add some items.
if (!InitListViewImageLists(hwndLV) ||
!InitListViewColumns(hwndLV) ||
!InitListViewItems(hwndLV, pfData)) {
DestroyWindow(hwndLV); return FALSE;
}
return hwndLV; // return the control's handle
} Initializing the Image Lists for a List View Control// InitListViewImageList - creates image lists for a list view.
// Returns TRUE if successful or FALSE otherwise.
// hwndLV - handle of the list view control
BOOL WINAPI InitListViewImageLists(HWND hwndLV)
{
HICON hiconItem; // icon for list view items
HIMAGELIST himlLarge; // image list for icon view
HIMAGELIST himlSmall; // image list for other views
// Create the full-sized and small icon image lists.
himlLarge = ImageList_Create(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), TRUE, 1, 1);
himlSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON), TRUE, 1, 1);
// Add an icon to each image list.
hiconItem = LoadIcon(g_hinst, MAKEINTRESOURCE(IDI_ITEM));
ImageList_AddIcon(himlLarge, hiconItem);
ImageList_AddIcon(himlSmall, hiconItem);
DeleteObject(hiconItem);
// Assign the image lists to the list view control.
ListView_SetImageList(hwndLV, himlLarge, LVSIL_NORMAL); ListView_SetImageList(hwndLV, himlSmall, LVSIL_SMALL);
return TRUE;
} Adding Columns to a List View Control// InitListViewColumns - adds columns to a list view control.
// Returns TRUE if successful or FALSE otherwise.
// hwndLV - handle of the list view control
BOOL WINAPI InitListViewColumns(HWND hwndLV)
{
extern char g_achTemp[256]; // temporary buffer
LV_COLUMN lvc;
int iCol;
// Initialize the LV_COLUMN structure.
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvc.fmt = LVCFMT_LEFT;
lvc.cx = 100;
lvc.pszText = g_achTemp;
// Add the columns.
for (iCol = 0; iCol < C_COLUMNS; iCol++) {
lvc.iSubItem = iCol;
LoadString(g_hinst, IDS_FIRSTCOLUMN + iCol,
g_achTemp, sizeof(g_achTemp));
if (ListView_InsertColumn(hwndLV, iCol, &lvc) == -1)
return FALSE;
}
return TRUE;
} Adding Items to a List View Control#define C_COLUMNS 6
typedef struct myitem_tag {
LPSTR aCols[C_COLUMNS];
} MYITEM; // InitListViewItems - adds items and subitems to a list view.
// Returns TRUE if successful or FALSE otherwise.
// hwndLV - handle of the list view control
// pfData - text file containing list view items with columns
// separated by semicolons
BOOL WINAPI InitListViewItems(HWND hwndLV, FILE *pfData)
{
extern char g_achTemp[256]; // temporary buffer
PSTR pszStart;
PSTR pszEnd;
int iItem;
int iSubItem;
LV_ITEM lvi;
// Initialize LV_ITEM members that are common to all items.
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
lvi.state = 0;
lvi.stateMask = 0;
lvi.pszText = LPSTR_TEXTCALLBACK; // app. maintains text
lvi.iImage = 0; // image list index
// Read each line in the specified file.
for (iItem = 0;
fgets(g_achTemp, sizeof(g_achTemp), pfData);
iItem++) {
// Allocate an application-defined structure to store the // item label and the text of each subitem.
MYITEM *pItem = LocalAlloc(LPTR, sizeof(MYITEM));
// Copy the first string (the label).
pszEnd = strchr(g_achTemp, ';');
*pszEnd = '\0';
pItem->aCols[0] = DupString(g_achTemp);
// Copy subsequent strings (subitems).
for (iSubItem = 1;
iSubItem < C_COLUMNS && pszEnd != NULL;
iSubItem++) {
pszStart = pszEnd + 1; if ((pszEnd = strchr(pszStart, ';')) != NULL)
*pszEnd = '\0';
pItem->aCols[iSubItem] = DupString(pszStart);
}
// Initialize item-specific LV_ITEM members.
lvi.iItem = iItem;
lvi.iSubItem = 0;
lvi.lParam = (LPARAM) pItem; // item data
// Add the item.
ListView_InsertItem(hwndLV, &lvi);
// There is no need to set the text of the subitems. They // default to LPSTR_TEXTCALLBACK.
}
return TRUE;
}
// DupString - allocates a copy of a string.
// lpsz - address of the null-terminated string to copy
LPSTR DupString(LPSTR lpsz)
{
int cb = lstrlen(lpsz) + 1;
LPSTR lpszNew = LocalAlloc(LMEM_FIXED, cb);
if (lpszNew != NULL)
CopyMemory(lpszNew, lpsz, cb);
return lpszNew;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货