一个可根据当前文本调整自身长度的CStatic派生类

重载如下函数void CStaticAS::SetWindowText(LPCTSTR lpszString)
{
    CRect rect;
    CSize size(0,0);
    GetWindowRect(rect);
    GetParent()->ScreenToClient(&rect);

    CClientDC dc(this);
    CFont * pOldFont=dc.SelectObject (GetFont());

    size=dc.GetTextExtent (lpszString);

    rect.right =rect.left + size.cx ;
    rect.bottom =rect.top + size.cy ;
    //can't be 'rect+=size' !!!!!!!!
    //ClientToScreen(rect);

    CWnd::SetWindowText (lpszString);
    MoveWindow(rect);
    //SetWindowPos(&wndNoTopMost,0,0,size.cx ,size.cy ,SWP_NOMOVE   );
    dc.SelectObject(pOldFont);
    Invalidate();
    UpdateWindow();
派生类可以定义成员变量来保存传入的数量和文本内容,例如: ``` class CMyDialogBar : public CDialogBar { public: CMyDialogBar(int nTextCount, CStringArray& TextList, COLORREF* TextColorList, COLORREF* BackgroundColorList); // 构造函数 private: int m_nTextCount; // 静态文本框数量 CStringArray m_TextList; // 静态文本显示文本 CTypedPtrList<CPtrList, CStatic*> m_StaticList; // 存储静态文本框指针的链表 COLORREF* m_TextColorList; // 文本颜色数组 COLORREF* m_BackgroundColorList; // 背景色数组 CTypedPtrList<CPtrList, CBrush*> m_BrushList; // 存储画刷对象的链表 }; ``` 其中,`nTextCount`为静态文本框数量,`TextList`为静态文本显示文本的字符串数组,`TextColorList`和`BackgroundColorList`分别为文本颜色和背景色的数组。 在派生类的构造函数中,可以将传入的参数保存到成员变量中: ``` CMyDialogBar::CMyDialogBar(int nTextCount, CStringArray& TextList, COLORREF* TextColorList, COLORREF* BackgroundColorList) { m_nTextCount = nTextCount; m_TextList.Copy(TextList); m_TextColorList = TextColorList; m_BackgroundColorList = BackgroundColorList; } ``` 这样就可以在派生类的OnInitDialog函数中使用成员变量来动态生成静态文本框、设置显示文本文本颜色、背景色和画刷对象了。在创建派生类对象时,可以将传入参数作为构造函数的参数,例如: ``` CStringArray TextList; TextList.Add(_T("Text 1")); TextList.Add(_T("Text 2")); TextList.Add(_T("Text 3")); COLORREF TextColorList[] = { RGB(255, 0, 0), RGB(0, 255, 0), RGB(0, 0, 255) }; COLORREF BackgroundColorList[] = { RGB(255, 255, 255), RGB(255, 255, 255), RGB(255, 255, 255) }; CMyDialogBar* pMyDialogBar = new CMyDialogBar(TextList.GetCount(), TextList, TextColorList, BackgroundColorList); ``` 这样就可以在派生类中使用传入的数量和文本内容了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值