User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,454 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,018 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1300 | Replies: 5
Reply
Join Date: Sep 2006
Posts: 5
Reputation: nhandal is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nhandal nhandal is offline Offline
Newbie Poster

Calculating a control position on a window

  #1  
Sep 10th, 2006
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Rep Power: 4
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Calculating a control position on a window

  #2  
Sep 10th, 2006
Could you post a short example program that we can compile, and that demonstrates the problem?
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Calculating a control position on a window

  #3  
Sep 10th, 2006
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;
        }
Last edited by WolfPack : Sep 11th, 2006 at 1:42 am.
バルサミコ酢やっぱいらへんで
Reply With Quote  
Join Date: Sep 2006
Posts: 5
Reputation: nhandal is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nhandal nhandal is offline Offline
Newbie Poster

Re: Calculating a control position on a window

  #4  
Sep 11th, 2006
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) !!!
Reply With Quote  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Rep Power: 4
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Calculating a control position on a window

  #5  
Sep 11th, 2006
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)
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
Reply With Quote  
Join Date: Sep 2006
Posts: 5
Reputation: nhandal is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nhandal nhandal is offline Offline
Newbie Poster

Re: Calculating a control position on a window

  #6  
Sep 12th, 2006
Thanks, it works now.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 3:21 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC