Monday, 28 January 2013

Beginning Windows programming in C

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: Windows
Include 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.

Windows Win32 message box

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_ICONQUESTIONMB_ICONQUESTION
MB_ICONWARNINGMB_ICONWARNING
MB_ICONINFORMATIONMB_ICONINFORMATION
MB_ICONERRORMB_ICONERROR

The other main elements of interest are the types input buttons available to the user (ie, Yes, No, Cancel, etc..)

MB_ABORTRETRYIGNOREAbort, Retry, and Ignore
MB_CANCELTRYCONTINUECancel, Try Again, and Continue
MB_HELPHelp
MB_OKOK
MB_OKCANCELOK and Cancel
MB_RETRYCANCELRetry and Cancel
MB_YESNOYes and No
MB_YESNOCANCELYes, 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.

IDABORTAbort button was pressed
IDCANCELCancel button was pressed
IDCONTINUEContinue button was pressed
IDIGNOREIgnore button was pressed
IDNONo button was pressed
IDOKOK button was pressed
IDRETRYRetry button was pressed
IDTRYAGAINTry Again button was pressed
IDYESYes 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


Next tutorial

Tutorial 2 - Creating a basic window

 

No comments:

Post a Comment