views:

663

answers:

5
+1  A: 

The icon shown on the task bar is taken from the window itself. If the only window is the standard Windows MesssageBox, then you'll get some sort of OS default. You have to create your own window and give it an icon, then Windows will use that.

Paul Alexander
Will an invisible window work for this?
Skrymsli
That you'd have to test. I suspect that it would as long as the message pump is on the main thread - just don't make the form invisible just at location -10000,-10000 or something like that.
Paul Alexander
+2  A: 

This looks like just sample code. If the real code is a non-console Windows application, you can do this:

Give your application's main window a task bar icon by calling SetIcon(). Then when you call MessageBox(), set the first parameter to the HWND of your application's main window.

John Dibling
Actually, I made the sample this way on purpose. I have a COM object that displays a window. My application doesn't actually create any windows directly. The icon in the taskbar from the com object's window is wacky.
Skrymsli
Although, your answer made me look harder at SetIcon and it turns out that I can get the window handle from the COM object and do ::SendMessage(wnd, WM_SETICON, FALSE, (LPARAM)hIcon); and that works!
Skrymsli
Nice. I knew COM created an invisible window, but I did not know you could use it in this way. Thanks for the update.
John Dibling
A: 
WNDCLASSEX wndclass;

wndclass.cbSize        = sizeof(wndclass);
// ..
wndclass.hIconSm       = ExtractIconEx( ... );
RegisterClassEx(&wndclass);

HWDN wnd = CreateWindow(...)
Magnus Skog
A: 

Why not just add an icon resource to the EXE? I'm pretty sure Windows will try that before falling back to the "generic" icons.

ChrisV
I tried that. Is there some particular way to add the icon so that what you are suggesting will work? I couldn't get it to work.
Skrymsli
A: 

Create a form but never show it then assign it an icon and use that as the parent of your message box.

This hides the icon:

using (var f = new Form())
{
    MessageBox.Show(f,"my message");
}

This will create an icon:

using (var f = new Form())
{
    f.Icon = Resources.IconUpload;
    f.Location=new Point(-1000,-1000);
    f.StartPosition = FormStartPosition.Manual;
    f.Show();
    MessageBox.Show(f,"my message");
}
eibrahim