Beginning Windows programming
http://www.win32developer.com/tutorial/windows/windows_tutorial_1.shtm
Welcome to the world of Windows, or also known as Win32, programming.
Programming under windows can be an initially daunting task.
Within the next few tutorials, you should have a better
understanding of how to program on the Win32 platform.
These tutorials assume a basic understanding of C or C++ and are
designed to compile and run under Visual Studio.net 2005 and 2008.
Windows programming is very different to programming a console
application. But, it certainly can be an exciting and rewarding
challenge
to take up. Once a few fundamentals are learnt you will be well on
your way. If I can do it, so can you.
Important note: These tutorials use Multi-Byte character set. If you use Unicode these projects will not compile (without minor alteration).
If you are unsure, please setup your project as per 'Pre-requisites 2' on the main tutorial page.
If you have any questions or suggestions. Please feel free to email
me (see the 'Contact Me' page) or post a discussion in the
forums.
Prerequisites
Project type: WindowsInclude files: windows.h
Library files: N/A
The most basic Windows program - The 'Message box'
The first thing I should point out is that, with console applications the entry point was something like so;int main(int argc,char *argv[])
In a Win32 application the entry point to a program is more like;
INT WINAPI wWinMain(HINSTANCE hInst,
HINSTANCE hPrevInst,
LPWSTR lpCmdLine,
INT nShowCmd)
The only real differences are that the first parameter holds the instance of the program being executed, the second parameter is the previous instance, which isn't really used any more as it was for compatibility with really old applications in the 16 bit days. The third parameter holds any command line arguments (eg. 'application.exe -SomeSwitches') and the last parameter tells the program how to start, ie. maximised, minimised, etc..
Now we have that out of the way we can move on...
What better way tho start our Win32 programming journey than a really simple Windows application. We will go through the steps in creating a simple message box. Message boxes are handy as they can alert the user to important information, for example error messages, simple notifications, or even as the user a question.
An example of a message box can be seen below.
Creating a Windows message box is achieved by using the MessageBox() function. To replicate the message box seen above we would type the following;
MessageBox(NULL,"Do you really want to continue?","Are you sure?",MB_ICONQUESTION);
The message box function is defined as follows;
int MessageBox(HWND hWnd,LPCTSTR lpText,LPCTSTR lpCaption,UINT uType);
hWnd - is the handle to the window that you are working with
lpText - is the text that is to be displayed in the message box
lpCaption - is the title of the message box
uType - defines various properties like the icon and types on input (eg, Yes, No, Cancel, etc..)
The last parameter is of particular interest as this is the main influence on how the message box looks and acts. In the example above we used 'MB_ICONQUESTION' to tell the message box to display the question mark icon. We could have used any of the following types;
MB_ICONQUESTION
MB_ICONWARNING
MB_ICONINFORMATION
MB_ICONERROR
The other main elements of interest are the types input buttons available to the user (ie, Yes, No, Cancel, etc..)
MB_ABORTRETRYIGNORE | Abort, Retry, and Ignore |
MB_CANCELTRYCONTINUE | Cancel, Try Again, and Continue |
MB_HELP | Help |
MB_OK | OK |
MB_OKCANCEL | OK and Cancel |
MB_RETRYCANCEL | Retry and Cancel |
MB_YESNO | Yes and No |
MB_YESNOCANCEL | Yes, No, and Cancel |
To define what icons and message buttons we want to use at the same time, we just separate the options with |.
For example MB_ICONQUESTION|MB_OKCANCEL will display the question mark icon with the buttons labelled 'OK' and 'Cancel'.
The message box function will return the value of the button that was pressed. The return values are actually integers, but it is best to use the 'defines' as listed below for readability and fault finding.
IDABORT | Abort button was pressed |
IDCANCEL | Cancel button was pressed |
IDCONTINUE | Continue button was pressed |
IDIGNORE | Ignore button was pressed |
IDNO | No button was pressed |
IDOK | OK button was pressed |
IDRETRY | Retry button was pressed |
IDTRYAGAIN | Try Again button was pressed |
IDYES | Yes button was pressed |
There are many other parameters related to text formatting and so on, which is best left visiting MSDN (linked at bottom of page).
In the next tutorial we will move on to basic Window creation and progress to more involved applications down the track.
The Full Code
#include <windows.h>
INT WINAPI wWinMain(HINSTANCE hInst,
HINSTANCE hPrevInst,
LPWSTR lpCmdLine,
INT nShowCmd)
{
int nResult=MessageBox(NULL,
"An example of Cancel,Retry,Continue",
"Hello Message Box!",
MB_ICONERROR|MB_ABORTRETRYIGNORE);
switch(nResult)
{
case IDABORT:
// 'Abort' was pressed
break;
case IDRETRY:
// 'Retry' was pressed
break;
case IDIGNORE:
// 'Ignore' was pressed
break;
}
return 0;
}
Things to try
Try adding another message box in the switch cases to display a response to the button being pressed. Don't forget to visit the forums to post your solutions!Additional information
For additional information we have provided the following links.Microsoft (MSDN) - Message boxes
No comments:
Post a Comment