| | |
Calculating a control position on a window
![]() |
•
•
Join Date: Sep 2006
Posts: 5
Reputation:
Solved Threads: 0
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:
This is the function for creating the treeview:
I appreciate it if anybody can tell me what is wrong.
Thank you in advance.
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:
C Syntax (Toggle Plain Text)
int wndHeight (HWND hwndhgt) { RECT wndRect; GetWindowRect(hwndhgt, &wndRect); return (wndRect.bottom - wndRect.top); }
C Syntax (Toggle Plain Text)
/* 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.
Can you do something like this in the WM_SIZE event and tell me what happens?
C Syntax (Toggle Plain Text)
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 2:42 am.
バルサミコ酢やっぱいらへんで
•
•
Join Date: Sep 2006
Posts: 5
Reputation:
Solved Threads: 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?
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) !!!
C Syntax (Toggle Plain Text)
#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:
C Syntax (Toggle Plain Text)
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) !!!
•
•
Join Date: Jul 2006
Posts: 65
Reputation:
Solved Threads: 14
•
•
•
•
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?
C Syntax (Toggle Plain Text)
SendMessage(GetDlgItem(hwnd, IDC_STATUSBAR), SB_SETTEXT, 255, (LPARAM)(LPSTR)"Ready");
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
out of this stony rubbish?"
![]() |
Similar Threads
- Abnormal Behaviour of Vertical ScrollBar (VB.NET)
- Windows GUI - problem with dialog box (C++)
- C# : Client windows in client windows (C#)
- newbie... Java load large picture :) Lit'l help plez (Java)
Other Threads in the C Forum
- Previous Thread: who sent the message "WM_CREATE" and "WM_PAINT"?
- Next Thread: what happened? Did I edited some .h file accidently???
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file floatingpointvalidation fork forloop frequency givemetehcodez grade gtkgcurlcompiling gtkwinlinux hacking highest histogram inches input intmain() iso kernel keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf opendocumentformat openwebfoundation owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault sequential single socket socketprograming socketprogramming stack standard string systemcall threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






