I am trying to get the Icon of a form, and set it to my own app's Icon. I found this code, however it does not really work for me:
MyIcon := TIcon.Create;
Try
MyIcon.Handle := MyHandle;
MyForm.Icon := MyIcon;
Finally
MyIcon.Free;
End;
end;
I tried opening an app that I know has a specific Icon, I find it's h开发者_Go百科andle, but the Icon that is shown in my app, is the default Windows exe one.
Any ideas?
To answer the question in the title of your post:
To obtain a TIcon
from a HWND
, that is, to obtain the icon associated with a window (not necessarily in your own application), do something like
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;
To use the icon of the HWND
window as the icon of your own form, do
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;
精彩评论