Week 2 Getting Started With Windows Programming
Week 2 Getting Started With Windows Programming
1
Setting up Visual Studio for a Windows project (1)
• Start Visual Studio.net. Select 'File', then 'New Project'
2
Setting up Visual Studio for a Windows project(2)
• You will see the following screen.
Choose 'Win32 Project' and choose a name for the project (in this case
“Window GUI”). Then select OK. 3
Setting up Visual Studio for a Windows project(3)
• You will see the following screen.
Select ‘Next'
4
Setting up Visual Studio for a Windows project(4)
• You will see the following screen.
Right click on 'Source files' and select 'Add -> New Item'
6
Setting up Visual Studio for a Windows Program (2)
• You will see the following screen.
Choose 'C++ File' and enter a name (in this case “main”) into the name
field. 7
Setting up Visual Studio for a Windows Program (3)
• You will see the following screen.
8
Writing the program
• The Windows equivalent to the "hello, world" program has
exactly the same components as the character-mode version.
It has an include statement, a program entry point, a function
call, and a return statement. Here's the program (Write the
program in the “main.cpp” edit tab area:
/*--------------------------------------------------------------
HelloMsg.c -- Displays "Hello, Windows Programming!" in a message box (c)
Charles Petzold, 1998
--------------------------------------------------------------*/
#include <windows.h>
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MessageBox (NULL, TEXT ("Hello, Windows Programming!"), TEXT ("HelloMsg"),
0) ;
return 0 ;
}
9
Writing the program (2)
• You will see the following screen.
10
Running the program
• Press F5 to build and RUN the program. The program will
compile and if there is no error, the following Dialog Box will
emerge which require a response from user. (Assuming there
is no Error: try solving any errors (normally syntax) and press
F5 again).
14
WinMain Function as Seen by Windows OS
• The WinMain function is declared as returning an int.
– When it exit OS will receive an integer number.
• The first parameter to WinMain is something called an "instance
handle."
– a number that an application uses to identify something, in this case the
program.
• The second parameter to WinMain is always NULL (defined as 0).
• The third parameter to WinMain is the command line used to run
the program.
• The fourth parameter to WinMain indicates how the program
should be initially displayed—either normally or maximized to fill
the window, or minimized to be displayed in the task list bar
• Discussed more in Chapter 3.
15
The MessageBox Function
• The MessageBox function creates a window, but it is a special-purpose
window of limited flexibility.
– The message box window has a title bar with a close button, an optional
icon, one or more lines of text, and up to four buttons.
– However, the icons and buttons must be chosen from a small collection that
Windows provides for you.
• 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
boxlpCaption - is the title of the message box
• uType - defines various properties like the icon and types on input
(eg, Yes, No, Cancel, etc..)
16
uType of MessageBox Function:
Input button
• The fourth argument to MessageBox can be a combination of
constants beginning whose ID identifier with the prefix MB_ that are
defined in WINUSER.H as follows:
#define MB_OK 0x00000000L
#define MB_OKCANCEL 0x00000001L
#define MB_ABORTRETRYIGNORE 0x00000002L
#define MB_YESNOCANCEL 0x00000003L
#define MB_YESNO 0x00000004L
#define MB_RETRYCANCEL 0x00000005L
17
Additional constant in the forth parameter
• You can use the C OR (|) operator to combine one of the
constants shown above with a constant that indicates which
of the buttons is the default:
#define MB_DEFBUTTON1 0x00000000L
#define MB_DEFBUTTON2 0x00000100L
#define MB_DEFBUTTON3 0x00000200L
#define MB_DEFBUTTON4 0x00000300L
• You can also use a constant that indicates the appearance of
an icon in the message box:
#define MB_ICONHAND 0x00000010L
#define MB_ICONQUESTION 0x00000020L
#define MB_ICONEXCLAMATION 0x00000030L
#define MB_ICONASTERISK 0x00000040L
18
Icon before the Text string (declared in second parameter)
ICON ID label
MB_ICONQUESTION
MB_ICONWARNING
MB_ICONINFORMATION
MB_ICONERROR
19
Return value of MessageBox Function
Type: int
Return code/value Description
IDABORT
• If a message box has a Cancel 3
The Abort button was selected.
button, the function returns IDCANCEL
The Cancel button was selected.
2
the IDCANCEL value if either
IDCONTINUE
the ESC key is pressed or the The Continue button was selected.
11
Cancel button is selected. If the IDIGNORE
The Ignore button was selected.
5
message box has no Cancel IDNO
The No button was selected.
button, pressing ESC has no 7
IDOK
effect. 1
The OK button was selected.
• If the function fails, the return IDRETRY
The Retry button was selected.
value is zero. To get extended 4
IDTRYAGAIN
error information, call 10
The Try Again button was selected.
GetLastError. IDYES
The Yes button was selected.
6
• If the function succeeds, the
return value is one of the
following menu-item values. 20
Week 2 Example!
#include <windows.h>
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
int nResult=MessageBox(NULL,
L"An example of Cancel,Retry,Continue", //Q1: What is the difference
//between using this L prefix
TEXT("Week 2 Example!"), //and this TEXT prefix
MB_ICONERROR|MB_ABORTRETRYIGNORE);//Q2: What does this “|” operator
//implements on the parameter.
switch(nResult)
{
case IDABORT:
// 'Abort' was pressed
break;
case IDRETRY:
// 'Retry' was pressed
break;
case IDIGNORE:
// 'Ignore' was pressed
break;
}
return 0 ;
21
}
Display of Exercise 1
• The display of the program will be as follows: