-
I'm trying to load an icon to set in the Windows taskbar, using this code: public static void LoadIcon(Window window, string iconName)
{
string iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, iconName);
if (!File.Exists(iconPath))
return;
var hwnd = new HWND(GetWindowHandle(window));
using var hIcon = PInvoke.LoadImage(null, iconPath, GDI_IMAGE_TYPE.IMAGE_ICON, 128, 128, IMAGE_FLAGS.LR_LOADFROMFILE);
var handle = hIcon.DangerousGetHandle();
if (handle != 0)
{
PInvoke.SendMessage(hwnd, PInvoke.WM_SETICON, new WPARAM(PInvoke.ICON_BIG), new LPARAM(handle));
}
//PInvoke.DestroyIcon(handle);
}
Is there any workaround? Thanks |
Beta Was this translation helpful? Give feedback.
Answered by
AArnott
May 3, 2024
Replies: 1 comment 2 replies
-
I wonder if you're expected to keep the icon valid for the life of it being displayed on the taskbar, rather than disposing of it. I also see you calling |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you avoid the
SafeHandle
overload altogether, there won't be aSafeHandle
to worry about releasing or finalizing. The nativeHANDLE
you get back would typically need to be released when you're done with it, but if you want it for the lifetime of your application, you don't ever have to release it because closing the process has the OS implicitly release it. So you can call theLoadImage
overload that returns a HANDLE and then just drop the result (after using it in SendMessage).