Hi,

I am trying to create a treeview control in a window that includes a toolbar and a status bar, I need to position the treeview immediately below the toolbar. In order to do this I need to calculate the height of the toolbar and used it as the Y parameter in the CreateWindowEx function that creates the Treeview. The problem is that the treeview appears on top of the toolbar covering the lower part of it. I am using the function below to calculate the height for both the toolbar and status bar to position the treeview between both of them. The calculation is correct for the status bar but something is wrong for the toolbar, that is the treeview appear immediately above the status bar but does not appear immediately below the toolbar. Below is my code:

This is the function to calculate the height for both the toolbar and the status bar:

int wndHeight (HWND hwndhgt)
    {
            RECT wndRect;
            GetWindowRect(hwndhgt, &wndRect);
            return (wndRect.bottom - wndRect.top);
    }

This is the function for creating the treeview:

/* Calculate the client area of the parent window */
 
 
                          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);

I appreciate it if anybody can tell me what is wrong.

Thank you in advance.

Recommended Answers

All 5 Replies

Could you post a short example program that we can compile, and that demonstrates the problem?

Can you do something like this in the WM_SIZE event and tell me what happens?

case WM_SIZE:
        {
            RECT rectTreeView, rectStatus, rectTool;
            UINT uToolHeight, uStatusHeight, uClientAlreaHeight;

            SendMessage(hwndToolBar, TB_AUTOSIZE, 0, 0);
            SendMessage(hwndStatusBar, WM_SIZE, 0, 0);

            GetClientRect(hWnd, &rectTreeView);
            GetWindowRect(hwndStatusBar, &rectStatus);
            GetWindowRect(hwndToolBar, &rectTool);

            uToolHeight = rectTool.bottom - rectTool.top;
            uStatusHeight = rectStatus.bottom - rectStatus.top;
            uClientAlreaHeight = rectTreeView.bottom;

            SetWindowPos(hwndTreeView, HWND_TOP, 0, uToolHeight, rectTreeView.right, uClientAlreaHeight - uStatusHeight - uToolHeight, FALSE);
            return 0;
        }

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:

GetClientRect(hwnd, &rcClient);
 
MoveWindow(hTreeView,0, wndHeight(hToolBar), 200,
     rcClient.bottom - wndHeight (hToolBar) - wndHeight (hStatusBar), TRUE);

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) !!!

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?

SendMessage(GetDlgItem(hwnd, IDC_STATUSBAR), SB_SETTEXT, 255, (LPARAM)(LPSTR)"Ready");

The WPARAM argument should be a zero-based index (with 255/SB_SIMPLEID it probably assumes the control is a single-part bar)

Thanks, it works now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.