我正在尝试获取表单的图标,并将其设置为我自己的应用程序的图标。我找到了这段代码,但它并不适合我:
MyIcon := TIcon.Create;
Try
MyIcon.Handle := MyHandle;
MyForm.Icon := MyIcon;
Finally
MyIcon.Free;
End;
end;
我尝试打开一个应用程序,我知道它有一个特定的Icon,我发现它的句柄,但是我的应用程序中显示的Icon是默认的Windows exe文件。
有什么想法吗?
答案 0 :(得分:4)
要回答帖子标题中的问题:
要从TIcon
获取HWND
,即获取与窗口关联的图标(不一定在您自己的应用程序中),请执行以下操作:
procedure TForm1.FormClick(Sender: TObject);
var
HIco: HICON;
Icon: TIcon;
begin
HIco := SendMessage(Handle, WM_GETICON, ICON_BIG, 0);
if HIco = 0 then
HIco := SendMessage(Handle, WM_GETICON, ICON_SMALL2, 0);
Icon := TIcon.Create;
try
Icon.ReleaseHandle;
Icon.Handle := HIco;
// Do something with Icon, for instance
Canvas.Draw(10, 10, Icon);
finally
Icon.Free;
end;
end;
要使用HWND
窗口的图标作为您自己表单的图标,请执行
procedure TForm1.FormClick(Sender: TObject);
var
HWin: HWND;
HIco: HICON;
begin
HWin := FindWindow(nil, 'New file'); // A Rejbrand Text Editor window
HIco := SendMessage(HWin, WM_GETICON, ICON_BIG, 0);
if HIco = 0 then
HIco := SendMessage(HWin, WM_GETICON, ICON_SMALL2, 0);
Self.Icon.ReleaseHandle; // important!
Self.Icon.Handle := HIco;
end;