Tutorial 18: Common Controls

We will learn what common controls are and how to use them. This tutorial will be a quick introduction to them only.

Theory:

Windows 95 comes with several user-interface enhancements over Windows 3.1x. They make the GUI richer. Several of them are in widely used before Windows 95 hit the shelf, such as status bar, toolbars etc. Programmers have to code them themselves. Now Microsoft has included them with Windows 9x and NT. We will learn about them here.
These are the new controls: Since there are many of them, loading them all into memory and registering them would be a waste of resource. All of them, with the exception of rich edit control, are stored in comctl32.dll with applications can load when they want to use the controls. Rich edit control resides in its own dll, richedXX.dll, because it's very complicated and hence larger than its brethren.
You can load comctl32.dll by including a call to InitCommonControls in your program. InitCommonControls is a function in comctl32.dll, so referring to it anywhere in your code will make PE loader load comctl32.dll when your program runs.You don't have to execute it, just include it in your code somewhere. This function does NOTHING! Its only instruction is "ret". Its sole purpose is to include reference to comctl32.dll in the import section so that PE loader will load it whenever the program is loaded. The real workhorse is the DLL entrypoint function which registers all common control classes when the dll is loaded. Common controls are created based on those classes just like other child window controls such as edit, listbox etc.
Rich edit is another matter entirely. If you want to use it, you have to call LoadLibrary to load it explicitly and call FreeLibrary to unload it.
Now we learn how to create them. You can use a resource editor to incorporate them into dialog boxes or you can create them yourself. Nearly all common controls are created by calling CreateWindowEx or CreateWindow, passing it the name of the control class. Some common controls have specific creation functions , however, they are just wrappers around CreateWindowEx to make it easier to create those controls. Existing specific creation functions are listed below: In order to create common controls, you have to know their class names. They are listed below:
 
Class Name
Common Control
ToolbarWindow32 Toolbar
tooltips_class32 Tooltip
msctls_statusbar32 Status bar
SysTreeView32 Tree view
SysListView32 List view
SysAnimate32 Animation
SysHeader32 Header
msctls_hotkey32 Hot-key
msctls_progress32 Progress bar
RICHEDIT Rich edit
msctls_updown32 Up-down
SysTabControl32 Tab
Property sheets and property pages and image list control have their own specific creation functions. Drag list control are souped-up listbox so it doesn't have its own class. The above class names are verified by checking resource script generated by Visual C++ resource editor. They differ from the class names listed by Borland's win32 api reference and Charles Petzold's Programming Windows 95. The above list is the accurate one.
Those common controls can use general window styles such as WS_CHILD etc. They also have their own specific styles such as TVS_XXXXX for tree view control, LVS_xxxx for list view control, etc. Win32 api reference is your best friend in this regard.
Now that we know how to create common controls, we can move on to communication method between common controls and their parent. Unlike child window controls, common controls don't communicate with the parent via WM_COMMAND. Instead they send WM_NOTIFY messages to the parent window when some interesting events occur with the common controls. The parent can control the children by sending messages to them. There are also many new messages for those new controls. You should consult your win32 api reference for more detail.
Let's examine progress bar and status bar controls in the following example.

Sample code:

_____________________________________________________________________________________
; Data:

[WindowHandle: 0]
______________________________________________________________________________________

; Window Class Structure:
 

[WindowClassEx: wc_Size: len
                wc_style: &CS_HREDRAW+&CS_VREDRAW
                WndProc: MainWindowProc
                wc_ClsExtra: 0  wc_WndExtra: 0        hInstance: 0    wc_hIcon: 0
                wc_hCursor: 0   wc_hbrBackground: 12  wc_MenuName: 0  wc_ClassName: ClassName
                wc_hIconSm: 0]

[FirstMessage: 0 #7]

[IDC_PROGRESS 1  IDC_STATUS 2  IDC_TIMER 3]

[TimerID: 0  hwndProgress: 0  hwndStatus: 0  CurrentStep: 0
 ClassName: 'CommonControlWinClass' 0
 AppName: 'Common Control Demo' 0
 ProgressClass: 'msctls_progress32' 0
 FinishMessage: 'Finished!' 0]
 
____________________________________________________________________________________________
____________________________________________________________________________________________

Main:
    call 'Kernel32.GetModuleHandleA' &NULL   | mov D§hInstance eax
    call 'User32.LoadIconA'  0  &IDI_WINLOGO | mov D§wc_hIcon eax  D§wc_hIconSm eax
    call 'User32.LoadCursorA' 0  &IDC_ARROW  | mov D§wc_hCursor eax
    call 'User32.RegisterClassExA'  WindowClassEX
    call 'User32.CreateWindowExA' &NULL ClassName AppName,
                                 &WS_OVERLAPPEDWINDOW,
                                 &CW_USEDEFAULT &CW_USEDEFAULT &CW_USEDEFAULT &CW_USEDEFAULT,
                                 &NULL &NULL D§hInstance &NULL
        mov D§WindowHandle eax
    call 'User32.ShowWindow'  D§WindowHandle &SW_SHOWNORMAL
    call 'User32.UpdateWindow'  D§WindowHandle
 
L1: call 'User32.GetMessageA' FirstMessage 0 0 0 | cmp eax 0 | je L9>
        call 'User32.TranslateMessage'  FirstMessage
        call 'User32.DispatchMessageA'  FirstMessage
    jmp L1<

L9: call 'Kernel32.ExitProcess' 0

____________________________________________________________________________________________
____________________________________________________________________________________________

Proc MainWindowProc:
    Arguments @Adressee, @Message, @wParam, @lParam
 
    pushad
 
    .If D@Message = &WM_CREATE

        call 'USER32.CreateWindowExA' &NULL  ProgressClass &NULL,
                                     &WS_CHILD+&WS_VISIBLE 100,
                                     200 300 20 D@Adressee IDC_PROGRESS,
                                     D§hInstance &NULL
           mov D§hwndProgress,eax
        mov eax 1000
        mov D§CurrentStep eax
        shl eax 16
        call 'User32.SendMessageA' D§hwndProgress &PBM_SETRANGE 0 eax
        call 'User32.SendMessageA' D§hwndProgress &PBM_SETSTEP 10 0
        call 'Comctl32.CreateStatusWindowA' &WS_CHILD+&WS_VISIBLE &NULL,
                                           D§WindowHandle IDC_STATUS
          mov D§hwndStatus,eax
        call 'User32.SetTimer' D@Adressee IDC_TIMER 100 &NULL
          mov D§TimerID eax

    .Else_If D@Message = &WM_DESTROY
        call 'User32.PostQuitMessage' &NULL
        If D§TimerID ne 0
           call 'User32.KillTimer' D§WindowHandle D§TimerID
        End_If
 
    .Else_If D@Message = &WM_TIMER
        call 'User32.SendMessageA' D§hwndProgress &PBM_STEPIT,0,0
        sub D§CurrentStep 10
        ..If D§CurrentStep = 0
             call 'User32.KillTimer' D§WindowHandle D§TimerID
             mov D§TimerID 0
             call 'User32.SendMessageA' D§hwndStatus &SB_SETTEXT 0 FinishMessage
             call 'User32.MessageBoxA' D§WindowHandle FinishMessage AppName,
                                      &MB_OK+&MB_ICONINFORMATION
             call 'User32.SendMessageA' D§hwndStatus &SB_SETTEXT 0 0
             call 'User32.SendMessageA' D§hwndProgress &PBM_SETPOS 0 0
        ..End_If
    .Else
        popad
        call 'User32.DefWindowProcA' D@Adressee D@Message D@wParam D@lParam
        Exit
 
    .End_If
 
    popad | mov eax 0
EndP
 

Analysis:

  Here is where we create the common control. Note that this CreateWindowEx call contains hWnd as the parent window handle. It also specifies a control ID for identifying this control. However, since we have the control's window handle, this ID is not used. All child window controls must have WS_CHILD style. After the progress bar is created, we can set its range. The default range is from 0 to 100. If you are not satisfied with it, you can specify your own range with PBM_SETRANGE message. lParam of this message contains the range, the maximum range is in the high word and the minimum one is in the low word. You can specify how much a step takes by using PBM_SETSTEP message. The example sets it to 10 which means that when you send a PBM_STEPIT message to the progress bar, the progress indicator will rise by 10. You can also set your own indicator level by sending PBM_SETPOS messages. This message gives you tighter control over the progress bar. Next, we create a status bar by calling CreateStatusWindow. This call is easy to understand so I'll not comment on it. After the status window is created, we create a timer. In this example, we will update the progress bar at a regular interval of 100 ms so we must create a timer control. Below is the function prototype of SetTimer. hWnd : Parent window handle
TimerID : a nonzero timer identifier. You can create your own identifier.
TimerInterval : the timer interval in milliseconds that must pass before the timer calls the timer procedure or sends a WM_TIMER message
lpTimerProc : the address of the timer function that will be called when the time interval expires. If this parameter is NULL, the timer will send WM_TIMER message to the parent window instead.

If this call is successful, it will return the TimerID. If it failed, it returns 0. This is why the timer identifer must be a nonzero value.

When the specified time interval expires, the timer sends a WM_TIMER message. You will put your code that will be executed here. In this example, we update the progress bar and then check if the maximum limit has been reached. If it has, we kill the timer and then set the text in the status window with SB_SETTEXT message. A message box is displayed and when the user clicks OK, we clear the text in the status bar and the progress bar.

[Iczelion's Win32 Assembly Homepage]