Welcome to MSDN Blogs Sign in | Join | Help

MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Commenter 8 wants to know how to tell the MessageBoxIndirect function to use an existing HICON instead of pointing it to an icon resource.

You can't.

The MessageBoxIndirect loads the icon itself. You can't hand it a pre-loaded icon.

Of course, it's hardly rocket science to write your own MessageBoxWithMyIcon function that lets you use whatever icon you want. There's no law that says all Yes/No dialogs must use the MessageBox function. Feel free to write your own.

The MessageBox and MessageBoxIndirect functions are just convenience functions. They don't create new functionality; they don't do anything you couldn't already do yourself. You can have a template dialog box that you use for "generic" purposes and set the icon and text yourself. Or, if you're really adventuresome, you can generate a dialog template on the fly.

The MessageBox and MessageBoxIndirect functions never aspired to be "everything anybody could ever do with a dialog box." They just provide some basic functionality that lots of people find useful. If you need more functionality, then you can always write it yourself. (There's already a function for "everything anybody could ever do with a standard Win32 dialog box": It's called, um, DialogBox.)

Windows Vista introduces a considerably more customizable "message box"-type dialog known as a Task Dialog; you may want to give that one a try.

Published Monday, July 21, 2008 7:00 AM by oldnewthing
Filed under:

Comments

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 10:55 AM by Nish

For those who are really insistent on using the native message box but with a custom icon, you could go for a CBT hook. Probably overkill on Vista where you have task dialogs (that Raymond mentioned)

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 11:22 AM by jiangsheng

For information, there is a MessageBoxIndirect function which accepts a customized icon in the parameter.

No such luck with MFC's AfxMessageBox, though.

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 11:26 AM by jiangsheng

oops, scratch that, did not read the line in the middle.

Still wondering why AfxMessageBox does not have a pwndParent parameter.

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 11:26 AM by Nish

Hey jiang sheng,

Raymond's post is about the fact that MessageBoxIndirect won't accept an HICON. My suggestion to use a CBT hook was so people could use an existing HICON.

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 11:27 AM by J. Daniel Smith

Several years ago I tried to replicate MessageBox() with my own code.  Maybe I was missing something obvious, but I didn't find it to be very easy: MessageBox() has lots of little details that are fairly noticeable if you don't match them exactly.

In the end, I ended up going with a hook too.

While I know you (Raymond) don't make the decisions about such things (and I'm sure the Microsoft lawyers have a habit of making even seemingly simple things incredibly complex), if MessageBox() is "just [a] convenience function", then why isn't the source code readily available to the average developer?

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 12:03 PM by Keeron Modi

Daniel, if you have access to VS 2008, you could try using the recently released update to VS 2008 that allows debugging right into the .NET framework's source code.

http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx

(the post is a bit old, and I haven't tried it recently myself either...but it worked like a charm before).

If the above works, you can look into the MessageBox code. Although, note that the code is only for debugging purposes and there's some copyright involved in copying that.

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 12:09 PM by Neil

If you were missing something obvious, then so was I (and I was only trying to replicate it on Windows 3.1, which should have been simpler), but it took me about 70 lines of code to generate a dialog item template from the message box parameters.

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 12:37 PM by J. Daniel Smith

Keeron: I think you'll find that the managed MessageBox() is just a thin wrapper around the Win32 call.

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 12:47 PM by jeffdav

But can I write my own dialog?

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 12:53 PM by Paul M. Parks

One of my favorite features of message boxes is the ability to press Ctrl-C and copy the text of the message box to the clipboard. I find this to be a lot more useful than taking a screen shot of the window. Hopefully, anyone that decides to create his own MessageBox replacement would have the good manners to replicate this feature. I get really grumpy when I encounter a "fake" message box that doesn't implement this.

PMP

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 12:58 PM by peetm

I too use a CBT hook to customize MessageBox - like changing the button text (although that's easy anyway), the icon etc.  I also have an option where it auto times-out :- it 'answers' with whatever button has the focus [if one does].  This has been very popular so far, i.e., no complaints like 'where'd the Message Box go?!'

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 3:15 PM by Dan

Coincidentally, here's a code sample on how to hook a dialog box so you can change stuff in it:

http://www.garry.tv/?p=588

This example changes button text.

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Monday, July 21, 2008 5:38 PM by Ulric

>One of my favorite features of message boxes

>is the ability to press Ctrl-C

>I get really grumpy when I encounter a "fake" message box that doesn't implement this.

right on, had that problem here too when we tried to replace the assert() dialog.

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Tuesday, July 22, 2008 10:38 AM by AsmGuru62

"I also have an option where it auto times-out"

I would have complained about this one! What if at the time of showing this message box I have to pick up my cell phone and talk a few minutes?!..

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Tuesday, July 22, 2008 12:18 PM by 8

Sorry again for my late reply, but thank you very much!

In the meantime, however, I already did write my own MessageBox function, which also implements the ability to use verbs on the buttons (like on Mac OS). And yes, I've looked at Task Dialogs too. I'm even looking into backporting them so I can also add it to wine.

The reason I asked at the time was because I was writing a very simple application which didn't involve any "magic" (a dialog, a socket... wow). But today I don't even work for the same company anymore.

@Paul: Thanks, I didn't know that. I'll fix that in my version right away :)

# re: MessageBoxIndirect loads the icon itself; you can't hand it a pre-loaded icon

Wednesday, July 23, 2008 9:23 AM by MadQ

Nitpick: >you can't hand it a pre-loaded icon

Sure you can! Just LoadImage it with the LR_SHARED flag beforehand. Not that that solves anything.

This was today's YB-style comment.

# TaskDialog under Win9x/XP

Wednesday, July 23, 2008 2:48 PM by Helmut Buhler

You might also consider using a TaskDialog wrapper that also works on Windows prior to Vista if you don't want to require Vista. See the cpp/h file I have done in the link above.

New Comments to this post are disabled
 
Page view tracker