静态文本框(CStatic)自绘

概要:

透明背景

详细内容:

透明背景:

  • 第一种方法

控件子类化:

class CStaticEx : public CStatic

响应WM_PAINT消息:

void CStaticEx::OnPaint()

{

    CPaintDC dc(this); // device context for painting

    // TODO: 背景透明

    CRect rect;

    GetClientRect(&rect);

    CString strText;

    GetWindowText(strText);

    CFont *pFont, *pOldFont;

    pFont = GetFont();

    pOldFont = dc.SelectObject(pFont);

    dc.SetBkMode(TRANSPARENT);

    dc.DrawText(strText, &rect, 0);

    dc.SelectObject(pOldFont);

    // Do not call CStatic::OnPaint() for painting messages

}
  • 第二种方法:

父窗口响应WM_CTLCOLOR消息:

HBRUSH CCStatic_0100Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

{

    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);



    // TODO:  Change any attributes of the DC here

    if(pWnd->GetDlgCtrlID() == IDC_STATIC1)

    {

        pDC->SetBkMode(TRANSPARENT);

        pDC->SetTextColor(RGB(255, 0, 128));

        return (HBRUSH)GetStockObject(NULL_BRUSH);

    }

    // TODO:  Return a different brush if the default is not desired

    return hbr;

}
  • 不过子控件调用SetWindowText函数设置文本时,文本会重叠

你可能感兴趣的:(static)