Thanks for both GloriousEremite and WolfPack for responding to my request. I found the error but I am posting the corrected code below so that other people can benefit from my mistake. But maybe you can help me with one more minor problem. When the form shows up if you click the menu item File, Exit. The help text appears in the status bar but the three parts of the status bar disappear. They reapear once again if you resize the window. Any idea why?
#include <windows.h>
#include <commctrl.h>
#include "resource.h"
#define IDC_STATUSBAR 101
#define IDC_TOOLBAR 102
#define IDC_TREEVIEW 103
#define TOOLBUTTONS 3
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int wndHeight(HWND hwndhgt);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("MyProgram") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
InitCommonControls();
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("My Program"), // window caption
WS_OVERLAPPEDWINDOW |
WS_CLIPCHILDREN, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hStatusBar, hToolBar, hTreeView;
int iStatusBarWidths[] = {300, 600, -1};
TBBUTTON tbButtons[TOOLBUTTONS];
TBADDBITMAP tbAbitmaps;
RECT rcClient;
switch (message)
{
case WM_CREATE:
/* Create the Status bar and set its parts */
hStatusBar = CreateWindowEx(0, STATUSCLASSNAME, NULL,
WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
hwnd, (HMENU)IDC_STATUSBAR, GetModuleHandle(NULL), NULL);
SendMessage(hStatusBar, SB_SETPARTS, sizeof(iStatusBarWidths)/sizeof(int), (LPARAM)iStatusBarWidths);
/* Create the ToolBar & send backwark compatabiltiy message */
hToolBar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
hwnd, (HMENU)IDC_TOOLBAR, GetModuleHandle(NULL), NULL);
SendMessage(hToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
tbAbitmaps.hInst = HINST_COMMCTRL;
tbAbitmaps.nID = IDB_STD_LARGE_COLOR;
SendMessage(hToolBar, TB_ADDBITMAP, 0, (LPARAM)&tbAbitmaps);
ZeroMemory(tbButtons, sizeof(tbButtons));
tbButtons[0].iBitmap = STD_FILENEW;
tbButtons[0].fsState = TBSTATE_ENABLED;
tbButtons[0].fsStyle = TBSTYLE_BUTTON;
tbButtons[1].iBitmap = STD_FILEOPEN;
tbButtons[1].fsState = TBSTATE_ENABLED;
tbButtons[1].fsStyle = TBSTYLE_BUTTON;
tbButtons[2].iBitmap = STD_FILESAVE;
tbButtons[2].fsState = TBSTATE_ENABLED;
tbButtons[2].fsStyle = TBSTYLE_BUTTON;
SendMessage(hToolBar, TB_ADDBUTTONS, sizeof(tbButtons)/sizeof(TBBUTTON), (LPARAM)&tbButtons);
/* Create the Tree View Control */
GetClientRect(hwnd, &rcClient);
hTreeView = CreateWindowEx(0, WC_TREEVIEW, NULL, WS_CHILD | WS_VISIBLE |WS_BORDER,
0, wndHeight(hToolBar), 200, rcClient.bottom - wndHeight (hToolBar) - wndHeight (hStatusBar),
hwnd, (HMENU)IDC_TREEVIEW, GetModuleHandle(NULL), NULL);
return 0 ;
case WM_SIZE:
SendMessage(GetDlgItem(hwnd, IDC_STATUSBAR), WM_SIZE, wParam, lParam);
SendMessage(GetDlgItem(hwnd, IDC_TOOLBAR), TB_AUTOSIZE, 0, 0);
GetClientRect(hwnd, &rcClient);
MoveWindow(hTreeView,0, wndHeight(hToolBar), 200,
rcClient.bottom - wndHeight (hToolBar) - wndHeight (hStatusBar), TRUE);
return 0;
/*This code is for displaying menu help in the status bar */
case WM_MENUSELECT: {
switch (LOWORD(wParam))
{
case ID_FILE_EXIT:
SendMessage(GetDlgItem(hwnd, IDC_STATUSBAR), SB_SETTEXT, 0, (LPARAM)(LPSTR)" Quit program");
break;
default:
SendMessage(GetDlgItem(hwnd, IDC_STATUSBAR), SB_SETTEXT, 255, (LPARAM)(LPSTR)"Ready");
}
return 0;
}
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
int wndHeight (HWND hwndhgt)
{
RECT wndRect;
GetWindowRect(hwndhgt, &wndRect);
return (wndRect.bottom - wndRect.top);
}
The error was that I did not define the HWND hStatusBar, hToolBar, hTreeView as static.
And the other change I made is that I added the following piece of code in the SIZE message:
But to be honest I still don't understand why not having the MoveWindow function would have such impact when the form is created (before any sizing message) !!!