CHAPTER 11 1
11.1 HIERARCHY OF WINDOWS 2
11.2 THREADS 2
11.2.1 USER-INTERFACE
THREAD
3
11.2.2 WORKER
THREAD
3
11.3 WINDOWS 3
11.3.1 DESKTOP
WINDOW
3
11.3.2 APPLICATION
WINDOWS
4
11.3.2.1 Client Area 5
11.3.2.2 Nonclient Area 5
11.3.3 WINDOW
ATTRIBUTES
6
11.3.3.1 Class Name 6
11.3.3.2 Window Name 6
11.3.3.3 Window Style 7
11.3.3.4 Extended Window Style 7
11.3.3.5 Position 7
11.3.3.6 Size 8
11.3.3.7 Parent or Owner Window Handle 8
11.3.3.8 Menu Handle or Child-Window Identifier 9
11.3.3.9 Application Instance Handle 9
11.3.3.10 Creation Data 9
11.3.3.11 Window Handle 9
11.3.4 MULTITHREAD
APPLICATIONS
10
11.4 CONTROLS AND DIALOG BOXES 10
11.4.1 EDIT
CONTROL
10
11.4.2 STATIC CONTROLS
11
11.4.3 SCROLL
BAR
11
11.5 COMMON CONTROLS 11
11.6 OTHER USER INTERFACE ELEMENTS 12
11.7 WINDOWS MESSAGES(BRIEF DESCRIPTION) 12
11.7.1 WM_SYSCOMMAND: 12
SUMMARY 14
EXERCISE 14User Interfaces 2
11.1 Hierarchy of Windows
The basic building block for displaying information in the Microsoft®
Windows™
graphical environment is the window. Microsoft Windows manages how each
window
relates to all other windows in terms of visibility, ownership, and
parent/child
relationship. Windows uses this relationship information when creating,
painting,
destroying, sizing or displaying a window. A window can have many children’s
and may
or may not have one parent. An example of windows is Notepad, calculator,
word pad
etc, are all windows.
A window shares the screen with other windows, including those from other
applications.
Only one window at a time can receive input from the user. The user can use
the mouse,
keyboard, or other input device to interact with this window and the
application that owns
it.
11.2 Threads
A thread is basically a path of execution through a program. It is also the
smallest unit of
execution that Win32 schedules. A thread consists of a stack, the state of
the CPU
registers, and an entry in the execution list of the system scheduler. Each
thread shares all
of the process’s resources.
A process consists of one or more threads and the code, data, and other
resources of a
program in memory. Typical program resources are open files, semaphores, and
dynamically allocated memory. A program executes when the system scheduler
gives one
of its threads execution control. The scheduler determines which threads
should run and
when they should run. Threads of lower priority may have to wait while
higher priority
threads complete their tasks. On multiprocessor machines, the scheduler can
move
individual threads to different processors to “balance” the CPU load.
Each thread in a process operates independently. Unless you make them
visible to each
other, the threads execute individually and are unaware of the other threads
in a process.
Threads sharing common resources, however, must coordinate their work by
using
semaphores or another method of inter process communication.
So a thread is a process that is part of a larger process or application. A
thread can
execute any part of an application's code, including code that is currently
being executed
by another thread. All threads share the
Virtual Address space
Global variables
Operating system resources of their
respective processes.
Threads are two types of threads.
User Interfaces 3
1. User-Interface Thread
2. Worker Thread
11.2.1 User-Interface Thread
In Windows, a thread that handles user input and responds to user events
independently is
User-Interface Thread. User-interface thread own one or more windows and
have its own
message queue. User-interface threads process messages, received from the
system.
11.2.2 Worker Thread
A worker thread is commonly used to handle background tasks. Tasks such as
calculation
and background printing are good examples of worker threads.
11.3 Windows
In a graphical Microsoft® Windows®-based application, a window is a
rectangular area
of the screen where the application displays output and receives input from
the user.
Therefore, one of the first tasks of a graphical Windows-based application
is to create a
window.
A window shares the screen with other windows, including those from other
applications.
Only one window at a time can receive input from the user. The user can use
the mouse,
keyboard, or other input device to interact with this window and the
application that owns
it.
A Window may further contain more windows inside it. For example lets take a
calculator, A calculator contains more windows in forms of buttons, radio
buttons and
check boxes.
• Every Window has its parent and zero or more
siblings.
• Top level window has desktop as its parent.
11.3.1 Desktop Window
When you start the system, it automatically creates the desktop window. The
desktop
window is a system-defined window that paints the background of
the screen and serves
as the base for all windows displayed by all applications.
The desktop window uses a bitmap to paint the background of the screen. The
pattern
created by the bitmap is called the
desktop wallpaper. By default, the desktop window
uses the bitmap from a .bmp file specified in the registry as the desktop
wallpaper.
The GetDesktopWindow
function returns a handle to the desktop window.
A system configuration application, such as a Control Panel item, changes
the desktop
wallpaper by using the SystemParametersInfo function with the
wAction parameter set to
User Interfaces 4
SPI_SETDESKWALLPAPER and the lpvParam
parameter specifying a bitmap file
name. SystemParametersInfo then loads the bitmap from the specified
file, uses the
bitmap to paint the background of the screen, and enters the new file name
in the registry.
11.3.2 Application Windows
Every graphical Microsoft® Windows®-based application creates at least one
window,
called the main window that
serves as the primary interface between the user and the
application. Most applications also create other windows, either directly or
indirectly, to
perform tasks related to the main window. Each window plays a part in
displaying output
and receiving input from the user.
When you start an application, the system also associates a taskbar button
with the
application. The taskbar button
contains the program icon and title. When the application
is active, its taskbar button is displayed in the pushed state.
An application window includes elements such as a title bar, a menu bar, the
window
menu (formerly known as the system menu), the minimize button, the maximize
button,
the restore button, the close button, a sizing border, a client area, a
horizontal scroll bar,
and a vertical scroll bar. An application's main window typically includes
all of these
components. The following illustration shows these components in a typical
main
window.
Figure 1 Windows Parts
User Interfaces 5
11.3.2.1 Client Area
The client area is the part
of a window where the application displays output, such as text
or graphics. For example, a desktop publishing application displays the
current page of a
document in the client area. The application must provide a function, called
a window
procedure, to process input to the window and display output in the client
area. For more
information, see Window Procedures.
11.3.2.2 Nonclient Area
The title bar, menu bar, window menu, minimize and maximize buttons, sizing
border,
and scroll bars are referred to collectively as the window's
nonclient area. The system
manages most aspects of the nonclient area; the application manages the
appearance and
behavior of its client area.
The title bar displays an
application-defined icon and line of text; typically, the text
specifies the name of the application or indicates the purpose of the
window. An
application specifies the icon and text when creating the window. The title
bar also makes
it possible for the user to move the window by using a mouse or other
pointing device.
Most applications include a menu bar
that lists the commands supported by the
application. Items in the menu bar represent the main categories of
commands. Clicking
an item on the menu bar typically opens a pop-up menu whose items correspond
to the
tasks within a given category. By clicking a command, the user directs the
application to
carry out a task.
The window menu is created
and managed by the system. It contains a standard set of
menu items that, when chosen by the user, set a window’s size or position,
closes the
application, or performs tasks.
The buttons in the upper-right corner affect the size and position of the
window. When
you click the maximize button,
the system enlarges the window to the size of the screen
and positions the window, so it covers the entire desktop, minus the
taskbar. At the same
time, the system replaces the maximize button with the restore button. When
you click
the restore button, the
system restores the window to its previous size and position. When
you click the minimize button,
the system reduces the window to the size of its taskbar
button, positions the window over the taskbar button, and displays the
taskbar button in
its normal state. To restore the application to its previous size and
position, click its
taskbar button. By clicking the close
button, application exits.
The sizing border is an area
around the perimeter of the window that enables the user to
size the window by using a mouse or other pointing device.
The horizontal scroll bar
and vertical scroll bar
convert mouse or keyboard input into
values that an application uses to shift the contents of the client area
either horizontally or
vertically. For example, a word-processing application that displays a
lengthy document
User Interfaces 6
typically provides a vertical scroll bar to enable the user to page up and
down through the
document.
11.3.3 Window Attributes
An application must provide the following information when creating a
window. (With
the exception of the Window Handle, which the creation function returns to
uniquely
identify the new window.)
•
Class Name
•
Window Name
•
Window Style
•
Extended Window Style
•
Position
•
Size
•
Parent or Owner Window Handle
•
Menu Handle or Child-Window
Identifier
•
Application Instance Handle
•
Creation Data
•
Window Handle
These window attributes are described in the following sections.
11.3.3.1 Class Name
Every window belongs to a window class. An application must register a
window class
before creating any windows of that class. The
window class defines most
aspects of a
window's appearance and behavior. The chief component of a window class is
the
window procedure, a function
that receives and processes all input and requests sent to
the window. The system provides the input and requests in the form of
messages. For
more information, see Window Classes, Window Procedures, and Messages and
Message Queues.
11.3.3.2 Window Name
A window name is a text
string that identifies a window for the user. A main window,
dialog box, or message box typically displays its window name in its title
bar, if present.
A control may display its window name, depending on the control's class. For
example,
buttons, edit controls, and static controls displays their window names
within the
rectangle occupied by the control. However, list boxes, combo boxes, and
static controls
do not display their window names.
To change the window name after creating a window, use the
SetWindowText function.
This function uses the
GetWindowTextLength and
GetWindowText functions to retrieve
the current window-name string from the window.
User Interfaces 7
11.3.3.3 Window Style
Every window has one or more window styles. A window style is a named
constant that
defines an aspect of the window's appearance and behavior that is not
specified by the
window's class. An application usually sets window styles when creating
windows. It can
also set the styles after creating a window by using the
SetWindowLong function.
The system and, to some extent, the window procedure for the class,
interpret the window
styles.
Some window styles apply to all windows, but most apply to windows of
specific
window classes. The general window styles are represented by constants that
begin with
the WS_ prefix; they can be combined with the OR operator to form different
types of
windows, including main windows, dialog boxes, and child windows. The
class-specific
window styles define the appearance and behavior of windows belonging to the
predefined control classes. For example, the SCROLLBAR class specifies a
scroll bar
control, but the SBS_HORZ and SBS_VERT styles determine whether a horizontal
or
vertical scroll bar control is created.
For lists of styles that can be used by windows, see the following topics:
• Window Styles
• Button Styles
• Combo Box Styles
• Edit Control Styles
• List Box Styles
• Rich Edit Control Styles
• Scroll Bar Control Styles
• Static Control Styles
11.3.3.4 Extended Window Style
Every window can optionally have one or more extended window styles. An
extended
window style is a named constant that defines an aspect of the
window's appearance and
behavior that is not specified by the window class or the other window
styles. An
application usually sets extended window styles when creating windows. It
can also set
the styles after creating a window by using the SetWindowLong
function.
For more information, see CreateWindowEx.
11.3.3.5 Position
A window's position is defined as the coordinates of its upper left corner.
These
coordinates, sometimes called window coordinates, are always relative to the
upper left
corner of the screen or, for a child window, the upper left corner of the
parent window's
client area. For example, a top-level window having the coordinates (10,10)
is placed 10
User Interfaces 8
pixels to the right of the upper left corner of the screen and 10 pixels
down from it. A
child window having the coordinates (10,10) is placed 10 pixels to the right
of the upper
left corner of its parent window's client area and 10 pixels down from it.
The WindowFromPoint function
retrieves a handle to the window occupying a particular
point on the screen. Similarly, the
ChildWindowFromPoint and
ChildWindowFromPointEx
functions retrieve a handle to the child window occupying a
particular point in the parent window's client area. Although
ChildWindowFromPointEx can ignore invisible, disabled, and transparent child
windows, ChildWindowFromPoint cannot.
11.3.3.6 Size
A window's size (width and height) is given in pixels. A window can have
zero width or
height. If an application sets a window's width and height to zero, the
system sets the size
to the default minimum window size. To discover the default minimum window
size, an
application uses the GetSystemMetrics
function with the SM_CXMIN and SM_CYMIN
flags.
An application may need to create a window with a client area of a
particular size. The
AdjustWindowRect and
AdjustWindowRectEx functions
calculate the required size of a
window based on the desired size of the client area. The application can
pass the resulting
size values to the CreateWindowEx function.
An application can size a window so that it is extremely large; however, it
should not size
a window so that it is larger than the screen. Before setting a window's
size, the
application should check the width and height of the screen by using
GetSystemMetrics with the SM_CXSCREEN and SM_CYSCREEN flags.
11.3.3.7 Parent or Owner Window Handle
A window can have a parent window. A window that has a parent is called
a child
window. The parent window
provides the coordinate system used for positioning a child
window. Having a parent window affects aspects of a window's appearance; for
example,
a child window is clipped so that no part of the child window can appear
outside the
borders of its parent window. A window that has no parent, or whose parent
is the
desktop window, is called a top-level
window. An application uses the EnumWindows
function to obtain a handle to each of its top-level windows. EnumWindows
passes the
handle to each top-level window, in turn, to an application-defined callback
function,
EnumWindowsProc.
A window can own, or be owned by, another window. An owned window always
appears
in front of its owner window, is hidden when its owner window is minimized,
and is
destroyed when its owner window is destroyed. For more information, see
Owned
Windows.
User Interfaces 9
11.3.3.8 Menu Handle or Child-Window Identifier
A child window can have a
child-window identifier, a unique, application-defined value
associated with the child window. Child-window identifiers are especially
useful in
applications that create multiple child windows. When creating a child
window, an
application specifies the identifier of the child window. After creating the
window, the
application can change the window's identifier by using the SetWindowLong
function,
or it can retrieve the identifier by using the GetWindowLong function.
Every window, except a child window, can have a menu. An application can
include a
menu by providing a menu handle either when registering the window's class
or when
creating the window.
11.3.3.9 Application Instance Handle
Every application has an instance handle associated with it. The system
provides the
instance handle to an application when the application starts. Because it
can run multiple
copies of the same application, the system uses instance handles internally
to distinguish
one instance of an application from another. The application must specify
the instance
handle in many different windows, including those that create windows.
11.3.3.10 Creation Data
Every window can have application-defined creation data associated with
it. When the
window is first created, the system passes a pointer to the data on to the
window
procedure of the window being created. The window procedure uses the data to
initialize
application-defined variables.
11.3.3.11 Window Handle
After creating a window, the creation function returns a
window handle that uniquely
identifies the window. A window handle has the HWND data type; an
application must
use this type when declaring a variable that holds a window handle. An
application uses
this handle in other functions to direct their actions to the window.
An application can use the FindWindow
function to discover whether a window with the
specified class name or window name exists in the system. If such a window
exists,
FindWindow returns a handle to the window. To limit the search to the
child windows of
a particular application, use the
FindWindowEx function.
The IsWindow function
determines whether a window handle identifies a valid, existing
window. There are special constants that can replace a window handle in
certain
functions. For example, an application can use HWND_BROADCAST in the
SendMessage and
SendMessageTimeout
functions, or HWND_DESKTOP in the
MapWindowPoints function.
User Interfaces 10
11.3.4 Multithread Applications
A Windows-based application can have multiple threads of execution, and each
thread
can create windows. The thread that creates a window must contain the code
for its
window procedure.
An application can use the
EnumThreadWindows function to enumerate the windows
created by a particular thread. This function passes the handle to each
thread window, in
turn, to an application-defined callback function,
EnumThreadWndProc.
The GetWindowThreadProcessId
function returns the identifier of the thread that created
a particular window.
To set the show state of a window created by another thread, use the
ShowWindowAsync
function.
11.4 Controls and Dialog Boxes
An application can create several types of windows in addition to its main
window,
including controls and dialog boxes.
A control is a window that
an application uses to obtain a specific piece of information
from the user, such as the name of a file to open or the desired point size
of a text
selection. Applications also use controls to obtain information needed to
control a
particular feature of an application. For example, a word-processing
application typically
provides a control to let the user turn word wrapping on and off. For more
information,
see Windows Controls.
Controls are always used in conjunction with another window—typically, a
dialog box. A
dialog box is a window that
contains one or more controls. An application uses a dialog
box to prompt the user for input needed to complete a command. For example,
an
application that includes a command to open a file would display a dialog
box that
includes controls in which the user specifies a path and file name. Dialog
boxes do not
typically use the same set of window components as does a main window. Most
have a
title bar, a window menu, a border (non-sizing), and a client area, but they
typically do
not have a menu bar, minimize and maximize buttons, or scroll bars. For more
information, see Dialog Boxes.
A message box is a special
dialog box that displays a note, caution, or warning to the
user. For example, a message box can inform the user of a problem the
application has
encountered while performing a task. For more information, see Message
Boxes.
11.4.1 Edit Control
An edit control is selected and receives the input focus when a user clicks
the mouse
User Interfaces 11
inside it or presses the TAB key. After it is selected, the edit control
displays its text (if
any) and a flashing caret that indicates the insertion point. The user can
then enter text,
move the insertion point, or select text to be edited by using the keyboard
or the mouse.
An edit control can send notification messages to its parent window in the
form of
WM_COMMAND messages.
11.4.2 Static controls
• A static control is a control that enables an
application to provide the user with
informational text and graphics that typically require no response.
• Applications often use static controls to
label other controls or to separate a group
of controls. Although static controls are child windows, they cannot be
selected.
Therefore, they cannot receive the keyboard focus.
Example of static control is a text in message box.
11.4.3 Scroll Bar
A window in a Win32®-based application can display a data object, such as a
document
or a bitmap that is larger than the window's client area. When provided with
a scroll bar,
the user can scroll a data object in the client area to bring into view the
portions of the
object that extend beyond the borders of the window.
Scroll bar is of two types. Horizontal Scroll bars and Vertical Scroll bar.
11.5 Common Controls
The common controls are a set of windows that are implemented by the common
control
, which is a dynamic-link (DLL) included with the Microsoft®
Windows®
operating system. Like other control windows, a common control is a child
window that
an application uses in conjunction with another window to perform I/O tasks.
Common controls are of these types.
• Date Time Picker Control.
• List View Control.
User Interfaces 12
11.6 Other user Interface Elements
The following are the user interface elements used in Windows.
• Cursors (Mouse shape)
• Icons (Windows Desktop Icons)
• Bitmaps (Images with RGB color values.)
• Accelerators (CTRL + S) Short Key
combinations.
11.7 Windows Messages (brief description)
The following are the some of the windows messages
• WM_CLOSE
• WM_COMMAND
• WM_CREATE
• WM_DESTROY
• WM_ENABLE
• WM_LBUTTONDOWN
• WM_PAINT
• WM_RBUTTONDOWN
• WM_SYSCOMMAND
• WM_QUIT
• WM_SETTEXT
11.7.1 WM_SYSCOMMAND
A window receives this message when the user chooses a command from the
window
menu (formerly known as the system or control menu) or when the user chooses
the
maximize button, minimize button, restore button, or close button.
wParam:
This parameter specifies the type of system command requested.
This parameter
can be one of the following values.
SC_MAXIMIZE
SC_MINIMIZE
SC_CLOSE
SC_RESTORE
SC_MAXIMIZE
lParam
This parameter is the low-order word specifies the horizontal
position of the
cursor, in screen coordinates, if a window menu command is chosen with the
mouse.
Otherwise, this parameter is not used. The high-order word specifies the
vertical position
of the cursor, in screen coordinates, if a window menu command is chosen
with the
User Interfaces 13
mouse. This parameter is -1 if the command is chosen using a system
accelerator, or zero
if using a mnemonic.
The Window Procedure (Switch Only)
case WM_SYSCOMMAND:
{
wParam &= 0xFFF0; // lower 4-bits used by system
switch(wParam)
{
case SC_MAXIMIZE:
wParam = SC_MINIMIZE;//we handle this message and change it to
//SC_MINIMIZE
return DefWindowProc(hWnd, message, wParam, lParam);
case SC_MINIMIZE:
wParam = SC_MAXIMIZE; //we handle this message and change it to
//SC_MAXIMIZE
return DefWindowProc(hWnd, message, wParam, lParam);
case SC_CLOSE:
if(MessageBox(hWnd, "Are you sure to quit?",
"Please Confirm", MB_YESNO) == IDYES)
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
break;
case WM_DESTROY: PostQuitMessage(0); break;
default: return DefWindowProc(hWnd, message, wParam, lParam);
}
Swap Minimize/Maximize
Application swap buttons Maximize and Minimize with each other, as it is
described in windows procedure.
User Interfaces 14
Summary
In this lecture, we learnt more about windows, its hierarchy and types. We
learnt
about windows types, these are owned windows and child windows. We must keep
this
point in mind that owned windows and child windows have different concepts.
Owned
windows have the handle of their owner windows, these handle make a chain of
owned
windows. We read about the behavior of owned windows and owner windows. We
knew
that if we bring some change to owner window then the owned windows will
response on
some changes like minimize and destroying operations. We also knew about
child
windows that these are the part of its parent’s client area. After we knew
about threads
and their types, threads are two types one is User interface thread and
second is working
thread. UI (User interface) thread is attached with user interfaces like
windows, messages
and dialog boxes. We gained a little knowledge about controls. And after
that we learnt
how to make a windows procedure with responses system menu including close,
maximize and minimize button.
Exercise
1. Write down a code which able to
i. Create window on screen with default coordinates
ii. Show Edit control on top left corner in the client area.
iii. Display button besides the edit control, which contains text ‘Show
text on client area’
iv. After pressing button, text must display on the client area.
v. And when the user closes the application, it must show message
box which will be containing ‘Thanks for using my application’ |