01 02 first windows program
TRANSCRIPT
-
7/31/2019 01 02 First Windows Program
1/21
FIRST WINDOWS PROGRAM
1
-
7/31/2019 01 02 First Windows Program
2/21
A CHARACTER-MODE MODEL
Basic version of C (Old version)
#include
{
printf ("hello, world\n") ;
}
ANSI version of C(or ANSI C)
#include
int main ()
{
printf ("hello, world\n") ;return 0 ;
}
2
-
7/31/2019 01 02 First Windows Program
3/21
THE WINDOWS EQUIVALENT
#include
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE
hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
MessageBox (NULL, TEXT ("Hello, Welcome to Windows
Programming!"), TEXT ("HelloMsg"), 0) ;
return 0 ;
}
3
-
7/31/2019 01 02 First Windows Program
4/21
THEMECHANICSOFCREATINGAPROGRAMINTHE
VISUAL C++ DEVELOPER STUDIO.
To begin,
select New from the File menu. In the New dialog box, pick the Projects tab. Select Win32 Application. In the
Location field, select a subdirectory.
In the Project Name field, type the name of the project, which in this case isHelloMsg.This will be a subdirectory of the directory indicated in the
Location field. The Create New Workspace button should be checked. The Platforms section
should indicate Win32. Choose OK.
A dialog box labeled Win32 Application - Step 1 Of 1 will appear.
Indicate that you want to create an Empty Proj ect, and press the Finishbutton.
Select New from the File menu again. In the New dialog box, pick the Files tab. Select C++ Source File.
The Add To Project box should be checked, and HelloMsg should beindicated.
Type HelloMsg.c in the File Name field.
Choose OK. 4
-
7/31/2019 01 02 First Windows Program
5/21
STRUCTURALLY, HELLOMSG.C ISIDENTICALTOTHE
ANSI C "HELLO, WORLD" PROGRAM.
The header file STDIO.H has been replaced with
WINDOWS.H,
the entry point main has been replaced withWinMain, and
the C run-time library function printf has been
replaced with the Windows API function MessageBox.
5
-
7/31/2019 01 02 First Windows Program
6/21
THE HEADER FILES
#include
WINDOWS.H is a master include file that includes otherWindows header files, some of which also include other headerfiles. The most important and most basic of these header filesare:
windef.hBasic type definitions.
winnt.hType definitions for Unicode support.
winbase.h Kernel functions.
winuser.hUser interface functions.
wingdi.hGraphics device interface functions.
These header files define all the Windows data types, functioncalls, data structures, and constant identifiers. They are animportant part of Windows documentation. 6
-
7/31/2019 01 02 First Windows Program
7/21
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:
7
int WinMain (HINSTANCE hInstance, HINSTANCEhPrevInstance, PSTR szCmdLine, int iCmdShow)
Or
int
WINAPI
WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nShowCmd
);
-
7/31/2019 01 02 First Windows Program
8/21
PROGRAM ENTRY POINT
The LP prefix stands for "long pointer" and is an artifactof 16-bit Windows.
The WinMainfunction is declared as returning an int.
The four parameters are:
hInstance is something called a "handle to an instance"or "handle to a module."
The operating system uses this value to identify the executable(EXE) when it is loaded in memory.
The instance handle is needed for certain Windows functions
For example, to load icons or bitmaps. 8
-
7/31/2019 01 02 First Windows Program
9/21
PROGRAM ENTRY POINT
hPrevInstancehas no meaning. It was used in 16-bit
Windows, but is now alwayszero.
pCmdLinecontains the command-line arguments asa Unicode string.
nCmdShowis a flag that says whether the main
application window will be minimized, maximized,orshown normally.
9
-
7/31/2019 01 02 First Windows Program
10/21
THEMESSAGEBOXFUNCTION
The MessageBoxfunction is designed to display short
messages.
The little window thatMessageBoxdisplays is actuallyconsidered to be a dialog box, although not one with a
lot of versatility.
10
-
7/31/2019 01 02 First Windows Program
11/21
THEMESSAGEBOXFUNCTION
The first argument to MessageBoxis normally a window
handle.
The second argument is the text string that appears in
the body of the message box, and
the third argument is the text string that appears in the
caption bar of the message box.
In HELLMSG.C, each of these text strings is enclosed in a TEXTmacro.
You don't normally have to enclose all character strings in the
TEXT macro, but it's a good idea if you want to be ready to
convert your programs to the Unicode character set. 11
-
7/31/2019 01 02 First Windows Program
12/21
THEMESSAGEBOXFUNCTION
The fourth argument to MessageBox can be a combination of
constants beginning with the prefix MB_ that are defined in
WINUSER.H.
You can pick one constant from the first set to indicate what
buttons you wish to appear in the dialog box:
12
BUTTUN TYPE CONSTANT VALUES
OK MB_OK 0x00000000L
OK + CANCEL MB_OKCANCEL 0x00000001L
ABORT + RETRY + IGNORE MB_ABORTRETRYIGNORE 0x00000002L
YES+NO+CANCEL MB_YESNOCANCEL 0x00000003L
YES+NO MB_YESNO 0x00000004L
RETRY+CANCEL MB_RETRYCANCEL 0x00000005L
-
7/31/2019 01 02 First Windows Program
13/21
THEMESSAGEBOXFUNCTION
You can also use a constant that indicates the
appearance of an icon in the message box:
13
BUTTUN TYPE CONSTANT VALUES
MB_ICONHAND 0x00000010L
MB_ICONQUESTION 0x00000020L
MB_ICONEXCLAMATION 0x00000030L
MB_ICONASTERISK 0x00000040L
MB_ICONWARNING MB_ICONEXCLAMATION
MB_ICONERROR MB_I CONHAND
MB_ICONINFORMATION MB_ICONASTERISK
MB_ICONSTOP MB_I CONHAND
-
7/31/2019 01 02 First Windows Program
14/21
COMPILE, LINK, AND RUN
When you're ready to compile the program, you can
select Build program.exe from the Build menu, or
press F7, or select the Build icon from the Build
toolbar.
Alternatively, you can select Execute program.exe
from the Build menu, or press Ctrl+F5, or click the
Execute Program icon (which looks like a red
exclamation point) from the Build toolbar. You'll get amessage box asking you if you want to build the
program.14
-
7/31/2019 01 02 First Windows Program
15/21
AN INTRODUCTIONTO UNICODE
Unicode is an extension of ASCII character encoding.
Rather than the 7 bits used to represent each character instrictASCII, or the 8 bits per character that have becomecommon on computers, Unicode uses a full16 bits for
character encoding.
This allows Unicode to represent all the letters,ideographs, and other symbols used in all the writtenlanguages of the world that are likely to be used in
computer communication.
Unicode impacts every part of the computer industry, butperhaps most profoundly operating systems andprogramming languages. 15
-
7/31/2019 01 02 First Windows Program
16/21
A BRIEF HISTORYOF CHARACTER SETS
It is uncertain when human beings began speaking, butwriting seems to be aboutsix thousand years old.
Early writing was pictographic in nature. Alphabets in whichindividual letters correspond to spoken sounds came about justthree thousand years ago.
Although the various written languages of the world served finefor some time, several nineteenth-century inventors saw a needfor something more.
When Samuel F. B. Morse developed the telegraph
between 1838 and 1854, he also devised a code to usewith it. Each letter in the alphabet corresponded to aseries of short and long pulses (dots and dashes).
There was no distinction between uppercase and lowercaseletters, but numbers and punctuation marks had their owncodes.
16
-
7/31/2019 01 02 First Windows Program
17/21
A BRIEF HISTORYOF CHARACTER SETS
Morse code was not the first instance of written languagebeing represented by something other than drawn orprintedglyphs.
Between 1821 and 1824, the young Louis Braille was
inspired by a military system for writing and readingmessages at night to develop a code for embossing raiseddots into paper for reading by the blind. Braille is essentially a 6-bit code that encodes letters, common
letter combinations, common words, and punctuation.
A special escape code indicates that the following lettercode is to be interpreted as uppercase. A special shift codeallows subsequent letter codes to be interpreted asnumbers.
17
http://../Imgs/Glyphhttp://../Imgs/Glyphhttp://../Imgs/Glyphhttp://../Imgs/Glyph -
7/31/2019 01 02 First Windows Program
18/21
AMERICAN STANDARDS
Early computer character codes evolved from the codingused on Hollerith ("do not fold, spindle, or mutilate")cards, invented by Herman Hollerith and first used in the1890 United States census.
A 6-bit character code known as BCDIC ("Binary-CodedDecimal Interchange Code") based on Hollerith coding wasprogressively extended to the 8-bit EBCDIC in the 1 960s andremains the standard on IBM mainframes but nowhere else.
Note: A punched card / punch card / IBM card, orHollerith card is a piece ofstiff paper that contains digitalinformation represented by the presence or absence ofholes in predefined positions.
18
http://../Imgs/500px-Blue-punch-card-front-horiz.pnghttp://en.wikipedia.org/wiki/Paperboardhttp://en.wikipedia.org/wiki/Digitalhttp://en.wikipedia.org/wiki/Digitalhttp://en.wikipedia.org/wiki/Paperboardhttp://en.wikipedia.org/wiki/Paperboardhttp://en.wikipedia.org/wiki/Paperboardhttp://../Imgs/500px-Blue-punch-card-front-horiz.pnghttp://../Imgs/500px-Blue-punch-card-front-horiz.png -
7/31/2019 01 02 First Windows Program
19/21
AMERICAN STANDARDS ASCII
The American Standard Code for InformationInterchange (ASCII) had its origins in the late 1 950s andwas finalized in 1967. Reliability considerations seemed to mandate that no shift
character be used, so ASCII couldn't be a 6-bit code. Cost ruledout the 8-bit version. (Bits were very expensive back then.)
The final code had 26 lowercase letters,
26 uppercase letters,
10 digits,
32 symbols, 33 control codes, and
a space,
for a total of 128 codes.
it appears in theANSI document. 19
-
7/31/2019 01 02 First Windows Program
20/21
ASCII
It is a character-encoding scheme.
Originally based on the English alphabet.
ASCII codes represent text in computers,
communications equipment, and other devices that
use text.
Most modern character-encoding schemes are based
on ASCII, though they support many more characters
than ASCII does. 20
http://../Imgs/ASCII_Code_Chart.svg.pnghttp://../Imgs/ASCII_Code_Chart.svg.png -
7/31/2019 01 02 First Windows Program
21/21
ASCII
All 128 ASCII characters, including non-printable
characters (represented by their abbreviations). The 95 ASCII graphic characters are numbered from
20hex to 7Ehex (decimal 32 to 126). The space
character is considered a non-printing graphic. 21
http://../Imgs/ASCII_Code_Chart.svg.pnghttp://../Imgs/ASCII_Code_Chart.svg.png