// Windows 头文件:
#include <windows.h>
// C 运行时头文件
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <stdio.h>
#include<CommCtrl.h>
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR *szTitle=TEXT("HelloWorld"); // 标题栏文本
TCHAR *szWindowClass=TEXT("MainWin"); // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
//初始化控件库
InitCommonControls();
//注册窗口类
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
/**窗口类**/
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.hInstance = hInstance;
wcex.lpszClassName = szWindowClass;
wcex.lpfnWndProc = WndProc;
wcex.style = CS_HREDRAW | CS_VREDRAW;
/**窗口特性**/
wcex.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.lpszMenuName = NULL;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
/**窗口背景**/
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
DWORD dwStyle;
static HWND hwndListView;
LV_COLUMN lvColumn;
LV_ITEM lvItem;
switch (message)
{
case WM_CREATE:
dwStyle = WS_TABSTOP |WS_CHILD |WS_BORDER |WS_VISIBLE |LVS_AUTOARRANGE |LVS_REPORT;
hwndListView = CreateWindowEx( WS_EX_CLIENTEDGE, // ex style
WC_LISTVIEW, // class name - defined in commctrl.h
TEXT(""), // dummy text
dwStyle, // style
0, // x position
0, // y position
600, // width
400, // height
hWnd, // parent
NULL, // ID
hInst, // instance
NULL);// no extra data
/***添加表头***/
lvColumn.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
lvColumn.cx = 100;//列宽,可以为每个列指定
// 第一列
lvColumn.iSubItem = 0;//用于控制添加的顺序,默认从前向后添加
lvColumn.pszText = TEXT("姓名");//列标题
ListView_InsertColumn(hwndListView, 0, &lvColumn);
// 第二列
lvColumn.iSubItem = 1;//子项索引
lvColumn.pszText = TEXT("年龄");
ListView_InsertColumn(hwndListView, 1, &lvColumn);
// 第三列
lvColumn.iSubItem = 2;
lvColumn.pszText = TEXT("地址");
ListView_InsertColumn(hwndListView, 2, &lvColumn);
//初始化
//ListView_DeleteAllItems(hwndListView);
ListView_SetItemCount(hwndListView, 100000);//设置项目数,分配内存使用
/***添加数据**/
lvItem.mask = LVIF_TEXT;
//插入第一行数据,这一段代码是不可以少的。
lvItem.iItem =0;//行号,从0开始
lvItem.iSubItem=0;//列号,
ListView_InsertItem(hwndListView,&lvItem);
//第一列
lvItem.iSubItem=0;//列索引
lvItem.pszText = TEXT("张三");//列数据
ListView_SetItem(hwndListView,&lvItem);
//第二列
lvItem.iSubItem=1;
lvItem.pszText = TEXT("30");
ListView_SetItem(hwndListView,&lvItem);
//第三列
lvItem.iSubItem=2;
lvItem.pszText = TEXT("北京市");
ListView_SetItem(hwndListView,&lvItem);
//添加第二行
lvItem.iItem =1;//行号
lvItem.iSubItem=0;
ListView_InsertItem(hwndListView,&lvItem);
//第一列
lvItem.iSubItem=0;
lvItem.pszText = TEXT("李四");
ListView_SetItem(hwndListView,&lvItem);
//第二列
lvItem.iSubItem=1;
lvItem.pszText = TEXT("50");
ListView_SetItem(hwndListView,&lvItem);
//第三列
lvItem.iSubItem=2;
lvItem.pszText = TEXT("西安市");
ListView_SetItem(hwndListView,&lvItem);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
这段代码是自己好不容易探索出来的,希望对有兴趣进行Win32开发的人有所帮助!
在自己探索Win32控件的使用的时候,发现其实Microsoft提供的例子也不多,也不全面。最需要了解的往往没有。
全部的用微软提供的工具自己摸索,哎,脑细胞死了好多。
下面是微软的工具
Comtrol spy:http://msdn.microsoft.com/en-us/library/bb773165(VS.85).aspx
Controls文档页面:https://msdn.microsoft.com/en-us/library/windows/desktop/bb773173(v=vs.85).aspx