最近在写一个控件CMenuButton,初衷是提供设置偏移量的接口,可是ASSERT总是中断,调试发现::GetClientRect()返回的rect都是0值。
Google了一下,原来:
Q:“为何在OnCreate调用取出的就是0 呢 ?
在OnInitialUpdate中是可以调用的,返回数据也是正确的 ”
A:“OnCreate时还没创建出客户区 ”
void CMenuButton::SetOffset(int OffsetL, int OffsetR, int OffsetTop, int OffsetBottom)
{
//if out of client bound, then hold it in the bound
RECT client_rect;
::GetClientRect(m_hWnd, &client_rect);//返回值总是0?
OffsetL = OffsetL<0 ? 0 : OffsetL;
OffsetR = OffsetR<0 ? 0 : OffsetR;
OffsetTop = OffsetTop<0 ? 0 :OffsetTop;
OffsetBottom = OffsetBottom<0 ? 0 :OffsetBottom;
ATLASSERT(( (OffsetL+OffsetR < client_rect.right-client_rect.left) && (OffsetTop+OffsetBottom < client_rect.bottom-client_rect.top)));
m_nOffsetLeft = OffsetL;
m_nOffsetRight = OffsetR;
m_nOffsetTop = OffsetTop;
m_nOffsetBottom = OffsetBottom;
m_nItemWidth = client_rect.right-client_rect.left-m_nOffsetLeft-m_nOffsetRight;
ATLASSERT(m_nItemWidth > 0);
m_nItemHeight2 = m_nItemHeight1 = client_rect.bottom-client_rect.top-m_nOffsetBottom-m_nOffsetTop;
ATLASSERT(m_nItemHeight1>0);
}
...