Chapter 23
23.1 OVERVIEW OF
WINDOWS
COMMON
CONTROLS
2
23.2 COMMON
CONTROL LIBRARY
3
DLL VERSIONS
4
23.3 COMMON
CONTROL STYLES
4
23.4 INITIALIZE
COMMON
CONTROLS
5
23.4.1 INITCOMMONCONTROLS
FUNCTION
5
23.4.2 INITCOMMONCONTROLSEX
FUNCTION
5
23.4.2.1 INITCOMMONCONTROLSEX STRUCTURE
6
23.5 LIST
VIEW
7
23.6 TODAY’S
GOAL
7
23.7 IMAGE
LIST
7
23.8 IMAGELIST_CREATE
FUNCTION
7
23.9 IMAGELIST_ADDICON
FUNCTION
9
23.10 IMAGELIST_REPLACEICON
FUNCTION
9
23.11 SCREEN
SHOT
OF AN EXAMPLE
APPLICATION
10
23.12 CREATING
LIST
VIEW
CONTROL
10
CREATING
IMAGE
LIST
10
23.13 WINDOWS
DEFAULT
FOLDER
ICON
11
23.14 ADD
IMAGE
LIST
11
23.15 ADD COLUMN
TO LIST
VIEW
11
23.16 ADD AN
ITEM
12
23.17 ADD
SUB
ITEM
FOR THIS ITEM
12
23.18 FIND
FIRST
FILE
12
23.19 ADD
COLUMN
TO LIST
VIEW
13
23.20 LAST
MODIFIED
DATE
OF FILE
13
23.21 MODIFIED
LIST
VIEW
CONTROL 13
SUMMARY
14
EXERCISES
14
Common Controls 2
23.1 Overview of Windows Common Controls
A control is a child window an application uses in conjunction
with another window to
perform simple input and output (I/O) tasks. Controls are most
often used within dialog
boxes, but they can also be used in other windows. Controls
within dialog boxes provide the
user with the means to type text, choose options, and direct a
dialog box to complete its
action. Controls in other windows provide a variety of services,
such as letting the user
choose commands, view status, and view and edit text. The user
control overviews discuss
how to use these controls.
The following table lists the Windows controls.
Control Description
Animation An animation control is a window that displays an
Audio-Video Interleaved
(AVI) clip.
Button Button controls typically notify the parent window when
the user chooses
the control.
Combo Box Combo box controls are a combination of list boxes and
edit controls,
letting the user choose and edit items.
ComboBoxEx ComboBoxEx
Controls are an extension of the combo box control that
provides native support for item images.
Date and Time
Picker
A date and time picker (DTP) control provides a simple and
intuitive
interface through which to exchange date and time information
with a user.
Drag List Box Drag List Boxes
are a special type of list box that enables the user to drag
items from one position to another.
Edit Edit controls let the user view and edit text.
Flat Scroll Bar Flat scroll bars behave just like standard
scroll bars except that you can
customize their appearance to a greater extent than standard
scroll bars.
Header
A header control is a window that is usually positioned above
columns of
text or numbers. It contains a title for each column, and it can
be divided
into parts.
Hot Key A hot key control is a window that enables the user to
enter a combination
of keystrokes to be used as a hot key.
Image Lists An image list is a collection of images of the same
size, each of which can
be referred to by its index.
IP Address
Controls
An Internet Protocol (IP) address control allows the user to
enter an IP
address in an easily understood format.
List Box List box controls display a list from which the user
can select one or more
items.
List-View A list-view control is a window that displays a
collection of items. The
control provides several ways to arrange and display the items.
Month Calendar A month calendar control implements a
calendar-like user interface.
Common Controls 3
Pager A pager control is a window container that is used with a
window that does
not have enough display area to show all of its content.
Progress Bar A progress bar is a window that an application can
use to indicate the
progress of a lengthy operation.
Property Sheets A property sheet is a window that allows the
user to view and edit the
properties of an item.
ReBar Rebar controls act as containers for child windows. An
application assigns
child windows, which are often other controls, to a rebar
control band.
Rich Edit Rich Edit controls let the user view and edit text
with character and
paragraph formatting, and can include embedded COM objects.
Scroll Bars Scroll bars let the user choose the direction and
distance to scroll
information in a related window.
Static Static controls often act as labels for other controls.
Status Bars A status bar is a horizontal window at the bottom of
a parent window in
which an application can display various kinds of status
information.
SysLink A SysLink control provides a convenient way to embed
hypertext links in a
window.
Tab
A tab control is analogous to the dividers in a notebook or the
labels in a
file cabinet. By using a tab control, an application can define
multiple pages
for the same area of a window or dialog box.
Toolbar
A toolbar is a control window that contains one or more buttons.
Each
button, when clicked by a user, sends a command message to the
parent
window.
ToolTip ToolTips are hidden most of the time. They appear
automatically, or pop
up, when the user pauses the mouse pointer over a tool.
Trackbar
A trackbar is a window that contains a slider and optional tick
marks. When
the user moves the slider, using either the mouse or the
direction keys, the
trackbar sends notification messages to indicate the change.
Tree-View
A tree-view control is a window that displays a hierarchical
list of items,
such as the headings in a document, the entries in an index, or
the files and
directories on a disk.
Up-Down
An up-down control is a pair of arrow buttons that the user can
click to
increment or decrement a value, such as a scroll position or a
number
displayed in a companion control.
23.2 Common control
Most common controls belong to a window class defined in the
common control DLL. The
window class and the corresponding window procedure define the
properties, appearance,
and behavior of the control. To ensure that the common control
DLL is loaded, include the
InitCommonControlsEx
function in your application. You create a common control by
specifying the name of the window class when calling the
CreateWindowEx
function or by
specifying the appropriate class name in a dialog box template.
Common Controls 4
DLL Versions
All 32-bit versions of Windows include common controls DLL,
Comctl32.dll. However, this
DLL has been updated several times since it was first
introduced. Each successive version
supports the features and application programming interface
(API) of earlier versions.
However, each new version also contains a number of new features
and a correspondingly
larger API. Applications must be aware of which version of
Comctl32.dll is installed on a
system, and only use the features and API that the DLL supports.
Because new versions of the common controls were distributed
with Microsoft Internet
Explorer, the version of Commctl32.dll that is present is
commonly different from the
version that was shipped with the operating system. It may
actually be several versions more
recent. It is thus not enough for your application to know which
operating system it is
running on. It must directly determine which version of
Comctl32.dll is present.
23.3 Common control Styles
CCS_ADJUSTABLE:
This style enables a toolbar's built-in customization features,
which enable the user to
drag a button to a new position or to remove a button by
dragging it off the toolbar. In
addition, the user can double-click the toolbar to display the
Customize Toolbar
dialog box, which enables the user to add, delete, and rearrange
toolbar buttons.
CCS_BOTTOM:Causes the control to position itself at the bottom of the
parent window's client area
and sets the width to be the same as the parent window's width.
Status windows have
this style by default.
CCS_LEFT:This style causes the control to be displayed vertically on the
left side of the parent
window.
CCS_NODIVIDER:This style prevents a two-pixel highlight from being drawn at
the top of the control.
CCS_NOMOVEX:This style causes the control to resize and move itself
vertically, but not horizontally,
in response to a WM_SIZE message. If CCS_NORESIZE is used, this
style does not
apply.
CCS_NOMOVEY:This style causes the control to resize and move itself
horizontally, but not vertically,
in response to a WM_SIZE
message. If CCS_NORESIZE is used, this style does not
apply. Header windows have this style by default.
CCS_NOPARENTALIGN:This style prevents the control from automatically moving to the
top or bottom of the
parent window. Instead, the control keeps its position within
the parent window
despite changes to the size of the parent. If CCS_TOP or
CCS_BOTTOM is also
used, the height is adjusted to the default, but the position
and width remain
unchanged.
Common Controls 5
CCS_NORESIZE:This style prevents the control from using the default width and
height when setting
its initial size or a new size. Instead, the control uses the
width and height specified in
the request for creation or sizing.
CCS_RIGHT:This style causes the control to be displayed vertically on the
right side of the parent
window.
CCS_TOP:This style causes the control to position itself at the top of
the parent window's client
area and sets the width to be the same as the parent window's
width. Toolbars have
this style by default.
CCS_VERT:This style causes the control to be displayed vertically.
23.4 Initialize Common Controls
For initialization common controls there are two function
available:
• InitCommonControls()
• InitCommonControlsEx()
23.4.1 InitCommonControls Function
Registers and initializes the common control window classes.
According to the Microsoft documentation this little function is
obsolete. New applications
should use the InitCommonControlsEx function. So you should not
use this function.
void InitCommonControls(VOID);
This little function does not return anything.
23.4.2 InitCommonControlsEx Function
Registers specific common control classes from the common
control dynamic-link
(DLL).
BOOL InitCommonControlsEx(
LPINITCOMMONCONTROLSEX
lpInitCtrls
);
lpInitCtrls: Pointer to an
INITCOMMONCONTROLSEX structure that contains information
specifying which control classes will be registered.
Return Value
Returns TRUE if successful, or FALSE otherwise.
Common Controls 6
The effect of each call to
InitCommonControlsEx
is cumulative. For example, if
InitCommonControlsEx is
called with the ICC_UPDOWN_CLASS flag, then is later called
with the ICC_HOTKEY_CLASS flag, the result is that both the
up-down and hot key
common control classes are registered and available to the
application.
23.4.2.1 INITCOMMONCONTROLSEX Structure
This structure carries information used to load common control
classes from the dynamiclink
(DLL). This structure is used with the
InitCommonControlsEx function.
typedef struct tagINITCOMMONCONTROLSEX {
DWORD dwSize;
DWORD dwICC;
} INITCOMMONCONTROLSEX, *LPINITCOMMONCONTROLSEX;
dwSize:Size of the structure, in bytes.
dwICC:Set of bit flags that indicate which common control classes will
be loaded from the
DLL. This value can be a combination of the following:
ICC_ANIMATE_CLASS:
Load animate control class.
ICC_BAR_CLASSES: Load
toolbar, status bar, trackbar, and ToolTip control
classes.
ICC_COOL_CLASSES: Load
rebar control class.
ICC_DATE_CLASSES: Load
date and time picker control class.
ICC_HOTKEY_CLASS: Load
hot key control class.
ICC_INTERNET_CLASSES:
Load IP address class.
ICC_LINK_CLASS: Load a
hyperlink control class.
ICC_LISTVIEW_CLASSES:
Load list-view and header control classes.
ICC_NATIVEFNTCTL_CLASS:
Load a native font control class.
ICC_PAGESCROLLER_CLASS:
Load pager control class.
ICC_PROGRESS_CLASS:
Load progress bar control class.
ICC_STANDARD_CLASSES:
Load one of the intrinsic User32 control classes. The
user controls include button, edit, static, listbox, combobox,
and scrollbar.
ICC_TAB_CLASSES: Load
tab and ToolTip control classes.
ICC_TREEVIEW_CLASSES:
Load tree-view and ToolTip control classes.
ICC_UPDOWN_CLASS: Load
up-down control class.
ICC_USEREX_CLASSES:
Load ComboBoxEx class.
ICC_WIN95_CLASSES:
Load animate control, header, hot key, list-view, progress
bar, status bar, tab, ToolTip, toolbar, trackbar, tree-view, and
up-down control
classes.
Common Controls 7
23.5 List View
23.6 Today’s Goal
Today we are going to create a List Box. This list box will be
explorer style list box. In this
list box you can see large, small, list, report styles.
23.7 Image List
An image list is a collection of images of the same size, each
of which can be referred to by
its index.
23.8 ImageList_Create Function
HIMAGELIST ImageList_Create(
int cx,
int cy,
UINT flags,
int cInitial,
int cGrow
);
cx:
Width, in pixels, of each image.
Common Controls 8
Cy:
Height, in pixels, of each image.
Flags:
Set of bit flags that specify the type of image list to create.
This parameter can be a
combination of the following values, but it can include only one
of the ILC_COLOR
values.
ILC_COLOR:
Use the default behavior if none of the other ILC_COLOR* flags
is specified.
Typically, the default is ILC_COLOR4, but for older display
drivers, the default is
ILC_COLORDDB:
ILC_COLOR4:
Use a 4-bit (16-color) device-independent bitmap (DIB) section
as the bitmap for the
image list.
ILC_COLOR8:
Use an 8-bit DIB section. The colors used for the color table
are the same colors as
the halftone palette.
ILC_COLOR16:
Use a 16-bit (32/64k-color) DIB section.
ILC_COLOR24:
Use a 24-bit DIB section.
ILC_COLOR32:
Use a 32-bit DIB section.
ILC_COLORDDB:
Use a device-dependent bitmap.
ILC_MASK:
Use a mask. The image list contains two bitmaps, one of which is
a monochrome
bitmap used as a mask. If this value is not included, the image
list contains only one
bitmap.
ILC_MIRROR:
Microsoft® Windows® can be mirrored to display languages such as
Hebrew or
Arabic that read right-to-left. If the image list is created on
a mirrored version of
Windows, then the images in the lists are mirrored, that is,
they are flipped so they
display from right to left. Use this flag on a mirrored version
of Windows to instruct
the image list not to automatically mirror images.
ILC_PERITEMMIRROR
cInitial:
This member is number of images that the image list initially
contains.
cGrow:
This member is a number of images by which the image list can
grow when the
system needs to make room for new images. This parameter
represents the number of
new images that the resized image list can contain.
Common Controls 9
23.9 ImageList_AddIcon Function
int ImageList_AddIcon(
HIMAGELIST himl,
HICON hicon
);
himl:
Handle to the image list. If this parameter identifies a masked
image list, the macro
copies both the image and mask bitmaps of the icon or cursor. If
this parameter
identifies a nonmasked image list, the macro copies only the
image bitmap.
Hicon:
Handle to the icon or cursor that contains the bitmap and mask
for the new image.
Return Value:
Returns the index of the new image if successful, or -1
otherwise.
Because the system does not save
hicon, you can destroy it after
the macro returns if the icon
or cursor was created by the CreateIcon function. You do not
need to destroy hicon if it was
loaded by the LoadIcon function; the system automatically frees
an icon resource when it is
no longer needed.
23.10 ImageList_ReplaceIcon Function
int ImageList_ReplaceIcon(
HIMAGELIST himl,
int i,
HICON hicon
);
himl:
Handle to the image list.
i:
Index of the image to replace. If
i is -1, the function appends
the image to the end of
the list.
Hicon:
Handle to the icon or cursor that contains the bitmap and mask
for the new image.
Return Value:
Returns the index of the image if successful, or -1 otherwise.
Common Controls 10
23.11 Screen Shot of an Example Application
23.12 Creating List View Control
#define ID_LISTVIEW 5
hWndListView = CreateWindow(WC_LISTVIEW,
"Window Name",
WS_TABSTOP | WS_CHILD | WS_BORDER | WS_VISIBLE | LVS_AUTOARRANGE
|
LVS_REPORT,
10, 10, 350, 280, hWndMain, (HMENU)ID_LISTVIEW,
hInstance, NULL
);
if(!hWndListView)
{
return 0;
}
Creating Image List
hLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
GetSystemMetrics(SM_CYICON), ILC_MASK, 1, 1);
hSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 1);
Common Controls 11
hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_FOLDER));
ImageList_AddIcon(hLarge, hIcon);
ImageList_AddIcon(hSmall, hIcon);
hIcon = LoadIcon(.. MAKEINTRESOURCE(IDI_ICON_FILE))
23.13 Windows Default Folder Icon
23.14 Add Image List
ListView_SetImageList(hWndListView, hLarge, LVSIL_NORMAL);
ListView_SetImageList(hWndListView, hSmall, LVSIL_SMALL);
HIMAGELIST ListView_SetImageList(
HWND hwnd,
HIMAGELIST himl,
int iImageList type of IL: LVSIL_NORMAL | LVSIL_SMALL |
LVSIL_STATE
);
23.15 Add column to List View
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvc.cx = COL_WIDTH;
for(i=0; i<3; ++i)
{
lvc.iSubItem = i;
lvc.fmt = alignments[i];
lvc.pszText = columnHeadings[i];
if(ListView_InsertColumn(hWndListView, i, &lvc) == -1)
return 1;
}
Folder.ico
Common Controls 12
23.16 Add an Item
/* add an item with 3 subitems = 4 columns */
lvi.state = 0; // no state: cut, focussed, selected etc.
lvi.stateMask = 0; // no state specified: cut, focussed,
selected etc.
lvi.lParam = (LPARAM)1234; // item specific data
do
{
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
lvi.iItem = itemNo++; // which item it refers to
lvi.iSubItem = 0; // refers to an ITEM
lvi.iImage = (findFileData.dwFileAttributes &
FILE_ATTRIBUTE_DIRECTORY) ?
0 : 1; // proper image
lvi.pszText = findFileData.cFileName;
// add the item
if(ListView_InsertItem(hWndListView, &lvi) == -1)
return 0;
23.17 Add Sub Item for this Item
lvi.mask = LVIF_TEXT;
lvi.iSubItem = 1;
(findFileData.nFileSizeHigh * (MAXDWORD+1)) +
findFileData.nFileSizeLow;
if(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
wsprintf(buf, "");
else
wsprintf(buf, "%10lu", findFileData.nFileSizeLow);
lvi.pszText = buf;
if(ListView_SetItem(hWndListView, &lvi) == -1)
return 1;
23.18 Find First File
hFind= FindFirstFile(DEFAULT_PATH, &findFileData);
if(hFind == INVALID_HANDLE_VALUE)
{
MessageBox(NULL, "Error calling FindFirstFile", "Error", MB_OK);
return 0;
}
Common Controls 13
23.19 Add Column to List View
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvc.cx = COL_WIDTH;
for(i=0; i<3; ++i)
{
lvc.iSubItem = i;
lvc.fmt = alignments[i];
lvc.pszText = columnHeadings[i];
if(ListView_InsertColumn(hWndListView, i, &lvc) == -1)
return 0;
}
23.20 Last Modified Date of File
FileTimeToLocalFileTime(&findFileData.ftLastWriteTime,
&fileTime);
FileTimeToSystemTime(&fileTime, &systemTime);
strcpy(strAMPM, systemTime.wHour>=12 ? "PM" : "AM");
if(systemTime.wHour>=12)
systemTime.wHour -= 12;
if(!systemTime.wHour)
systemTime.wHour = 12;
wsprintf(buf, "%d/%d/%d %2d:%02d %s", systemTime.wMonth,
systemTime.wDay,
systemTime.wYear, systemTime.wHour, systemTime.wMinute,
strAMPM);
lvi.iSubItem = 2;
lvi.pszText = buf;
if(ListView_SetItem(hWndListView, &lvi) == -1)
return 1;
23.21 Modified List View control
VOID SetView(HWND hwndListView, DWORD dwStyle)
{
DWORD dwCurrentStyle;
dwCurrentStyle = GetWindowLong(hwndListView, GWL_STYLE);
SetWindowLong(hwndListView, GWL_STYLE, (dwCurrentStyle &
~LVS_TYPEMASK) | dwStyle);
}
Common Controls 14
Summary
Common Controls are the part of Microsoft Windows Graphics
Operating System.
Almost all the WYSIWYG application use Common Controls for their
compatibility and user
friendliness with windows. In this lecture, we studied about
common controls, their styles
and behavior. We also created an application which best
demonstrates the List View control
of common controls. Common controls include controls like page
controls, tree controls, list
view controls that is modified from windows original control,
button control that is also
modified from windows original controls, data and time picker
control, status bar, progress
bar, rebar controls. These all controls reside in common
controls and the has
shipped with many versions. Before using the you must
check the valid version of the
because different version of contains different
controls properties.
Exercises
1. Create Tree control and show all the files hierarchy. |