Client Size - GetClien
when called from CDialogBar::OnCreate(), GetClientRect() is not returning the size of the CDialogBar as it would when called from CDialogBar::OnEraseBkgnd(CDC* pDC) with pDC->GetClipBox(); Why and how to get the full width and height of the CDialogBar upon creation ?
The CDialogBar is actually a class I inherited from CDialogBar. In its OnCreate() I'm attempting to create a CTabCtrl which takes the whole width of the bar, but it doesn't work because of the wrong RECT size. The code looks like this:
afx_msg int Toolbar::OnCreate(LPCREATESTRUCT lpCs) {
if(-1 == TOOLBAR_PARENT::OnCreate(lpCs)) {
TRACE("failed to create toolbar");
return -1;
}
ModifyStyle(WS_CLIPCHILDREN,0);
RECT r;
GetClientRect(&r);
m_tabs.Create(TCS_TABS | WS_CHILD|WS_VISIBLE ,r,this,IDR_TOOLBARTABS);
m_tabs.InsertItem(0,_T("hello"));
m_tabs.InsertItem(1,_T("world"));
m_tabs.InsertItem(2,_T("world"));
m_tabs.InsertItem(3,_T("world"));
m_tabs.InsertItem(4,_T("world"));
m_tabs.InsertItem(5,_T("world"));
m_tabs.InsertItem(6,_T("world"));
m_tabs.InsertItem(7,_T("world"));
m_tabs.InsertItem(8,_T("world"));
m_tabs.InsertItem(9,_T("world"));
m_tabs.InsertItem(10,_T("world"));
return 0;
}
Thank you in advance.
[1334 byte] By [
bhavacakra] at [2007-11-20 8:26:57]

# 2 Re: Client Size - GetClien
I am positive that GetClientRect should have returned the expected values because you are using it after the base class call of OnCreate.
However try using the LPCREATESTRUCT's
cy
Specifies the height of the new window.
cx
Specifies the width of the new window.
y
Specifies the y-coordinate of the upper left corner of the new window. Coordinates are relative to the parent window if the new window is a child window; otherwise coordinates are relative to the screen origin.
x
Specifies the x-coordinate of the upper left corner of the new window. Coordinates are relative to the parent window if the new window is a child window; otherwise coordinates are relative to the screen origin.
# 3 Re: Client Size - GetClien
In my particular case it returns {0,0,495,37}, which is not the right value. I've checked it with Spy++(beside of the fact that I can see visually that the tab does not take up the whole width), it should be {0,0,1072,37}. There are no other windows in between, the tab control is the only child of the dialog bar. The dialog bar is created with the style WS_CHILD|WS_VISIBLE|CBRS_TOP, if that would have any relevance.
# 6 Re: Client Size - GetClien
I think I know where the problem is - the resource dialog bar used as template, that one is 495 in width. How to change the width and height of it at runtime ? At the moment I'm using it just as a placeholder so I can call Create in MainFrame::OnCreate(), actually I don't plan to use it. Could you tell me how to
1 ) change its width and height at runtime
2 ) modify the template in the resource editor so it stretches at full width of the parent
3 ) ideally, not having to use the template at all ?
# 9 Re: Client Size - GetClien
Why didn't I think of this before: :)
You should handle the WM_SIZE message or override the OnSize(...) function of the toolbar. And in this OnSize function you should size your tab control accordingly.
For sizing any window use the SetWindowsPos function.
There are two varieties of this function; one is a global Win API function and the other one is the CWnd's member function. You can use any of the two functions u like, but to go with the MFC's way I prefer the CWnd's member function.
But before you call SetWindowsPos from this OnSize function be sure the window (HWnd) of the tab control is created and valid.
To check this validity you can use ::IsWindow(...) function supplying the HWnd of the tab control
# 10 Re: Client Size - GetClien
for those interested, this did the trick:
afx_msg void Toolbar::OnSize(UINT nType,int cx,int cy) {
RECT r;
//GetWindowRect(&r);//<-- buggy, as the user can move part of the (left edge) off the screen
GetClientRect(&r);
m_tabs.SetWindowPos(this,0,0,r.right,r.bottom,SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOCOPYBITS|SWP_NOZORDER|SWP_FRAMECHANGED);
}