tags:

views:

1152

answers:

6

I'm making a custom error dialog in my WPF app and I want to use a standard windows error icon. Can I get the OS-specific icon from WPF? If not, does anyone know where to get .pngs with transparency of them? Or know where in Windows to go extract them from?

So far my searches have turned up nothing.

+1  A: 

In Visual Studio, use File + Open + File and select c:\windows\system32\user32.dll. Open the Icon node and double-click 103. On my machine that's the Error icon. Back, right-click it and select Export to save it to a file.

That's the iffy way. These icons are also available in Visual Studio. From your Visual Studio install directory, navigate to Common7\VS2008ImageLibrary\xxxx\VS2008ImageLibrary.zip + VS2008ImageLibrary\Annotations&Buttons\ico_format\WinVista\error.ico. The redist.txt file in the Visual Studio install directly explicitly gives you the right to use this icon in your own application.

Hans Passant
Actually the unmanaged resource IDs are #defined in winuser.h and compiled into literally millions of programs by now. So they aren't changing. The index within the DLL, and even which DLL contains the system icons might change, but the resource ID never will.
Ben Voigt
@Ben: no, MB_ICONERROR has the value 0x10. Not close to the resource ID. Again: if you have special knowledge of how this works then, puhleze, post your own answer.
Hans Passant
@nobugz: You seem determined to make everything personal rather than let each technical topic stand alone. In this particular case, you might educate yourself by reading the answer I was typing as you posted yours and several hours before your comment. The resource ID OIC_HAND or equivalently OIC_ERROR, which is used with the MAKEINTRESOURCE macro, hasn't changed since LoadImage was introduced and never can, since it's compiled into millions of programs.
Ben Voigt
@nobugz: sorry, I was mistaken about the intervening time. It was just about one hour, not several hours before.
Ben Voigt
+5  A: 

About using Standard Microsoft Icons.

The vast majority of developers out there don't know that Visual Studio comes with an Image Library. So here goes two links that highlight it:

About using Microsoft Visual Studio 2010 Image Library.

About using Microsoft Visual Studio 2008 Image Library.

Leniel Macaferi
A: 

Can't you simply use Windows API?

Delphi Example:

procedure TForm1.FormClick(Sender: TObject);
var
  errIcon: HICON;
begin
  errIcon := LoadIcon(0, IDI_ERROR);
  DrawIcon(Canvas.Handle, 10, 10, errIcon)
end;
Andreas Rejbrand
A: 

You can use .NET's SystemIcons class for roughly the first three steps if the default size is ok, see modosansreves answer

So it could be as simple as:

 Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Error.Handle)
Ben Voigt
A: 

You can draw it like this:

Graphics g = this.CreateGraphics();
g.DrawIcon(SystemIcons.Question, 40, 40);
Hun1Ahpu
... no `Graphics` in WPF. What you wrote is for GDI.
modosansreves
GDI+ actually, but it's true that none of us have yet provided the conversion to a WPF image. I'm not familiar with WPF but I'm sure someone knows how.
Ben Voigt
+4  A: 

There is a SystemIcons class, but it need adjustment for WPF needs (i.e. converting Icon to ImageSource).

modosansreves
You completely obviated the need for p/invoke. Good call.
Ben Voigt
by the way, http://stackoverflow.com/questions/1127647/convert-system-drawing-icon-to-system-media-imagesource, this may help
modosansreves
That other thread has about four steps too many. Use http://msdn.microsoft.com/en-us/library/system.windows.interop.imaging.createbitmapsourcefromhicon(v=VS.90).aspx
Ben Voigt
Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Error.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); - Seems to work great, thanks!
RandomEngy