如何选中其他程序ListView控件中的某个Item
1个回答
2015-08-25
展开全部
/*
* 本程序适用于:Win2KP+SP4[Windows TaskManager(5.0.2195.6620)]
08.
* WinXP+SP1[Windows TaskManager]
09.
* 代码在Win2000P+SP4 + VC6+SP6测试通过
10.
*/
11.
12.
13.
#include< windows.h >
14.
#include< commctrl.h >
15.
16.
17.
18.
int APIENTRY WinMain(HINSTANCE hInstance,
19.
HINSTANCE hPrevInstance,
20.
LPSTR lpCmdLine,
21.
int nCmdShow)
22.
23.
{
24.
25.
HWND hwnd;
26.
int iItem=0;
27.
LVITEM lvitem, *plvitem;
28.
DWORD PID;
29.
HANDLE hProcess;
30.
31.
32.
hwnd=FindWindow("#32770","Windows 任务管理器");
33.
hwnd=FindWindowEx(hwnd,0,"#32770",0);
34.
hwnd=FindWindowEx(hwnd,0,"SysListView32",0);
35.
36.
if (!hwnd)
37.
38.
MessageBox(NULL,"[Windows 任务管理器] 尚未启动!","错误!",NULL);
39.
40.
else
41.
{
42.
43.
GetWindowThreadProcessId(hwnd, &PID);
44.
45.
hProcess=OpenProcess(PROCESS_ALL_ACCESS,false,PID);
46.
if (!hProcess)
47.
48.
MessageBox(NULL,"获取进程句柄操作失败!","错误!",NULL);
49.
50.
else
51.
{
52.
plvitem=(LVITEM*)VirtualAllocEx(hProcess, NULL,sizeof(LVITEM),
53.
MEM_COMMIT, PAGE_READWRITE);
54.
55.
if (!plvitem)
56.
57.
MessageBox(NULL,"无法分配内存!","错误!",NULL);
58.
59.
else
60.
{
61.
MessageBox(NULL,"本演示程序将选中TaskManager中的第6个项目。","提示",NULL);
62.
63.
iItem=5;//5在这里是第六个(从零开始)
64.
65.
lvitem.state=LVIS_SELECTED;
66.
lvitem.stateMask=LVIS_SELECTED;
67.
WriteProcessMemory(hProcess, plvitem, &lvitem,sizeof(LVITEM), NULL);
68.
SendMessage(hwnd, LVM_SETITEMSTATE, (WPARAM)iItem, (LPARAM)plvitem);
69.
70.
}
71.
}
72.
}
73.
74.
//释放内存
75.
CloseHandle(hwnd);
76.
CloseHandle(hProcess);
77.
VirtualFreeEx(hProcess, plvitem, 0, MEM_RELEASE);
78.
79.
return 0;
* 本程序适用于:Win2KP+SP4[Windows TaskManager(5.0.2195.6620)]
08.
* WinXP+SP1[Windows TaskManager]
09.
* 代码在Win2000P+SP4 + VC6+SP6测试通过
10.
*/
11.
12.
13.
#include< windows.h >
14.
#include< commctrl.h >
15.
16.
17.
18.
int APIENTRY WinMain(HINSTANCE hInstance,
19.
HINSTANCE hPrevInstance,
20.
LPSTR lpCmdLine,
21.
int nCmdShow)
22.
23.
{
24.
25.
HWND hwnd;
26.
int iItem=0;
27.
LVITEM lvitem, *plvitem;
28.
DWORD PID;
29.
HANDLE hProcess;
30.
31.
32.
hwnd=FindWindow("#32770","Windows 任务管理器");
33.
hwnd=FindWindowEx(hwnd,0,"#32770",0);
34.
hwnd=FindWindowEx(hwnd,0,"SysListView32",0);
35.
36.
if (!hwnd)
37.
38.
MessageBox(NULL,"[Windows 任务管理器] 尚未启动!","错误!",NULL);
39.
40.
else
41.
{
42.
43.
GetWindowThreadProcessId(hwnd, &PID);
44.
45.
hProcess=OpenProcess(PROCESS_ALL_ACCESS,false,PID);
46.
if (!hProcess)
47.
48.
MessageBox(NULL,"获取进程句柄操作失败!","错误!",NULL);
49.
50.
else
51.
{
52.
plvitem=(LVITEM*)VirtualAllocEx(hProcess, NULL,sizeof(LVITEM),
53.
MEM_COMMIT, PAGE_READWRITE);
54.
55.
if (!plvitem)
56.
57.
MessageBox(NULL,"无法分配内存!","错误!",NULL);
58.
59.
else
60.
{
61.
MessageBox(NULL,"本演示程序将选中TaskManager中的第6个项目。","提示",NULL);
62.
63.
iItem=5;//5在这里是第六个(从零开始)
64.
65.
lvitem.state=LVIS_SELECTED;
66.
lvitem.stateMask=LVIS_SELECTED;
67.
WriteProcessMemory(hProcess, plvitem, &lvitem,sizeof(LVITEM), NULL);
68.
SendMessage(hwnd, LVM_SETITEMSTATE, (WPARAM)iItem, (LPARAM)plvitem);
69.
70.
}
71.
}
72.
}
73.
74.
//释放内存
75.
CloseHandle(hwnd);
76.
CloseHandle(hProcess);
77.
VirtualFreeEx(hProcess, plvitem, 0, MEM_RELEASE);
78.
79.
return 0;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询