下面这段代码是获取托盘里一个图标的信息,不知道问题出在哪里,帮忙看下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,shellAPI, StdCtrls, ComCtrls, ImgList,commctrl;type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
ImageList1: TImageList;
ListView1: TListView;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
function GetSysTrayWnd: HWND;implementation{$R *.dfm}
var
h_Tray:hwnd;function GetSysTrayWnd: HWND;
{
返回系统托盘的句柄,适合于WinXP以上版本
}
begin
Result := FindWindow('Shell_TrayWnd', nil);
Result := FindWindowEx(Result, 0, 'TrayNotifyWnd', nil);
Result := FindWindowEx(Result, 0, 'SysPager', nil);
Result := FindWindowEx(Result, 0, 'ToolbarWindow32', nil);
end;procedure TForm1.Button1Click(Sender: TObject);
var
h_ico:HICON;
h_icolist:TStringlist;
btn_count,i:integer;
h_Process,dwProcessID:DWord;
Point:Pointer;
btn_ico:Tbutton;
nNumberOfBytesRead:dword;
text:string;
begin
h_Tray:=GetSysTrayWnd;
btn_count:=sendmessage(h_Tray,TB_BUTTONCOUNT,0,0);//得到托盘区图标个数
GetWindowThreadProcessId(h_Tray,@dwProcessID);//得到线程id
h_Process:=OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE,
false,
dwProcessID);得到进程句柄
point:=VirtualAllocEx(h_Process,//创建虚拟空间
nil,
4096,
MEM_RESERVE or MEM_COMMIT,
PAGE_READWRITE);
for i:=0 to btn_count do
begin
btn_ico:=Tbutton.Create(self);
SendMessage(h_Tray, TB_GETBUTTON, i, LPARAM(Point));
ReadProcessMemory(h_Process,
(Point),
@btn_ico,
sizeof(@btn_ico),
nNumberOfBytesRead);
SendMessage(h_Tray, TB_GETBUTTONTEXT , i, LPARAM(Point));//得到托盘button的title信息.
listbox1.Items.Add(text);
ReadProcessMemory(h_Process,
(Point),
@btn_ico,
sizeof(@text),
nNumberOfBytesRead);
listbox1.Items.Add(text);//加进listbox end; SHOWmessage(inttostr(btn_count));
{
h_icolist:=TStringlist.Create;
imagelist1.Handle:=(SendMessage(h_Tray,TB_GETIMAGELIST,0,0));//释放
listbox1.Items.Add(inttostr(imagelist1.Handle)); }end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,shellAPI, StdCtrls, ComCtrls, ImgList,commctrl;type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
ImageList1: TImageList;
ListView1: TListView;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
function GetSysTrayWnd: HWND;implementation{$R *.dfm}
var
h_Tray:hwnd;function GetSysTrayWnd: HWND;
{
返回系统托盘的句柄,适合于WinXP以上版本
}
begin
Result := FindWindow('Shell_TrayWnd', nil);
Result := FindWindowEx(Result, 0, 'TrayNotifyWnd', nil);
Result := FindWindowEx(Result, 0, 'SysPager', nil);
Result := FindWindowEx(Result, 0, 'ToolbarWindow32', nil);
end;procedure TForm1.Button1Click(Sender: TObject);
var
h_ico:HICON;
h_icolist:TStringlist;
btn_count,i:integer;
h_Process,dwProcessID:DWord;
Point:Pointer;
btn_ico:Tbutton;
nNumberOfBytesRead:dword;
text:string;
begin
h_Tray:=GetSysTrayWnd;
btn_count:=sendmessage(h_Tray,TB_BUTTONCOUNT,0,0);//得到托盘区图标个数
GetWindowThreadProcessId(h_Tray,@dwProcessID);//得到线程id
h_Process:=OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE,
false,
dwProcessID);得到进程句柄
point:=VirtualAllocEx(h_Process,//创建虚拟空间
nil,
4096,
MEM_RESERVE or MEM_COMMIT,
PAGE_READWRITE);
for i:=0 to btn_count do
begin
btn_ico:=Tbutton.Create(self);
SendMessage(h_Tray, TB_GETBUTTON, i, LPARAM(Point));
ReadProcessMemory(h_Process,
(Point),
@btn_ico,
sizeof(@btn_ico),
nNumberOfBytesRead);
SendMessage(h_Tray, TB_GETBUTTONTEXT , i, LPARAM(Point));//得到托盘button的title信息.
listbox1.Items.Add(text);
ReadProcessMemory(h_Process,
(Point),
@btn_ico,
sizeof(@text),
nNumberOfBytesRead);
listbox1.Items.Add(text);//加进listbox end; SHOWmessage(inttostr(btn_count));
{
h_icolist:=TStringlist.Create;
imagelist1.Handle:=(SendMessage(h_Tray,TB_GETIMAGELIST,0,0));//释放
listbox1.Items.Add(inttostr(imagelist1.Handle)); }end;end.
SizeOf(@btn_ico)、SizeOf(@text)都为四····参考如下代码:
procedure TForm1.Button1Click(Sender: TObject);
var
h_ico: HICON;
h_icolist: TStringlist;
btn_count, i: integer;
h_Process, dwProcessID:DWord;
Point: Pointer;
btn_ico: TTBButton;
nNumberOfBytesRead: dword;
buffer: array[0..255] of Char;
begin
h_Tray:=GetSysTrayWnd;
btn_count:=sendmessage(h_Tray,TB_BUTTONCOUNT,0,0);//得到托盘区图标个数
GetWindowThreadProcessId(h_Tray,@dwProcessID);//得到线程id
h_Process:=OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE,
false,
dwProcessID); //得到进程句柄
point:=VirtualAllocEx(h_Process,//创建虚拟空间
nil,
4096,
MEM_RESERVE or MEM_COMMIT,
PAGE_READWRITE);
for i:=0 to btn_count - 1 do
begin
SendMessage(h_Tray, TB_GETBUTTON, i, LPARAM(Point));
ReadProcessMemory(h_Process,
(Point),
@btn_ico,
sizeof(btn_ico),
nNumberOfBytesRead);
SendMessage(h_Tray, TB_GETBUTTONTEXT , i, LPARAM(Point));//得到托盘button的title信息.
ReadProcessMemory(h_Process,
(Point),
@buffer,
sizeof(buffer),
nNumberOfBytesRead);
listbox1.Items.Add(buffer);//加进listbox end; SHOWmessage(inttostr(btn_count));
{
h_icolist:=TStringlist.Create;
imagelist1.Handle:=(SendMessage(h_Tray,TB_GETIMAGELIST,0,0));//释放
listbox1.Items.Add(inttostr(imagelist1.Handle)); }
end;