在windows SDK 中的相关控件编程中,List Control 可以说是很常见的一种控件,但对于这种控件的操作,网上基本上都是针对MFC的,对于MFC的相关 List Control,我这里不介绍,这里主要讲一下,纯 SDK的 List Control 操作.首先我们得将 commctrl.h,头文件引入到项目中,这个就是包含windows SDK组件的一个头文件.
然后我们再把ComCtl32.lib 这个动态库加入到项目中,然后 就可以进行高版本的控件编程。代码如下,自己去看了。。只有这一个文件,直接copy到你的项目中的C文件上就可以运行,当然不要忘记添加链接库.
#include <windows.h> #include <commctrl.h> HWND hwnd; HWND listCtrl; TCHAR szAppName[]="ListControl"; LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam); ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE hInstance,int iShowCmd); void InsertItemString(HWND hCtrl); int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow){ MSG msg; MyRegisterClass(hInstance); if(!InitInstance(hInstance,nCmdShow)){ return 0; } while(GetMessage(&msg,NULL,0,0)){ TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } ATOM MyRegisterClass(HINSTANCE hInstance){ WNDCLASSEX wcex; INITCOMMONCONTROLSEX ct; wcex.cbSize=sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WinProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(NULL,IDI_APPLICATION); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = szAppName; wcex.hIconSm = LoadIcon(NULL, NULL); ct.dwSize=sizeof(INITCOMMONCONTROLSEX); ct.dwICC=ICC_WIN95_CLASSES; return RegisterClassEx(&wcex) && InitCommonControlsEx(&ct); } BOOL InitInstance(HINSTANCE hInstance,int iShowCmd){ //父 菜单 hwnd=CreateWindow(szAppName,"列表控件",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,hInstance,NULL); if(!hwnd){ return FALSE; } ShowWindow(hwnd,iShowCmd); UpdateWindow(hwnd); return TRUE; } LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){ RECT rect; LVCOLUMN lvc; TCHAR * lpszNames[3] = { TEXT("编号"), TEXT("姓名"), TEXT("年龄") }; int i = 0; switch(message){ case WM_CREATE: //这里创建一个List View listCtrl=CreateWindowEx(0L,WC_LISTVIEW,NULL,WS_CHILD | WS_BORDER |WS_VISIBLE| LVS_REPORT | LVS_EDITLABELS | LVS_SHOWSELALWAYS, 0,0,200,200,hwnd,(HMENU)1000,((CREATESTRUCT*)lParam)->hInstance,NULL); for (; i < 3 ; i++) { lvc.mask = LVCF_TEXT | LVCF_WIDTH; lvc.cx = 200; lvc.cchTextMax = 10; lvc.pszText = lpszNames[i]; ListView_InsertColumn(listCtrl,i,&lvc); } ListView_SetExtendedListViewStyleEx(listCtrl,0,LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES); InsertItemString(listCtrl); return 0; case WM_SIZE: GetClientRect(hwnd,&rect); MoveWindow(listCtrl,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,TRUE); //ShowWindow(listCtrl,WM_NOMAL); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wParam,lParam); } void InsertItemString(HWND hCtrl){ LVITEM lvi; lvi.mask=LVIF_TEXT; lvi.cchTextMax=100; lvi.pszText="A00001"; lvi.iItem=0; lvi.iSubItem=0; ListView_InsertItem(hCtrl,&lvi); ListView_SetItemText(hCtrl,0,1,"小明"); ListView_SetItemText(hCtrl,0,2,"男"); lvi.pszText="A00002"; lvi.iItem=1; ListView_InsertItem(hCtrl,&lvi); ListView_SetItemText(hCtrl,1,1,"小花"); ListView_SetItemText(hCtrl,1,2,"女"); lvi.pszText="A00003"; lvi.iItem=2; ListView_InsertItem(hCtrl,&lvi); ListView_SetItemText(hCtrl,2,1,"小李"); ListView_SetItemText(hCtrl,2,2,"男"); }