January 27, 2021
Hot Topics:

Windows API Tutorial - Part One

  • By Karl Moore
  • Send Email »
  • More Articles »

The final argument in the Message Box API declaration is admittedly a little confusing. You're supposed to pass it a number that dictates how the message box should look.

Hmm, let's unravel the mystery now:

  • Switch back to the API Viewer
  • Change the API Type to 'Constants'
  • Scroll down to the constants starting with 'MB'

These are all the Message Box constants, meaning they're words that represent a number. And you can pass these to the wType argument to tell it how it should look.

For instance, can you see the 'MB_ABORTRETRYIGNORE = &H2&' line? Try double-clicking on it. You should see this appear in the 'Selected Items' box:


If we add this line to our module, we could pass the word M_ABORTRETRYIGNORE as the wType parameter. Visual Basic translates this into &H2&, which is just a special number in disguise. The API knows that this number means it should displays an Abort/Retry/Ignore box so it does!

Let's add a few of those constants now:

  • Add the following code to your module:
Public Const MB_ABORTRETRYIGNORE = &H2&Public Const MB_YESNO = &H4&Public Const MB_YESNOCANCEL = &H3&Public Const MB_RETRYCANCEL = &H5&Public Const MB_OKCANCEL = &H1&Public Const MB_OK = &H0&Public Const MB_ICONSTOP = MB_ICONHANDPublic Const MB_ICONQUESTION = &H20&Public Const MB_ICONASTERISK = &H40&Public Const MB_ICONEXCLAMATION = &H30&

These are all constants uncovered via the API Viewer.

Top Tip: If you were researching this function yourself, you'd find information on the existence of such constants at one of the sources mentioned so far perhaps a book or API reference site.

Now we've added this code, let's try it all again:

  • Behind your Form's Command Button, change the code as follows:
Call MessageBox(Form1.hwnd, "Did Video Kill the Radio Star?", _"Whodunnit?", MB_ICONEXCLAMATION)

Here, we're telling the API to display the same message box but with a question icon, as represented by the MB_ICONQUESTION constant.

  • Press F5 and test your code

You should see the message displayed, alongside a question mark bubble.

  • Try changing that parameter from MB_ICONQUESTION to:

See what happens? Even though you're passing it these two separate items, the API interprets it and realises it should display a yes/no message box with a question bubble icon. (See top tip at bottom of page regarding this)

Hold on a minute. Did Video Kill the Radio Star? If the user clicks No, understandably nothing happens. But if the user clicks Yes, then I think it's time to call in the cops.

In fact, it'd also be pretty cool if we could notify the judge, arrange a trial date, automatically inform members of the jury and schedule a press conference but we'll save that for next week.

So how do we figure out exactly what the user pressed? Well don't forget this is a function that returns a number (Long). And it's that Long value that specifies exactly what the user clicked.

  • Alter the code behind your Command Button as follows:
Print MessageBox(Form1.hwnd, "Did Video Kill the Radio Star?", _"Whodunnit?", MB_YESNO Or MB_ICONEXCLAMATION)

Note that I've changed 'Call' to 'Print' which will basically print the Long returned by the MessageBox function onto your form.

  • Now run your application and try clicking 'Yes'

The form displays 6.

  • And now try clicking 'No'

...and the form displays 7. Great! We already know a couple of responses. A few others are listed in the API Viewer, starting with the letters 'ID'.

We're now going to throw those constants into our module to make life easier:

  • Add the following constants to your module:
Public Const IDYES = 6Public Const IDNO = 7Public Const IDABORT = 3Public Const IDCANCEL = 2Public Const IDIGNORE = 5Public Const IDRETRY = 4Public Const IDOK = 1

Getting bored? Me too. Let's just get this darn thingy up and running:

  • Enter the following code behind your Command Button:
Private Sub Command1_Click()If MessageBox(Form1.hwnd, "Did Video Kill the Radio Star?", _"Whodunnit?", MB_YESNO Or MB_ICONQUESTION) = IDYES Then   Call MessageBox(Form1.hwnd, "The police will be round in five", _   "Thanks for the Tip-Off", MB_OK Or MB_ICONEXCLAMATION)End IfEnd Sub

Here, we're displaying a message box with a question icon and yes/no buttons. If the user clicks on yes, the function returns IDYES (the number 6) and so the code then goes on to display a second message box thanking the user and displaying a pretty yellow exclamation mark. Ahh, ain't that cute? ;-)

Top Tip: It might sound weird to type 'MB_OK Or MB_ICONEXCLAMATION' rather than 'MB_OK And MB_ICONEXCLAMATION' but don't worry about it. It's to do with something called bitwise comparison. And it's an awful subject, so I won't elucidate. Just remember that when putting two or more things together for the API, use 'Or' as the sticky tape.

Note: A download of this Message Box example is available here.

Page 5 of 6

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date