Custom WinForm with MessageBox Icon and Sound
Asked Answered
O

2

8

I want to create a modal dialog that has more controls than what a standard .NET MessageBox offers you. I've created my own Windows Form that will be called with ShowDialog() to give the modal behavior. However, I'd like to utilize the graphics that come with MessageBox via MesageBoxIcon. Is this possible? Is it also possible to replicate playing the error/warning windows sounds associated with the message box icons (as they are set in the user's windows settings)?

Olibanum answered 28/3, 2012 at 16:16 Comment(0)
C
15

See System.Drawing.SystemIcons class to display the system icons the MessageBox class uses, such as Question, Information and Warning.

e.Graphics.DrawImage(SystemIcons.Question.ToBitmap(), new Point(0, 0));

For the sounds, see the System.Media.SystemSounds class to play the associated sounds.

System.Media.SystemSounds.Asterisk.Play();
Chalcopyrite answered 28/3, 2012 at 16:21 Comment(3)
Just wanted to add that this was a nice reference for drawing the system icon: codeproject.com/script/Articles/ViewDownloads.aspx?aid=154680Olibanum
You need to use Graphics.DrawIcon(), or some variation, as SystemIcons returns Icon, not Image, but you're essentially there.Olibanum
@StealthRabbi Oops. Updated code. Yes, DrawIcon or use the ToBitmap() function.Chalcopyrite
R
1

MessageBox is provided by the OS I'm afraid. You can extend it, but it requires a lot of work (see this CodeProject article for a tutorial). Your best bet is probably to start again with a control inheriting from Form as you suggest.

To access the icons, it's as simple as using the System.Drawing.SystemIcons class (documentation for that is here.)

Ream answered 28/3, 2012 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.