2

I was wondering if there is a simple way add and remove an Icon of a WPF Window at runtime. This is not duplicate question; please read it carefully. I need a way to hide the icon of a WPF Window while the program is running and the window is initialized. All of my searches route to this code sample (from Stack Overflow):

IntPtr hwnd = new WindowInteropHelper(window).Handle;

// Change the extended window style to not show a window icon
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);

// Update the window's non-client area to reflect the changes
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);

...which doesn't work. I have tried it extensively and can confirm that on 64-Bit Windows 10 version 1607 it does not work at all. Another code sample I get directed to is this one (also from Stack Overflow):

const int ICON_SMALL = 0;
const int ICON_BIG = 1;

IntPtr hWnd = new WindowInteropHelper(this).Handle;
int currentStyle = (int)GetWindowLong(hWnd, GWL_EXSTYLE);

SetWindowLong(hWnd, GWL_EXSTYLE, (uint)currentStyle | WS_EX_DLGMODALFRAME);

SendMessage(hWnd, WM_SETICON, (IntPtr)ICON_SMALL, IntPtr.Zero);
SendMessage(hWnd, WM_SETICON, (IntPtr)ICON_BIG, IntPtr.Zero);

SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);

...which doesn't work either. Both options are seen in various adaptations all over the site, but I can't seem to get any of them working, even outside of Visual Studio. Is there now some new way that only works with windows 10? It would seem that the part which is not working is the frame redraw. As far as I can tell, it doesn't redraw anything. Are there any other options? All help is appreciated.

Alex Fanat
  • 748
  • 10
  • 22
  • This is a duplicate. You should search before asking. https://stackoverflow.com/questions/2341230/removing-icon-from-a-wpf-window?rq=1 – Bent Tranberg Jul 25 '17 at 01:47
  • 2
    One simple answer: Set the Icon to a transparent icon. – Bent Tranberg Jul 25 '17 at 01:47
  • 1
    @BentTranberg This is not a duplicate. I mention in my post that the codes samples I provided are from actual Stack Overflow questions, though it may not be too clear. What I am basically saying is that none of the currently available solutions presented by SO are working for me. That link that you posted has just an adaptation of my first code sample. It still does not work. Also, setting the icon to transparent does not work because it does not actually make the window look semi-modal, which is what I was going for in the first place. – Alex Fanat Jul 25 '17 at 07:30
  • Ok then, but shouldn't you update your question with this additional information? Seems to me the question is not quite what the title says either, but I could be wrong again. I don't know what you mean by semi-modal. Maybe others will, but perhaps an image illustrating it would be of help. – Bent Tranberg Jul 25 '17 at 08:58
  • 1
    @BentTranberg Thanks for the suggestion. I will update the title and I will add a screenshot of what I am looking for. What I meant by semi-modal was a window without an icon. Actual modal windows do not have minimize buttons nor do they have maximize buttons, and they can't be resized. An example is a MessageBox. I just need custom content within. – Alex Fanat Jul 25 '17 at 09:50
  • On second thought, the title is pretty representative of my question. – Alex Fanat Jul 25 '17 at 09:51
  • 1
    Oh man, I have the same issue on Windows 10 (mine is 1809). None of the answers for "Remove icon from a wpf window" work. Did you were able to solve the issue? – Xam Dec 01 '19 at 04:35
  • @Xam I was able to solve it but this was a long time ago and have since misplaced the solution. I will attempt to find it at some point and post it here. – Alex Fanat Dec 01 '19 at 12:01
  • @AlexFanat I was able to solve it. Your second code snippet worked. It seems that I have the wrong value for WM_SETICON. – Xam Dec 01 '19 at 17:15

0 Answers0