程序代码:
#include <windows.h>
#include <commctrl.h>
下面四行我加到commctrl.h里了
//#ifdef _WIN32_WINNT
//#undef _WIN32_WINNT
//#endif
//#define _WIN32_WINNT 0x0600
typedef struct LVGROUP {
UINT cbSize;
UINT mask;
LPWSTR pszHeader;
int cchHeader;
LPWSTR pszFooter;
int cchFooter;
int iGroupId;
UINT stateMask;
UINT state;
UINT uAlign;
#if _WIN32_WINNT >= 0x0600
LPWSTR pszSubtitle;
UINT cchSubtitle;
LPWSTR pszTask;
UINT cchTask;
LPWSTR pszDescriptionTop;
UINT cchDescriptionTop;
LPWSTR pszDescriptionBottom;
UINT cchDescriptionBottom;
int iTitleImage;
int iExtendedImage;
int iFirstItem;
UINT cItems;
LPWSTR pszSubsetTitle;
UINT cchSubsetTitle;
#endif
} LVGROUP, *PLVGROUP;
#define LVGROUP_V5_SIZE CCSIZEOF_STRUCT(LVGROUP, uAlign)
#define LVGF_HEADER 0x00000001
#define LVGF_GROUPID 0x00000010
HWND listview1;
LVCOLUMN list1;
LVITEM item1;
void centerWindows(HWND hwnd)
{
int scrWidth,scrHeight;
RECT rect;
scrWidth=GetSystemMetrics(SM_CXSCREEN);
scrHeight=GetSystemMetrics(SM_CYSCREEN);
GetWindowRect(hwnd,&rect);
SetWindowPos(hwnd,HWND_TOP,(scrWidth-rect.right+rect.left)/2,(scrHeight-rect.bottom+rect.top)/2,rect.right-rect.left,rect.bottom-rect.top,SWP_SHOWWINDOW);
}
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
TCHAR szClassName[] = TEXT("WindowsApp");
int WINAPI
WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS|CS_HREDRAW|CS_VREDRAW; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; //MAKEINTRESOURCE (IDC_TEST1);
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW);
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
TEXT("SDK_LISTVIEW_GROUP"), /* Title Text */
// WS_OVERLAPPEDWINDOW, /* default window */
WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX,//|WS_THICKFRAME, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
800, /* The programs width */
500, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
//SetWindowLong(hwnd, GWL_STYLE, WS_OVERLAPPED | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
/* Make the window visible on the screen */
centerWindows(hwnd);
ShowWindow (hwnd, nFunsterStil);
UpdateWindow(hwnd);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
INITCOMMONCONTROLSEX icex; // Ensure that the common control DLL is loaded.
switch (message) /* handle the messages */
{
//构建窗体时
case WM_CREATE:
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex);
//创建listview
listview1 = CreateWindowEx(WS_EX_STATICEDGE,
WC_LISTVIEW,
NULL, WS_BORDER|WS_CHILD|WS_VISIBLE|LVS_REPORT|LVS_SINGLESEL|LVS_NOSORTHEADER|LVS_SHOWSELALWAYS,
5, 5, 780, 460,
hwnd, NULL, NULL, NULL);
ListView_SetExtendedListViewStyle(listview1, LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);//设置listview扩展风格
ListView_EnableGroupView(listview1, TRUE);//启用分组支持
LVGROUP group1;
group1.cbSize = LVGROUP_V5_SIZE;
group1.mask = LVGF_HEADER | LVGF_GROUPID;
group1.pszHeader = (LPWSTR)("组1");
group1.iGroupId = 0;
ListView_InsertGroup(listview1, -1, &group1);
group1.pszHeader = (LPWSTR)("组2 - Hello World");
group1.iGroupId = 1;
ListView_InsertGroup(listview1, -1, &group1);
//内存清零
RtlZeroMemory(&list1, sizeof(LVCOLUMN));
RtlZeroMemory(&item1, sizeof(LVITEM));
//创建列
list1.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;//掩码
list1.fmt = LVCFMT_LEFT;//左对齐
list1.cx = 100;//列宽
list1.pszText = TEXT("列1");
ListView_InsertColumn(listview1, 0, &list1);//创建列
list1.pszText = TEXT("list2");
list1.cx = 200;
ListView_InsertColumn(listview1, 1, &list1);
list1.pszText = TEXT("hello world");
ListView_InsertColumn(listview1, 2, &list1);
list1.pszText = TEXT("happy");
ListView_InsertColumn(listview1, 3, &list1);
//创建项目
item1.mask = LVIF_TEXT | LVIF_GROUPID;
item1.pszText = TEXT("项目");
item1.iItem = 0;//项目号
item1.iGroupId = 0;
ListView_InsertItem(listview1, &item1);
item1.iItem = 1;
ListView_InsertItem(listview1, &item1);
item1.pszText = TEXT("item 3");
item1.iItem = 2;
ListView_InsertItem(listview1, &item1);
item1.pszText = TEXT("第四个");
item1.iItem = 3;
ListView_InsertItem(listview1, &item1);
item1.iGroupId = 1;
item1.pszText = TEXT("我在组2内");
item1.iItem = 4;
ListView_InsertItem(listview1, &item1);
item1.pszText = TEXT("item 5");
item1.iItem = 2;
ListView_InsertItem(listview1, &item1);
//创建子项目
item1.mask = LVIF_TEXT;
item1.iItem = 1;
item1.iSubItem = 1;
item1.pszText = TEXT("子项目");
ListView_SetItem(listview1, &item1);
item1.iItem = 1;
item1.iSubItem = 2;
item1.pszText = TEXT("子项目");
ListView_SetItem(listview1, &item1);
item1.iItem = 1;
item1.iSubItem = 3;
item1.pszText = TEXT("子项目");
ListView_SetItem(listview1, &item1);
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
程序能运行,但不分组!!??
[此贴子已经被作者于2018-6-25 00:43编辑过]