8.1 BRIEF HISTORY OF WIN32 2
8.2 WINDOWS COMPONENTS 3
8.2.1 KERNEL
3
8.2.2 GDI (GRAPHICS
DEVICE
INTERFACE)
3
8.2.2.1 Pens 4
8.2.2.2 Brushes 4
8.2.3 USER
7
8.2.3.1 Clipboard 7
8.3 HANDLES IN WINDOWS 7
8.4 OUR FIRST WIN32 PROGRAM 8
SUMMARY 9
Windows Basics
8.1 Brief History of Win32Before starting windows programming lets
take a short look at the history of Windows.
• In 1983
Windows announced for the
first time in history.
• In November 1985 Windows 1.0 is
launched.
• In April 1987 Windows 2.0 shipped.
• In 1988 Windows/386 emerged out. This
version of Windows supported Multiple
DOS boxes. DOS boxes are
the Consol windows which are enabled to get input or
show output only in text form or character form and no GUI is supported in
this mode.
• November 1989 Win word 1.0 finally
shipped.
• In May 1990 Windows 3.0 shipped. It is
designed to operate in 3 Modes. These are
A. Real Mode or 8086 mode
B. Protected Mode or 286 mode
C. Enhanced or 386 mode with multiple DOS boxes and with support of
Virtual
Memory. In virtual Memory some
of the area on Hard disk is used as system Memory.
• Late 1991 Windows version
3.1 released. This version of Windows came with the
Multimedia extensions that later became the part of Windows standard builds.
• Late 1992 Windows NT beta version
released. And with this version Win32 API also
published. Windows NT offers preemptive
Multitasking.
Multitasking is of two types.
1.
Preemptive Multitasking.
In Preemptive multi tasking, the operating system
parcels out CPU time slices to each program.
2. Cooperative or Non
Preemptive Multitasking. In
this type of multitasking
each program can control the CPU for as long as it needs it. If a program is
not
using the CPU it can allow another program to use it temporarily.
• Summer 1993, Windows NT
version 3.1 is launched. This version of windows is
enabled to run as well, on MIPS and Alpha CPU’s as Intel x86 CPU.
• Summer 1994 Windows version 3.5 is
launched.
• In August 1995 Windows95 shipped.
Windows95 was designed for home computing.
• September 1995 Windows version 3.51
released. And it is considered as the most
solid version of NT for servers.
• Summer 1996, Support for the MIPS and
PowerPC machines are dropped.
• June 1998, Windows98 released with
built in Internet Explorer version 4.
Windows Basics
3
• September 1998, Visual Studio 6.0
released. Visual Studio6.0 consists of three
languages i.e. Visual C++, Visual Basic and Visual J++. Visual studio comes
in three
categories, i.e. learner or student edition, professional and Enterprise
edition.
Visual C++ is a compiler
that will be using in the Windows programming course. This
Visual C++ compiler would be the part of Professional or Enterprise Visual
Studio
package.
• Feb 2000 Windows 2000 released with
major improvements. It is proved to be a much
stable version than the earlier versions of Microsoft Windows series of
Operating
systems. It is also called Windows NT5.
8.2 Windows Components
Microsoft Windows consists of three important components. These are:
1. Kernel
2. GDI (Graphics Device Interface)
3. User
8.2.1 Kernel
Kernel is a main module of the operating system. This provides system
services for
managing threads, memory, and resources.
Kernel has to perform very important responsibilities e.g.
1. Process Management
2. File Management
3. Memory Management (System and Virtual Memory)
In Windows Operating System Kernel is implemented in the form of
Kernel32.dll file.
The Kernel is responsible for scheduling and synchronizing threads,
processing,
exception and interrupts. Loading applications and managing memory. Kernel
is
responsible for the system stability and efficiency.
8.2.2 GDI (Graphics Device Interface)
GDI is a subsystem responsible for displaying text and images on display
devices and
printers. The GDI processes Graphical function calls from a Windows-based
application.
It then passes those calls to the appropriate device driver, which generates
the output on
the display hardware. By acting as a buffer between applications and output
devices, the
GDI presents a device-independent view of the world for the application
while interacting
in a device-dependent format with the device.
Windows Basics
4
GDI is responsible to display application’s graphics objects on Screen and
Printer. The
Applications that use GDI need not worry about Graphics Hardware because GDI
provides the suitable device independent interface. In GDI subsystem,
anything we want
to display or print, there’s Device
context or DC. Device context or DC is a logical term
in windows. Whenever we have to display something, we get DC of display
device, and
whenever we have to print something we get DC of printer device.
GDI is implemented in the form of GDI32.dll. This contains
all the APIs
that need to draw graphics or text objects. We can write text, and draw
rectangles,
polygons, lines, points, etc by using
Pens and Brushes:
8.2.2.1 Pens
A pen is a graphics tool
that an application for Microsoft Windows uses to draw lines and
curves. Drawing applications use pens to draw freehand lines, straight
lines, and curves.
Computer-aided design (CAD) applications use pens to draw visible lines,
hidden lines,
section lines, center lines, and so on. Word processing and desktop
publishing
applications use pens to draw borders and rules. Spreadsheet applications
use pens to
designate trends in graphs and to outline bar graphs and pie charts.
Each pen consists of three attributes: style, width, and color. While no
limits are imposed
on the width and color of a pen, the pen's style must be supported by the
operating
system. These styles are illustrated in the following figure.
Figure 1 Pens types
8.2.2.2 Brushes
A brush is a graphics tool
that a Windows based application uses to paint the interior of
polygons, ellipses, and paths. Drawing applications use brushes to paint
shapes; word
processing applications use brushes to paint rules; computer-aided design
(CAD)
Solid
Dash
Dot
Dash-Dot
Dash-Dot-Dot
Null
Windows Basics
5
applications use brushes to paint the interiors of cross-section views; and
spreadsheet
applications use brushes to paint the sections of pie charts and the bars in
bar graphs.
There are two types of brushes: logical
and physical. A
logical brush is one that
you
define in code as the ideal combination of colors and/or pattern that an
application should
use to paint shapes. A physical brush
is one that a device driver creates, which is based
on your logical-brush
definition.
Brush Origin
When an application calls a drawing function to paint a shape, Windows
positions a
brush at the start of the paint operation and maps a pixel in the brush
bitmap to the
window origin of the client area. (The window origin is the upper-left
corner of the
window's client area.) The coordinates of the pixel, that Windows maps, are
called the
brush origin.
The default brush origin is located in the upper-left corner of the brush
bitmap at the
coordinates (0,0). Windows then copies the brush across the client area,
forming a pattern
that is as tall as the bitmap. The copy operation continues, row by row,
until the entire
client area is filled. However, the brush pattern is visible only within the
boundaries of
the specified shape. (Here, the term bitmap is used in its most literal
sense—as an
arrangement of bits—and does not refer exclusively to bits stored in an
image file).
There are instances when the default brush origin should not be used. For
example, it
may be necessary for an application to use the same brush to paint the
backgrounds of its
parent and child windows and blend a child window's background with that of
the parent
window.
The following illustration shows a five-pointed star filled by using an
application-defined
brush. The illustration shows a zoomed image of the brush, as well as the
location to
which it was mapped at the beginning of the paint operation.
Windows Basics
6
Figure 2 Brush Origin
(Description from MSDN)
Logical Brush Types
Logical brushes come in three varieties: solid, pattern, and
hatched.
A solid brush consists of a color
or pattern defined by some element of the
Windows user interface (for example, you can paint a shape with the color
and
pattern conventionally used by Windows to display disabled buttons).
A hatched brush consists of a
combination of a color and of one of the six patterns
defined by Win32. The following table illustrates the appearance of these
predefined patterns.
Brush Style Illustration
Backward diagonal
Cross-hatched
Diagonally cross-hatched
Forward diagonal
Windows Basics
7
Horizontal
Vertical
Figure 3 Brush Styles
8.2.3 User
User component manages all the user interface elements.
User interface elements include Dialogs, Menus, Text, Cursor, Controls,
Clipboard, etc.
User component is implemented in User32.dll file. You would be familiar with
all the
user interface elements but the new thing for you might be Clipboard.
8.2.3.1 Clipboard
In Windows, data is shareable among applications. For example you are typing
in
Notepad and after you have typed, you want to copy all the text written in
Notepad to
another Editor, say, MS Word. How could this be possible? The answer is:
through
clipboard. Yes, in clipboard, firstly we copy all the data to clipboard and
then paste that
data to MS Word because clipboard is shareable object. All the text or image
data you
have previously copied can now be pasted in other application.
Following are some of the features of clipboard.
• User32.dll manages clipboard.
• Clipboard is used to cut copy and
paste operations.
• Clipboard is temporary storage area. When you
shut down windows, data saved in
clipboard will be lost.
8.3 Handles in Windows
A term handle is a 32 bit number in Win32 environment. It is
normally a type defined as
void *. Handle has an extensive use in Windows. Using handles you can
destroy, release
and take other actions on the object. The basic types of handles in windows
are:
• HANDLE
• HWND
• HINSTANCE
HWND is of type Handle to Window.
HINSTANCE is of type Handle to instance of the application.
Windows Basics
8
Every application has unique identifier in Memory that is called an instance
of the
application.
8.4 Our first Win32 Program
For writing win32 program you should be
familiar of programming concepts in C++.
First we will include header file
windows.h in our source file
because this header contains
prototype of useful APIs that will be used in our windows programs. This
header also
contains some other headers required for commonly used APIs.
#include <windows.h>
Every Windows GUI base program starts its execution from the
WinMain API. And
Every Windows console base program starts its execution from simple
main function.
Here we will be discussing about Windows Graphical User Interface programs.
So we will start our program by writing
WinMain.
int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR
lpCmdLine,int nCmdShow)
{
MessageBox(NULL, “This is the First Windows Program \n Author: Shahid”,
“Virtual Uinversity”, MB_OK|MB_ICONINFORAMTION);
return 0;
}
WinMain:
WinMain is the starting point in Every Win32 GUI programs.
WinMain has four
Parameters these are,
1) First is instance of the current application.
2) Second parameter is also an instance of this application which is used
for the previous
application of the same type that is already running. It is used only in
Window 16bit
editions or Windows 3.1. Windows 32bit editions do not support this
parameter. It is here
just for compatibility.
3) Third parameter is a command line argument of string type which is a type
defined as
char *.
4) Fourth parameter is windows style.
Calling Convention:
CALLBACK is a calling convention which is a type defined as
__stdcall.
Return Value:
In our application WinMain returns 0. WinMain always returns a
value of type integer.
Windows Basics
9
MessageBox:
MessageBox API is used to display a message on screen. It comes
in windows dialog
form and has caption and client area, both of these contains strings. It
also has buttons,
like Ok, Cancel, Yes, No, Retry, Abort. It has four arguments.
1) First is the HANDLE to window, this handle is a parent handle of the
messagebox. In
our case it has no parent handle so we write NULL here that shows that it
has parent
desktop.
2) Second parameter is a string type which print string in the body of the
message box
which is also called client area of the message box
3) Third parameter is also a string type it prints string in a caption of
the message box.
4) Fourth parameter is a buttons that will be displayed in the message box.
We presented
here some of the constants like MB_OK|MB_ICONINFORMATION which display ok
button and icon information on the left side of the message box. These
constants can be
presented here by using bitwise OR operator.
Summary
In this Lecture, we studied about the windows history about when and how
different
editions were released. Then we learnt about the windows components: Kernel,
GDI and
User. Kernel is the heart of Operating system and GDI is a Graphics device
interface in
windows which is used to display and print graphics and text objects. Then
we studied
user component which is responsible to control all the dialogs, menu,
windows, Windows
controls, etc. ‘Handles in windows’ is the introductory part of handles used
in windows.
Handles are 32 bit number that may be void * type which is defined in
‘WinUser.h’
header file. Finally we wrote our first Win32 program. We always write
WinMain in
every windows program because WinMain is the starting point of Windows
Programs.
And we learnt how to display message box in WinMain function and then we
returned
from WinMain with the returned value Zero. And we discussed about the
Message box
API and explained its parameters as well.
Tips: Never use second parameter
(HINSTANCE) of WinMain because it is not used in
recent version of windows. In future lectures, we will discuss how to
determine the
existence of previous application.
Windows Basics
10
Copyright
Some of the course material and Documentation on Microsoft Windows APIs has
been
taken from Microsoft for the preparation of Win32 course. This course has
been designed
and prepared by Virtual University.
Microsoft, Visual C++, Windows, Windows NT, Win32, and Win32s are either
registered trademarks or trademarks of Microsoft Corporation.
|