Windows programming with Visual C++

advertisement
Windows programming with Visual C++
Windows programming using a graphical user interface (GUI) instead of a commandline interface found in console applications.
Goal to implement C++ code using the Windows Application Programming Interface
(API), which is a library of methods and code providing access to the operating system.
To create a GUI, your code must call operating system functions, using the API. A GUI
is a graphically based environment consisting of familiar items such as windows, menus,
buttons, icon, mouse control, etc. Windows programs run in floating windows and
include the standard buttons for minimize, maximize, and close, as well as title bars,
scroll bars and resizability.
Computers are classified according to how many bits (binary digits: 0,1) can be
transmitted simultaneously into the microprocessor (CPU). The bus or data bus is the
electronic pathway that conveys signals to the CPU from memory and other internal
devices. The wider the bus (in bits) the more information can be sent simultaneously to
the microprocessor and the faster a program runs.
The Pentium family has 64-bit wide data buses. The various Windows operating systems
are specifically designed to work with the various data buses (16-, 32-, or 64-bit).
Windows 2000 supports a 64-bit bus, while older versions support 32- or 16-bit bus
architecture.
A separate Windows API exists for each Windows generation. The 32-bit operating
systems support the Win32 API, which means a program written for NT, 95, 98, and CE
should run on all these platforms. Note that there is generally not backward compatibility
with earlier versions of Windows operating systems.
A major goal of Windows 2000 was that 64-bit applications should run on a 32-bit
operating system. To this end, Microsoft made the Win64 API almost identical to the
Win32 API, except that 64-bit applications can run on 32-bit versions of the operating
system. Because of the similarity, the term “Windows API” refers to either the Win32 or
Win64 API.
About the Windows API
The windows.h header includes all the functions, variables, and other programming
elements that make up the Windows API. This file is also part of the Microsoft Platform
Software Developer Kit (SDK), or Platform SDK, which includes headers, sample code,
tools, and documentation for building applications using the API.
The API defines its own data types, written in uppercase characters, such as CHAR, INT,
and BOOL, and hundreds more (most do not have counterparts in C/C++).
A pointer data type (beginning with prefix P or LP) declares the type and name of an
API pointer. A handle (prefix H) is similar to a pointer, but is typically used to control
and manipulate (handle) a window. All individual components (buttons, menus, etc.) are
considered as windows and require their own handle. A child window appears within an
area defined by a parent window, which is a primary application containing one or more
child windows.
A How to Create a WinMain() Function
The WinMain() function is the starting point for API programs. Its declaration is
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
WINAPI is an API type required in all API functions. The first two parameters are of
HINSTANCE type, meaning they are instance handlers. An instance is a copy of a
window, program, or any object that happens to be running . You can have multiple
copies of a program running, so each needs its own handle. The second parameter is a
handle to a previous instance. (Not used after Windows 3.1). The third parameter is a
string pointer to the command line string that executes the program, e.g.,
c:\MyProjects\calculator.exe. This string might include additional information (e.g., a
password) that needs to be sent to initiate a program. The last parameter is an integer that
represents the startup mode of the window (minimized, maximized, etc.). Window
constants begin with the WS_ prefix, as in WS_NORMAL or WS_MAXIMIZE .
Aside from being the entry point for all Windows applications, the WinMain() function
has the job of creating and instantiating windows.
There are three steps:
1. defining and registering the window (using RegisterClass();
2. creating a parent and child windows (using CreateWindow();
3. displaying the program (using ShowWindow()).
Events and Messages
Windows programs are called event-driven.
An event is a specific circumstance that is monitored by Windows.
Typical events include responding to a mouse click, redrawing a previously obscured
window, or responding to a button click.
When an event occurs, Windows sends a message to the program that is associated with
the event.
A message is a set of instructions, such as where and when the event occurred. You refer
to messages in code using any of the hundreds of predefined Windows message constants
(prefix WM_).
For example, the WM_COMMAND message is generated for events involving menus
and buttons, such as a button click.
Every 32-bit Windows application has its own message queue where messages are
placed (in order of arrival) until they are processed by the application.
The processing involves writing a message loop to retrieve the message and adding code
to handle the message. A message loop continually checks the queue for new messages
and sends them to the appropriate procedure.
This typically involves 3 function calls:
1. GetMessage,
2. TranslateMessage
3. Dispatchmessage.
A window procedure, or windproc, is a special function that processes any messages
received. Windprocs are sometimes called callback functions because they give
Windows a way of “calling back” your application once it generates an event and
message.
Your program “calls” Windows every time it generates an event and Windows calls back
your application and gives it the message in the queue.
The message loop then forwards the message to the windproc that Windows is trying to
reach (or call back). The windproc is the heart of any API program because that is where
the program’s functionality resides.
RESEARCH TOPICS
 What is the difference between event-driven programming and the programs you
have been writing so far?
 What is the hardware /software platform you are using at home and at school?
 What is the difference between a pointer and a handle?
 What is a callback function?
Download