Win32 GUI编程之二-------------使用Win32控件(ListView)

// 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






  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值