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.

Comments (17)
  1. Nish says:

    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)

  2. jiangsheng says:

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

    No such luck with MFC’s AfxMessageBox, though.

  3. jiangsheng says:

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

    Still wondering why AfxMessageBox does not have a pwndParent parameter.

  4. Nish says:

    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.

  5. 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?

  6. Keeron Modi says:

    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.

  7. Neil says:

    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.

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

  9. jeffdav says:

    But can I write my own dialog?

  10. 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

  11. peetm says:

    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?!’

  12. Dan says:

    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.

  13. Ulric says:

    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.

  14. AsmGuru62 says:

    "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?!..

  15. 8 says:

    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 :)

  16. MadQ1 says:

    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.

  17. 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.

Comments are closed.

Skip to main content