0% found this document useful (0 votes)
19 views

Week 2 Getting Started With Windows Programming

The document discusses setting up a Windows GUI project in Visual Studio. It describes the steps to create a new project, add source files, and write and run a simple "Hello World" Windows program using MessageBox. The program demonstrates the basic structure of a Windows program, including the #include directive, WinMain entry point, and use of the MessageBox function to display a message. It also explains some of the parameters used in WinMain and MessageBox.

Uploaded by

fatah.ozil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Week 2 Getting Started With Windows Programming

The document discusses setting up a Windows GUI project in Visual Studio. It describes the steps to create a new project, add source files, and write and run a simple "Hello World" Windows program using MessageBox. The program demonstrates the basic structure of a Windows program, including the #include directive, WinMain entry point, and use of the MessageBox function to display a message. It also explains some of the parameters used in WinMain and MessageBox.

Uploaded by

fatah.ozil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Computer System

and Multimedia (Week 2)


Getting Started With Windows
Programming
Textbook: Programming Windows / Charles Petzold. -- 5th ed.
Chapter 1. Getting Started

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.

Select 'Empty project' and click 'Finish'


5
Setting up Visual Studio for a Windows Program(1)
• After the Visual Studio IDE opens, in the Solution Explorer window, Right click on 'Source files'
and select 'Add -> New Item'
• 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).

• Press OK and the program will exit 11


About the main.cpp Program in the
Windows GUI Project
• Structurally, HELLOMSG.C is identical to the K&R
"hello, world" program.
• The header file STDIO.H has been replaced with
WINDOWS.H, the entry point main has been
replaced with WinMain, and the C run-time library
function printf has been replaced with the
Windows API function MessageBox.
• However, there is much in the program that is new,
including several strange-looking uppercase
identifiers.
12
The Header Files
• HELLOMSG.C begins with a preprocessor directive that you'll find at
the top of virtually every Windows program written in C:
#include <windows.h>
• WINDOWS.H is a master include file that includes other Windows
header files, some of which also include other header files. The
most important and most basic of these header files are:
windef.h Basic type definitions.
winnt.h Type definitions for Unicode support.
winbase.h Kernel functions.
winuser.h User interface functions.
wingdi.h Graphics device interface functions.
• These header files define all the Windows data types, function
calls, data structures, and constant identifiers. They are an
important part of Windows documentation.
13
Program Entry Point
• Just as the entry point to a C program is the function main, the
entry point to a Windows program is WinMain, which always
appears like this:

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR


szCmdLine, int iCmdShow)

• This entry point is documented in /Platform SDK/User Interface


Services/Windowing/Windows/Window Reference/Window
Functions. It is declared in WINBASE.H like so (line breaks and
all):

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR


lpCmdLine, int nShowCmd );

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

• In the program we pun NULL which is the MB_OK input


button.

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)

The icon are as follows shown respectively to the ID identifier:

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:

• The program does logical dicision but just


exit.
• Answer the Question in the comment field
with prefix Q. 22
Programming Exercise Week 2
Update the program shown in on week 2 example such that it
will display the following respective message and icon in a new
message box with only a OK button, if the respective button is
selected and then .

Button Icon Displayed


Message in new message box
Selected
IDABORT Abort button was pressed
IDRETRY Retry button was pressed
IDIGNORE Ignore button was pressed
For Lecturer press here for solution
23

You might also like