You are on page 1of 19

Introduction to Windows Programming

Windows Programming is the C programming language in which the


Microsoft Windows Operating System is written. In the world, most
of the developers use C windows programming to develop software
than Object-Oriented languages

In this topic, we will learn about the Introduction to Windows.


Microsoft Windows is a multitasking operating system developed by
Microsoft Corporation, which uses Graphical User Interface to
interact with users. Microsoft was initially named “Traf-O-Data” in
1972, was renamed “Micro-soft” in November 1975, and then
“Microsoft” on November 26, 1976. Microsoft entered the
marketplace in August 1981 by releasing version 1.0 of the operating
system Microsoft DOS (MS-DOS), a 16-bit command-line operating
system. Bill Gates and Paul Allen founded Microsoft, and the
Windows operating system has been its primary product.

Types:

 Graphics Device Interface (GDI) and GDI+


 Application Programming Interface (API) Messaging Application
Programming Interface (MAPI) Remote Application Programming
Interface (RAPI) Speech Application Programming Interface (SAPI)
Telephony Application Programming Interface (TAPI)
Event-Driven Programming:

Most modern computer programs that people use have Graphical


User Interfaces (GUIs).
A GUI has icons on the computer screen and a mouse (or other
device) to control a pointer that can be used to operate the computer.

An event occurs whenever an event listener detects an event trigger


and responds by running a method called an event handler.
Modern operating systems and programming languages contain
facilities to let programmers set up event listeners

An event trigger can be almost any activity or condition selected by


the programmer, such as a mouse movement, someone pressing the
enter key, or a bank account balance changing.
An event handler is a method that is activated when the event trigger
occurs.
Almost any method can serve as an event handler, such as those that
play sounds, initiate data communications, or perform calculations.

• Events can be used to control objects moving in 3D space.


– Detect when some object moves
– Detect when two objects collide
– Make objects move in response to mouse or keyboard
commands.
• So before discussing events in Alice, let’s talk about 3D space a
little more.
In Alice:

• Events can be used to control objects moving in 3D space.


– Detect when some object moves
– Detect when two objects collide
– Make objects move in response to mouse or keyboard
commands.
• So before discussing events in Alice, let’s talk about 3D space a
little more.

3D space:

• A dimension is a way of measuring something.


• We create a dimension whenever we assign a value on a
continuous scale to some property.
• Example: On a scale of 1 to 100, how much do you approve of
George Bush’s performance as president?
Measuring distance:

• If you wish to measure the location of a point on a straight line,


then you only need one number.
• You could mark a start point on the line, and then measure how
far a point is from the start point
• By using negative and positive numbers, you could also indicate
which direction the distance spans.

Orientation

• In addition, to the concepts of distance and direction, we also


have the concept of orientation.
• Orientation means the direction the object is facing.
• If an object is facing the positive direction on the line, its
orientation is forward.
If facing negative, then backward

Position in 2D

• On a flat surface, like a sheet of paper, you need two values to


specify an object’s position (two dimensions).
• Rene Descarte developed a system of quantification for two
dimensions.
• This is called Cartesian coordinates.
• Cartesian coordinates have an X axis (dimension) and Y axis.
• The coordinates of a point are always specified as x,y.

Visual Basic Data Types:

In Visual Basic, Data Types are useful to define a type of data the


variable can hold, such as integer, float, string, etc., in our application.
 
Visual Basic is a Strongly Typed programming language. Before we
perform any operation on a variable, it’s mandatory to define a
variable with a required data type to indicate what type of data the
variable can hold in our application.

Data Type Size Range

Boolean It depends on the Platform. True or False

Byte 1 byte 0 to 255

Char 2 bytes 0 to 65535

Date 8 bytes 0:00:00am 1/1/01 to 11:59:59pm 12/31/9999

Decimal 16 bytes (+ or -)1.0 x 10e-28 to 7.9 x 10e28

Double 8 bytes -1.79769313486232e308 to 1.79769313486232e308

Integer 4 bytes -2,147,483,648 to 2,147,483,647

Long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,8

Object 4 bytes on a 32-bit platform, 8 Any type can be stored in a variable of type Object
bytes on a 64-bit platform

SByte 1 byte -128 to 127

Short 2 bytes -32,768 to 32,767


Single 4 bytes -3.4028235E+38 through -1.401298E-45 † for negative
45 through 3.4028235E+38 † for positive values

String Depends on Platform 0 to approximately 2 billion Unicode characters

UInteger 4 bytes 0 to 4,294,967,295

ULong 8 bytes 0 to 18,446,744,073,709,551,615 (1.8...E+19 †)

UShort 2 bytes 0 to 65,535

User- Depends on Platform Each member of the structure has a range determined
Defined and is independent of the ranges of the other member

Resources:

A resource is a text file that allows the compiler to manage objects
such as pictures, sounds, mouse cursors, dialog boxes, etc. Microsoft
Visual Studio makes creating a resource file particularly easy by
providing the necessary tools in the same environment used to
program.

A resource is a text file that allows the compiler to manage objects


such as pictures, sounds, mouse cursors, dialog boxes, etc. Microsoft
Visual Studio makes creating a resource file particularly easy by
providing the necessary tools in the same environment used to
program. This means, you usually do not have to use an external
application to create or configure a resource file. Following are some
important features related to resources.
 Resources are interface elements that provide information to the
user.
 Bitmaps, icons, toolbars, and cursors are all resources.
 Some resources can be manipulated to perform an action such as
selecting from a menu or entering data in dialog box.
 An application can use various resources that behave
independently of each other, these resources are grouped into a
text file that has the *.rc extension.
 Most resources are created by selecting the desired one from the
Add Resource dialog box.

 The Add Resource dialog box provides an extensive list of


resources which can be used as per requirements, but if you
need something which is not available then you can add it
manually to the *.rc file before executing the program.

Identifiers
An identifier is a symbol which is a constant integer whose name
usually starts with ID. It consists of two parts − a text string (symbol
name) mapped to an integer value (symbol value).
 Symbols provide a descriptive way of referring to resources and
user-interface objects, both in your source code and while you're
working with them in the resource editors.
 When you create a new resource or resource object,
the resource editors provide a default name for the resource,
for example, IDC_DIALOG1, and assign a value to it.
 The name-plus-value definition is stored in the Resource.h file.
Step 1 − Let us look into our CMFCDialogDemo example from the
last chapter in which we have created a dialog box and its ID
is IDD_EXAMPLE_DLG.

Step 2 − Go to the Solution Explorer, you will see the resource.h file
under Header Files. Continue by opening this file in editor and you
will see the dialog box identifier and its integer value as well.

Icons
An icon is a small picture used on a window which represents an
application. It is used in two main scenarios.
 On a Window's frame, it is displayed on the left side of the
Window name on the title bar.
 In Windows Explorer, on the Desktop, in My Computer, or in
the Control Panel window.
If you look at our MFC Modal Demo example, you will see that
Visual studio was using a default icon for the title bar as shown in the
following snapshot.

You can create your own icon by following the steps given below −
Step 1 − Right-click on your project and select Add → Resources,
you will see the Add Resources dialog box.

Step 2 − Select Icon and click New button and you will see the
following icon.

Step 3 − In Solution Explorer, go to Resource View and expand


MFCModalDemo > Icon. You will see two icons. The
IDR_MAINFRAME is the default one and IDI_ICON1 is the newly
created icon.
Step 4 − Right-click on the newly Created icon and select Properties.
Step 5 − IDI_ICON1 is the ID of this icon, now Let us change this ID
to IDR_MYICON.
Step 6 − You can now change this icon in the designer as per your
requirements. We will use the same icon.
Step 7 − Save this icon.
Step 8 − Go to the CMFCModalDemoDlg constructor in
CMFCModalDemoDlg.cpp file which will look like the following
code.
CMFCModalDemoDlg::CMFCModalDemoDlg(CWnd* pParent /* =
NULL*/)
: CDialogEx(IDD_MFCMODALDEMO_DIALOG, pParent) {
m_hIcon = AfxGetApp() -> LoadIcon(IDR_MAINFRAME);
}
Step 9 − You can now see that the default icon is loaded in the
constructor. Let us change it to IDR_ MYICON as shown in the
following code.
CMFCModalDemoDlg::CMFCModalDemoDlg(CWnd* pParent /* =
NULL*/)
: CDialogEx(IDD_MFCMODALDEMO_DIALOG, pParent) {
m_hIcon = AfxGetApp() -> LoadIcon(IDR_ MYICON);
}
Step 10 − When the above code is compiled and executed, you will
see the new icon is displayed on the dialog box.

Menus:
Menus allow you to arrange commands in a logical and easy-to-find
fashion. With the Menu editor, you can create and edit menus by
working directly with a menu bar that closely resembles the one in
your finished application. To create a menu, follow the steps given
below −
Step 1 − Right-click on your project and select Add → Resources.
You will see the Add Resources dialog box.

Step 2 − Select Menu and click New. You will see the rectangle that
contains "Type Here" on the menu bar.

Step 3 − Write some menu options like File, Edit, etc. as shown in the
following snapshot.

Step 4 − If you expand the Menu folder in Resource View, you will
see the Menu identifier IDR_MENU1. Right-click on this identifier
and change it to IDM_MAINMENU.

Step 5 − Save all the changes.


Step 6 − We need to attach this menu to our dialog box. Expand your
Dialog folder in Solution Explorer and double click on the dialog box
identifier.

Step 7 − You will see the menu field in the Properties. Select the
Menu identifier from the dropdown as shown above.
Step 8 − Run this application and you will see the following dialog
box which also contains menu options.

Toolbars
A toolbar is a Windows control that allows the user to perform some
actions on a form by clicking a button instead of using a menu.
 A toolbar provides a convenient group of buttons that simplifies
the user's job by bringing the most accessible actions as buttons.
 A toolbar can bring such common actions closer to the user.
 Toolbars usually display under the main menu.
 They can be equipped with buttons but sometimes their buttons
or some of their buttons have a caption.
 Toolbars can also be equipped with other types of controls.
To create a toolbar, following are the steps.
Step 1 − Right-click on your project and select Add → Resources.
You will see the Add Resources dialog box.

Step 2 − Select Toolbar and click New. You will see the following
screen.

Step 3 − Design your toolbar in the designer as shown in the


following screenshot and specify the IDs as well.

Step 4 − Add these two variables in CMFCModalDemoDlg class.


CToolBar m_wndToolBar;
BOOL butD;
Step 5 − Following is the complete implementation of
CMFCModalDemoDlg in CMFCModalDemoDlg.h file

Accelerators
An access key is a letter that allows the user to perform a menu action
faster by using the keyboard instead of the mouse. This is usually
faster because the user would not need to position the mouse
anywhere, which reduces the time it takes to perform the action.
Windows Message:

The operating system communicates with your application window by


passing messages to it. A message is simply a numeric code that
designates a particular event. For example, if the user presses the left
mouse button, the window receives a message that has the following
message code
The Windows operating system uses messages to communicate to and from the
windows in the system. In general, messages can be classified as
Messages generated by Windows in response to user input from the keyboard or
mouse
Messages generated by an application, such as those produced by
the SendMessage function
A message is composed of four parts
A window handle that identifies the target window of the message
A message identifier, which is a 32-bit long
Two 32-bit values called message parameters

Messages related to a type of control:


Static, Listbox, Combo box, Button, Edit, Scrollbar, TreeView, ListView,
Toolbar, Trackbar, Statusbar, Progressbar, ToolTip, UpDown, Tab Control

Mouse messages

Keyboard messages

Clipboard messages

Dialog messages

MDI messages

You create a message loop by using the Get Message and Dispatch
Message functions. If your application must obtain character input
from the user, include the Translate Message function in the loop.
Translate Message translates virtual-key messages into character
messages.

In general, a message must be sent to a window. All the messages sent


to you are stored in a Message Queue, a place in the memory which
stores messages which are transferred between applications.
Message Loop:

The way you retrieve messages from the Message Queue is by


creating a Message Loop. A Message Loop is a loop that checks for
messages in the Message Queue. Once a message is received, the
Message Loop dispatches the message by calling a Message Handler,
a function designed to help the Message Loop at processing the
message.

The Message Loop will end when a WM_QUIT message is received,


signaling the application to end. This message could be sent because
the user selected Exit from your File menu, clicked on the close
button (the X small button in the upper right corner of your window),
or pressed Alt+F4. Windows has default Message Handlers for
almost all the messages, giving your window the default window
behavior. In fact, all the standard controls are simply windows with
Message handlers. Take a Button for example. When it gets
a WM_PAINT message, it will draw the button. When you Left-click
the button, it gets a WM_LBUTTONDOWN message, and it draws
the pressed-button. When you let go of the mouse button, it receives
a WM_LBUTTONUP message, and respectively draws the button.
Windows defines many different message types (which are stored as
UINTs). They usually begin with the letters "WM" and an underscore,
as in WM_CHAR and WM_SIZE. The names of the message are
usually a good indicator of what they represent. WM_SIZE for sizing
messages, WM_CHAR for character entry messages and so on. The
naming convention in MFC for message handler functions is to take
away the "WM_" and replace it with "On", so the message handler
for WM_SIZE is usually called OnSize.
A message comes with 2 parameters that give you more information
about the event. Each parameter is a 32-bit
value: lParam and wParam. For example: WM_MOUSEMOVE will
give you the mouse coordinates in one parameter, and in the other,
some flags indicating the state of the ALT, Shift, CTRL and mouse
buttons.
A Message may also return a value which allows you to send data
back to the sending program. For example,
the WM_QUERYENDSESSION message sent by windows before
the computer is shutdown, expects you to return a Boolean value. If
your application can terminate conveniently, it should return TRUE;
otherwise, it should return FALSE. Other message such as
the WM_CTLCOLOR messages expect you to return an HBRUSH.

Device Contexts:

A device context is a Windows data structure containing information


about the drawing attributes of a device such as a display or a printer.
All drawing calls are made through a device-context object, which
encapsulates the Windows APIs for drawing lines, shapes, and text.
Device contexts allow device-independent drawing in Windows.
Device contexts can be used to draw to the screen, to the printer, or to
a metafile.

CPaintDC objects encapsulate the common idiom of Windows,


calling the BeginPaint function, then drawing in the device context,
then calling the EndPaint function. The CPaintDC constructor
calls BeginPaint for you, and the destructor calls EndPaint. The
simplified process is to create the CDC object, draw, and then destroy
the CDC object. In the framework, much of even this process is
automated. In particular, your OnDraw function is passed
a CPaintDC already prepared (via OnPrepareDC), and you simply
draw into it. It is destroyed by the framework and the underlying
device context is released to Windows upon return from the call to
your OnDraw function.

CClientDC objects encapsulate working with a device context that


represents only the client area of a window.
The CClientDC constructor calls the GetDC function, and the
destructor calls the ReleaseDC function. CWindowDC objects
encapsulate a device context that represents the whole window,
including its frame.

CMetaFileDC objects encapsulate drawing into a Windows metafile.


In contrast to the CPaintDC passed to OnDraw, you must in this case
call OnPrepareDC yourself.
Mouse Drawing

Most drawing in a framework program — and thus most device-


context work — is done in the view's OnDraw member function.
However, you can still use device-context objects for other purposes.
For example, to provide tracking feedback for mouse movement in a
view, you need to draw directly into the view without waiting
for OnDraw to be called.

In such a case, you can use a CClientDC device-context object to draw


directly into the view.

Device independence is one of the chief features of Microsoft


Windows. Applications can draw and print output on a variety of
devices. The software that supports this device independence is
contained in two dynamic-link libraries. The first, Gdi.dll, is referred
to as the graphics device interface (GDI); the second is referred to as a
device driver. The name of the second depends on the device where
the application draws output. For example, if the application draws
output in the client area of its window on a VGA display, this library
is Vga.dll; if the application prints output on an Epson FX-80 printer,
this library is Epson9.dll.

An application must inform GDI to load a particular device driver


and, once the driver is loaded, to prepare the device for drawing
operations (such as selecting a line color and width, a brush pattern
and color, a font typeface, a clipping region, and so on). These tasks
are accomplished by creating and maintaining a device context (DC).
A DC is a structure that defines a set of graphic objects and their
associated attributes, and the graphic modes that affect output. The
graphic objects include a pen for line drawing, a brush for painting
and filling, a bitmap for copying or scrolling parts of the screen, a
palette for defining the set of available colors, a region for clipping
and other operations, and a path for painting and drawing operations.
Unlike most of the structures, an application never has direct access to
the DC; instead, it operates on the structure indirectly by calling
various functions.
Document Interface:

MDI enables users to boost productivity by opening more than one


file at once. For example, when opening an Excel spreadsheet, they
may be able to open another file, such as a Word document.

Document Interface:

Multiple Document Interface (MDI) is a


Microsoft Windows programming interface for creating an
application that enables users to work with
multiple documents simultaneously. Each document is in a
separate space with its own controls for scrolling.

What is the purpose of Multiple Document Interface?


MDI enables users to boost productivity by opening more than one
file at once. For example, when opening an Excel spreadsheet, they
may be able to open another file, such as a Word document.

The user can see and work with different documents, such as a
spreadsheet, a text document or a drawing space, by simply moving
the cursor from one space to another. This feature enables users to
work simultaneously on different documents without having to close
out of one application before starting up another.

Each document is displayed within a separate child window within


the client area. MDI applications can be used for a variety of
purposes. An MDI example is working on one document while
referring to another, viewing different presentations of the same
information, viewing numerous websites simultaneously and
performing any task requiring various reference points and work areas
simultaneously.

An MDI application is like the Windows desktop interface with


multiple viewing spaces. However, the MDI viewing spaces are
confined to the application's window or client area. MDI is useful
because it saves time and reduces errors.

What is the difference between Single Document Interface and


Multiple Document Interface?
Single Document Interface (SDI) and MDI differ significantly. The
former enables users to view only one window at a time. While MDI,
on the other hand, enables users to see all windows at once.

What are the advantages of Multiple Document Interface?


MDI enables users to open more than one file at once. It was
developed to enable people working with large amounts of data to
easily view several documents simultaneously. The main advantage of
MDI is that it makes it easier to manage files because they can be
viewed side by side. For example, comparing two versions of the
same spreadsheet is often helpful when working with multiple
spreadsheets. With MDI, this becomes much simpler.

Another benefit of MDI is that users can quickly switch between


documents without having to close them first. In addition, MDI
reduces the amount of screen clutter and improves readability.

What are the disadvantages of Multiple Document Interface?


Without an MDI frame window, floating toolbars from one
application can clutter the workspace, potentially confusing users with
a messy interface. It can be tricky to utilize multiple screens on
desktops as the parent window might require stretching across two or
more screens, hiding sections.

MDI can also make it increasingly challenging to work with several


applications simultaneously. This is because there may be restrictions
on how windows from multiple applications are arranged together
without obscuring each other.
MDI child windows sometimes behave differently from SDI
applications, and MDI parent windows behave more like a desktop. It
can quickly become confusing whenever this is the case, and users
have to learn two subtly different windowing concepts.

Typically, windows that belong to an application are grouped together


automatically and according to preestablished policies. However, this
can potentially make MDI redundant. Furthermore, hot keys and
controls that users learn to use in an MDI application won't apply to
other applications. In contrast, in an advanced Windows manager,
behaviors and user preferences can be shared across client
applications running on the same system.

Dynamic Linking:

Dynamic linking means that the code for some external routines is


located and loaded when the program is first run. When you compile a
program that uses shared libraries, the shared libraries are
dynamically linked to your program by default.

Programs that are linked dynamically are linked against shared


objects that have the extension . so. An example of such an object is
the shared object version of the standard C library, libc.so.

You might also like