00 SDK中WC_LISTVIEW的应用【原】

Post date: 2012/4/3 上午 09:58:17

2009-11-01 14:44

代码如下:

#include <windows.h>

#include <CommCtrl.h> //不可少

HWND hWndList = NULL;

HINSTANCE g_hInstance = NULL;

LVITEM item;

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

// 主函数

int WINAPI WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInst,

LPSTR lpszCmdLine,

int nCmdShow)

{

HWND hwnd;

MSG Msg;

WNDCLASS Wndclass;

char lpszClassName[]="窗口";

char lpszTitle[]="my_windows";

//::InitCommonControls();

g_hInstance = hInstance;

// 窗口类的定义

Wndclass.style=0;

Wndclass.lpfnWndProc=WndProc;

Wndclass.cbClsExtra=0;

Wndclass.cbWndExtra=0;

Wndclass.hInstance=hInstance;

Wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);

Wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);

Wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

Wndclass.lpszMenuName="MENUE";

Wndclass.lpszClassName=lpszClassName;

// 注册窗口

if (!RegisterClass(&Wndclass))

{

MessageBeep(0);

return FALSE;

}

//创建窗口

hwnd = CreateWindow(lpszClassName,

lpszTitle,

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

NULL,

NULL,

hInstance,

NULL);

//显示窗口

ShowWindow(hwnd,nCmdShow);

//绘制用户区

UpdateWindow(hwnd);

//消息循环

while(GetMessage(&Msg,NULL,0,0))

{

TranslateMessage(&Msg);

DispatchMessage(&Msg);

}

return Msg.wParam;

}

//窗口函数

LRESULT CALLBACK WndProc(HWND hwnd,

UINT message,

WPARAM wParam,

LPARAM lParam)

{

LVCOLUMN ColInfo1 = {0};

LVCOLUMN ColInfo2 = {0};

switch(message)

{

case WM_CREATE:

hWndList = CreateWindowEx(0, WC_LISTVIEW,

WC_LISTVIEW,

WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |

LVS_REPORT | LVS_AUTOARRANGE | LVS_SHOWSELALWAYS | LVS_SHAREIMAGELISTS | LVS_SINGLESEL,

0,

0,

900, //900合适

900, //900合适

hwnd,

NULL,

g_hInstance,

NULL

);

ColInfo1.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;

ColInfo1.iSubItem = 0;

ColInfo1.fmt = LVCFMT_CENTER;

ColInfo1.cx = 300;

ColInfo1.pszText = "Col1";

ColInfo1.cchTextMax = 50;

ColInfo2.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;

ColInfo2.iSubItem = 0;

ColInfo2.fmt = LVCFMT_CENTER;

ColInfo2.cx = 300;

ColInfo2.pszText = "Col2";

ColInfo2.cchTextMax = 50;

SendMessage(hWndList, LVM_INSERTCOLUMN, WPARAM(0), LPARAM(&ColInfo1));

SendMessage(hWndList, LVM_INSERTCOLUMN, WPARAM(1), LPARAM(&ColInfo2));

item.mask=LVIF_TEXT;

item.pszText="哈哈";

item.iItem=0;

SendMessage(hWndList, LVM_INSERTITEM, 0, LPARAM(&item));

break ;

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(hwnd,message,wParam,lParam);

}

return(0);

}