Calculating a control position on a window

Reply

Join Date: Sep 2006
Posts: 5
Reputation: nhandal is an unknown quantity at this point 
Solved Threads: 0
nhandal nhandal is offline Offline
Newbie Poster

Calculating a control position on a window

 
1
  #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:
  1.  
  2. int wndHeight (HWND hwndhgt)
  3. {
  4. RECT wndRect;
  5. GetWindowRect(hwndhgt, &wndRect);
  6. return (wndRect.bottom - wndRect.top);
  7. }
This is the function for creating the treeview:

  1. /* Calculate the client area of the parent window */
  2.  
  3.  
  4. GetClientRect(hwnd, &rcClient);
  5.  
  6.  
  7. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Calculating a control position on a window

 
1
  #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 Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Calculating a control position on a window

 
0
  #3
Sep 10th, 2006
Can you do something like this in the WM_SIZE event and tell me what happens?

  1. case WM_SIZE:
  2. {
  3. RECT rectTreeView, rectStatus, rectTool;
  4. UINT uToolHeight, uStatusHeight, uClientAlreaHeight;
  5.  
  6. SendMessage(hwndToolBar, TB_AUTOSIZE, 0, 0);
  7. SendMessage(hwndStatusBar, WM_SIZE, 0, 0);
  8.  
  9. GetClientRect(hWnd, &rectTreeView);
  10. GetWindowRect(hwndStatusBar, &rectStatus);
  11. GetWindowRect(hwndToolBar, &rectTool);
  12.  
  13. uToolHeight = rectTool.bottom - rectTool.top;
  14. uStatusHeight = rectStatus.bottom - rectStatus.top;
  15. uClientAlreaHeight = rectTreeView.bottom;
  16.  
  17. SetWindowPos(hwndTreeView, HWND_TOP, 0, uToolHeight, rectTreeView.right, uClientAlreaHeight - uStatusHeight - uToolHeight, FALSE);
  18. return 0;
  19. }
Last edited by WolfPack; Sep 11th, 2006 at 2:42 am.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 5
Reputation: nhandal is an unknown quantity at this point 
Solved Threads: 0
nhandal nhandal is offline Offline
Newbie Poster

Re: Calculating a control position on a window

 
1
  #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?


  1.  
  2. #include <windows.h>
  3. #include <commctrl.h>
  4. #include "resource.h"
  5.  
  6. #define IDC_STATUSBAR 101
  7. #define IDC_TOOLBAR 102
  8. #define IDC_TREEVIEW 103
  9.  
  10. #define TOOLBUTTONS 3
  11.  
  12. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  13. int wndHeight(HWND hwndhgt);
  14.  
  15. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16. PSTR szCmdLine, int iCmdShow)
  17. {
  18. static TCHAR szAppName[] = TEXT ("MyProgram") ;
  19. HWND hwnd ;
  20. MSG msg ;
  21. WNDCLASS wndclass ;
  22.  
  23. InitCommonControls();
  24.  
  25. wndclass.style = CS_HREDRAW | CS_VREDRAW ;
  26. wndclass.lpfnWndProc = WndProc ;
  27. wndclass.cbClsExtra = 0 ;
  28. wndclass.cbWndExtra = 0 ;
  29. wndclass.hInstance = hInstance ;
  30. wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
  31. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
  32. wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  33. wndclass.lpszMenuName = szAppName ;
  34. wndclass.lpszClassName = szAppName ;
  35.  
  36. if (!RegisterClass (&wndclass))
  37. {
  38. MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  39. szAppName, MB_ICONERROR) ;
  40. return 0 ;
  41. }
  42. hwnd = CreateWindow (szAppName, // window class name
  43. TEXT ("My Program"), // window caption
  44. WS_OVERLAPPEDWINDOW |
  45. WS_CLIPCHILDREN, // window style
  46. CW_USEDEFAULT, // initial x position
  47. CW_USEDEFAULT, // initial y position
  48. CW_USEDEFAULT, // initial x size
  49. CW_USEDEFAULT, // initial y size
  50. NULL, // parent window handle
  51. NULL, // window menu handle
  52. hInstance, // program instance handle
  53. NULL) ; // creation parameters
  54.  
  55.  
  56.  
  57. ShowWindow (hwnd, iCmdShow) ;
  58. UpdateWindow (hwnd) ;
  59.  
  60. while (GetMessage (&msg, NULL, 0, 0))
  61. {
  62. TranslateMessage (&msg) ;
  63. DispatchMessage (&msg) ;
  64. }
  65. return msg.wParam ;
  66. }
  67.  
  68. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  69. {
  70.  
  71. static HWND hStatusBar, hToolBar, hTreeView;
  72. int iStatusBarWidths[] = {300, 600, -1};
  73. TBBUTTON tbButtons[TOOLBUTTONS];
  74. TBADDBITMAP tbAbitmaps;
  75. RECT rcClient;
  76.  
  77. switch (message)
  78. {
  79. case WM_CREATE:
  80.  
  81. /* Create the Status bar and set its parts */
  82. hStatusBar = CreateWindowEx(0, STATUSCLASSNAME, NULL,
  83. WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
  84. hwnd, (HMENU)IDC_STATUSBAR, GetModuleHandle(NULL), NULL);
  85.  
  86.  
  87. SendMessage(hStatusBar, SB_SETPARTS, sizeof(iStatusBarWidths)/sizeof(int), (LPARAM)iStatusBarWidths);
  88.  
  89.  
  90. /* Create the ToolBar & send backwark compatabiltiy message */
  91. hToolBar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
  92. hwnd, (HMENU)IDC_TOOLBAR, GetModuleHandle(NULL), NULL);
  93. SendMessage(hToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
  94. tbAbitmaps.hInst = HINST_COMMCTRL;
  95. tbAbitmaps.nID = IDB_STD_LARGE_COLOR;
  96. SendMessage(hToolBar, TB_ADDBITMAP, 0, (LPARAM)&tbAbitmaps);
  97. ZeroMemory(tbButtons, sizeof(tbButtons));
  98. tbButtons[0].iBitmap = STD_FILENEW;
  99. tbButtons[0].fsState = TBSTATE_ENABLED;
  100. tbButtons[0].fsStyle = TBSTYLE_BUTTON;
  101.  
  102. tbButtons[1].iBitmap = STD_FILEOPEN;
  103. tbButtons[1].fsState = TBSTATE_ENABLED;
  104. tbButtons[1].fsStyle = TBSTYLE_BUTTON;
  105.  
  106. tbButtons[2].iBitmap = STD_FILESAVE;
  107. tbButtons[2].fsState = TBSTATE_ENABLED;
  108. tbButtons[2].fsStyle = TBSTYLE_BUTTON;
  109.  
  110. SendMessage(hToolBar, TB_ADDBUTTONS, sizeof(tbButtons)/sizeof(TBBUTTON), (LPARAM)&tbButtons);
  111.  
  112. /* Create the Tree View Control */
  113.  
  114. GetClientRect(hwnd, &rcClient);
  115.  
  116. hTreeView = CreateWindowEx(0, WC_TREEVIEW, NULL, WS_CHILD | WS_VISIBLE |WS_BORDER,
  117. 0, wndHeight(hToolBar), 200, rcClient.bottom - wndHeight (hToolBar) - wndHeight (hStatusBar),
  118. hwnd, (HMENU)IDC_TREEVIEW, GetModuleHandle(NULL), NULL);
  119.  
  120. return 0 ;
  121.  
  122.  
  123. case WM_SIZE:
  124. SendMessage(GetDlgItem(hwnd, IDC_STATUSBAR), WM_SIZE, wParam, lParam);
  125. SendMessage(GetDlgItem(hwnd, IDC_TOOLBAR), TB_AUTOSIZE, 0, 0);
  126. GetClientRect(hwnd, &rcClient);
  127. MoveWindow(hTreeView,0, wndHeight(hToolBar), 200,
  128. rcClient.bottom - wndHeight (hToolBar) - wndHeight (hStatusBar), TRUE);
  129.  
  130. return 0;
  131. /*This code is for displaying menu help in the status bar */
  132. case WM_MENUSELECT: {
  133. switch (LOWORD(wParam))
  134. {
  135. case ID_FILE_EXIT:
  136. SendMessage(GetDlgItem(hwnd, IDC_STATUSBAR), SB_SETTEXT, 0, (LPARAM)(LPSTR)" Quit program");
  137. break;
  138. default:
  139. SendMessage(GetDlgItem(hwnd, IDC_STATUSBAR), SB_SETTEXT, 255, (LPARAM)(LPSTR)"Ready");
  140. }
  141.  
  142. return 0;
  143. }
  144.  
  145. case WM_DESTROY:
  146. PostQuitMessage (0) ;
  147. return 0 ;
  148. }
  149. return DefWindowProc (hwnd, message, wParam, lParam) ;
  150. }
  151.  
  152.  
  153. int wndHeight (HWND hwndhgt)
  154. {
  155. RECT wndRect;
  156. GetWindowRect(hwndhgt, &wndRect);
  157. return (wndRect.bottom - wndRect.top);
  158. }

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:

  1.  
  2. GetClientRect(hwnd, &rcClient);
  3.  
  4. MoveWindow(hTreeView,0, wndHeight(hToolBar), 200,
  5. 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 Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Calculating a control position on a window

 
1
  #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?
  1. 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 Quick reply to this message  
Join Date: Sep 2006
Posts: 5
Reputation: nhandal is an unknown quantity at this point 
Solved Threads: 0
nhandal nhandal is offline Offline
Newbie Poster

Re: Calculating a control position on a window

 
1
  #6
Sep 12th, 2006
Thanks, it works now.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC