高分求一个ListView的例子

zy1691 2008-12-11 12:53:42
求一个ListView的例子代码,注意要sdk的,不是clistcrtl的。
因为在网上找了好多,都不大完整,看了半天没闹明白,对于ListView_InsertColumn,ListView_InsertItem这些东西懵懵懂懂,只好来这向大牛们请教了。
最好是完整的能运行的程序,有注释更好,或者有完整的使用步骤介绍,总之只要看明白了就立即给分,谢谢了。
...全文
141 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
up
toadzw 2008-12-12
  • 打赏
  • 举报
回复
//#include "stdafx.h"

#include <windowsx.h>
#include <tchar.h>
#include <CommCtrl.h>

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

HINSTANCE hInst = NULL;
HWND hList = NULL;

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASSEX wcex;

hInst = hInstance;

ZeroMemory(&wcex, sizeof(wcex));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszClassName = TEXT("Win32");

RegisterClassEx(&wcex);

HWND hWnd = CreateWindow(
TEXT("Win32"),
TEXT("Win32"),
WS_OVERLAPPEDWINDOW,
500,
300,
500,
300,
NULL,
NULL,
hInstance,
NULL);

if (!hWnd)
return FALSE;

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

SetTimer(hWnd, 1, 10, NULL);

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int) msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_CREATE :
{
hList = CreateWindow(
TEXT("SysListView32"),
TEXT("ListView"),
WS_CHILD | WS_BORDER | WS_VISIBLE | LVS_REPORT,
0,
0,
500,
300,
hWnd,
NULL,
hInst,
NULL);
if (hList)
{
LVCOLUMN info;
info.mask = LVCF_TEXT | LVCF_WIDTH;
info.pszText = TEXT("Column0");
info.cx = 100;
ListView_InsertColumn(hList, 0, &info);

info.mask = LVCF_TEXT | LVCF_WIDTH;
info.pszText = TEXT("Column1");
info.cx = 100;
ListView_InsertColumn(hList, 1, &info);

info.mask = LVCF_TEXT | LVCF_WIDTH;
info.pszText = TEXT("Column2");
info.cx = 100;
ListView_InsertColumn(hList, 2, &info);

info.mask = LVCF_TEXT | LVCF_WIDTH;
info.pszText = TEXT("Column3");
info.cx = 200;
ListView_InsertColumn(hList, 3, &info);

for (int i = 0; i < 4; ++i)
{
LVITEM item;
ZeroMemory(&item, sizeof(item));
item.mask = LVIF_TEXT;
item.cchTextMax = sizeof(TEXT("item"));
item.pszText = TEXT("item");
item.iItem = i;
ListView_InsertItem(hList, &item);

for (int j = 0; j < 4; ++j)
{
TCHAR text[32];
_stprintf(text, TEXT("item-%d-%d"), i, j);
ListView_SetItemText(hList, i, j, text);
}
}
}
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

帅得不敢出门 2008-12-12
  • 打赏
  • 举报
回复
http://www.codeproject.com/win32/lvwDlgZj.asp
bencharluo 2008-12-12
  • 打赏
  • 举报
回复
up
sagegz 2008-12-12
  • 打赏
  • 举报
回复
还没学到这,进来学习下!支持LZ的学习态度.
zy1691 2008-12-12
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wiowei 的回复:]
引用还有点小问题不明白,例子里面ListView的风格用了LVS_EX_FULLROWSELECT,一选就是一整排,怎么弄才能让他每个item可以单独选呢?我试了其他的风格,就变成只有第一个item可以选.其他后面的的subitem全都选不到,也不能操作.不知道该怎么弄。

http://support.microsoft.com/kb/131284/zh-cn

另外作者在用InitCommonControlsEx初始化控件了以后,在WM_INITDIALOG里面又用了一个InitCommonControls(); 不明白他这什么意思,为什…
[/Quote]
了解了,谢谢Rene。
wiowei 2008-12-12
  • 打赏
  • 举报
回复
[Quote]还有点小问题不明白,例子里面ListView的风格用了LVS_EX_FULLROWSELECT,一选就是一整排,怎么弄才能让他每个item可以单独选呢?我试了其他的风格,就变成只有第一个item可以选.其他后面的的subitem全都选不到,也不能操作.不知道该怎么弄。

http://support.microsoft.com/kb/131284/zh-cn

另外作者在用InitCommonControlsEx初始化控件了以后,在WM_INITDIALOG里面又用了一个InitCommonControls(); 不明白他这什么意思,为什么初始化两次?我把InitCommonControls();注释掉以后运行也挺正常,不理解.

InitCommonControls是为了使用XP界面风格,你是XP系统注释掉也无所谓,不过据说可能会导致运行问题(没研究过)

还有个小问题就是OleInitialize(NULL);起什么作用的?

OleInitialize是初始化Ole的运行环境,Ole是在Com的基础上作的扩展,是ActiveX运行的基础。你用的就是com controls(没注意到这个头文件吗:#include <commctrl.h>)
[/Quote]
zy1691 2008-12-12
  • 打赏
  • 举报
回复
没人解答我的问题吗
zy1691 2008-12-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wiowei 的回复:]
http://www.codeproject.com/KB/combobox/listview.aspx
[/Quote]
这个例子不错,谢谢了.
看到脑袋大了,基本明白怎么搞了.作者还用了自绘,背景图什么的,功能挺丰富,让我脑袋更大了点.

还有点小问题不明白,例子里面ListView的风格用了LVS_EX_FULLROWSELECT,一选就是一整排,怎么弄才能让他每个item可以单独选呢?我试了其他的风格,就变成只有第一个item可以选.其他后面的的subitem全都选不到,也不能操作.不知道该怎么弄.

另外作者在用InitCommonControlsEx初始化控件了以后,在WM_INITDIALOG里面又用了一个InitCommonControls(); 不明白他这什么意思,为什么初始化两次?我把InitCommonControls();注释掉以后运行也挺正常,不理解.

还有个小问题就是OleInitialize(NULL);起什么作用的?
分数我加高,麻烦费心了.
malone1 2008-12-11
  • 打赏
  • 举报
回复
UP
zy1691 2008-12-11
  • 打赏
  • 举报
回复
to星宇,只能看见个白方框啊,listview没有显示.
星羽 2008-12-11
  • 打赏
  • 举报
回复
给你写个简单的


// win32.cpp : Defines the entry point for the application.
//

//#include "stdafx.h"

#include <windowsx.h>
#include <tchar.h>
#include <CommCtrl.h>

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

HINSTANCE hInst = NULL;
HWND hList = NULL;

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASSEX wcex;

hInst = hInstance;

ZeroMemory(&wcex, sizeof(wcex));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszClassName = TEXT("Win32");

RegisterClassEx(&wcex);

HWND hWnd = CreateWindow(
TEXT("Win32"),
TEXT("Win32"),
WS_OVERLAPPEDWINDOW,
500,
300,
500,
300,
NULL,
NULL,
hInstance,
NULL);

if (!hWnd)
return FALSE;

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

SetTimer(hWnd, 1, 10, NULL);

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int) msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_CREATE :
{
hList = CreateWindow(
TEXT("SysListView32"),
TEXT("ListView"),
WS_CHILD | WS_BORDER | WS_VISIBLE | LVS_REPORT,
0,
0,
500,
300,
hWnd,
NULL,
hInst,
NULL);
if (hList)
{
LVCOLUMN info;
info.mask = LVCF_TEXT | LVCF_WIDTH;
info.pszText = TEXT("Column0");
info.cx = 100;
ListView_InsertColumn(hList, 0, &info);

info.mask = LVCF_TEXT | LVCF_WIDTH;
info.pszText = TEXT("Column1");
info.cx = 100;
ListView_InsertColumn(hList, 1, &info);

info.mask = LVCF_TEXT | LVCF_WIDTH;
info.pszText = TEXT("Column2");
info.cx = 100;
ListView_InsertColumn(hList, 2, &info);

info.mask = LVCF_TEXT | LVCF_WIDTH;
info.pszText = TEXT("Column3");
info.cx = 200;
ListView_InsertColumn(hList, 3, &info);

for (int i = 0; i < 4; ++i)
{
LVITEM item;
ZeroMemory(&item, sizeof(item));
item.mask = LVIF_TEXT;
item.cchTextMax = sizeof(TEXT("item"));
item.pszText = TEXT("item");
item.iItem = i;
ListView_InsertItem(hList, &item);

for (int j = 0; j < 4; ++j)
{
TCHAR text[32];
_stprintf(text, TEXT("item-%d-%d"), i, j);
ListView_SetItemText(hList, i, j, text);
}
}
}
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
wiowei 2008-12-11
  • 打赏
  • 举报
回复
http://www.codeproject.com/KB/combobox/listview.aspx
zy1691 2008-12-11
  • 打赏
  • 举报
回复
顶一下

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧