在工作中经常需要使用CStatic 在该控件属性中设置透明 是无效的 需通过代码实现其透明
方法1
直接在父窗体的OnCtlColor中更改
HBRUSH CDlgStockPrice::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何特性
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
if (nCtlColor = CTLCOLOR_STATIC)
{
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)::GetStockObject(NULL_BRUSH);
}
return hbr;
}
#pragma once
// CColorStaticEx
class CColorStaticEx : public CStatic
{
DECLARE_DYNAMIC(CColorStaticEx)
public:
CColorStaticEx();
virtual ~CColorStaticEx();
protected:
DECLARE_MESSAGE_MAP()
public:
void SetFont(CFont font); //设置字体
void SetFontSize(int size); //设置字体大小
void SetTextColor(COLORREF color); //设置字体颜色
void SetHightFontColor(COLORREF color);
void SetHightTextSize(int size);
void IsDrawBorad(bool draw);
private:
void DrawBoard(CDC* dc,CRect rct);
private:
int m_size;
CString m_str;
CString m_curstr;
CFont m_font;
COLORREF m_text_color;
COLORREF m_text_high_color;
int m_text_high_size;
int m_text_size;
bool m_bDrawBoard;
public:
afx_msg void OnPaint();
void SetWindowText(LPCTSTR lpszString);
};
// ColorStaticEx.cpp : 实现文件
//
#include "stdafx.h"
#include "StockTradeClient.h"
#include "ColorStaticEx.h"
// CColorStaticEx
IMPLEMENT_DYNAMIC(CColorStaticEx, CStatic)
CColorStaticEx::CColorStaticEx()
{
m_text_size = 100;
m_text_high_size = 200;
m_text_color = RGB(0,0,0);
m_text_high_color = RGB(255,0,0);
m_curstr = "";
m_bDrawBoard = true;
// m_high_font.CreateFont(-24,0,0,0,FW_BOLD,FALSE,FALSE,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,_T("微软雅黑"));
}
CColorStaticEx::~CColorStaticEx()
{
}
BEGIN_MESSAGE_MAP(CColorStaticEx, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
void CColorStaticEx::SetFont(CFont font)
{
//m_font = font;
}
void CColorStaticEx::SetFontSize(int size)
{
m_font.CreatePointFont(size, _T("微软雅黑"));
}
void CColorStaticEx::SetTextColor(COLORREF color)
{
m_text_color = color;
}
void CColorStaticEx::SetHightFontColor(COLORREF color)
{
m_text_high_color = color;
}
void CColorStaticEx::SetHightTextSize(int size)
{
m_text_high_size = size;
}
void CColorStaticEx::IsDrawBorad(bool draw) //是否画边界
{
m_bDrawBoard = draw;
}
void CColorStaticEx::DrawBoard(CDC* dc,CRect rct) //绘制边界
{
CPen pen;
pen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
CPen *oldPen;
oldPen = dc->SelectObject(&pen);
dc->MoveTo(rct.left, rct.top);
dc->LineTo(rct.right, rct.top);
dc->LineTo(rct.right, rct.bottom);
dc->LineTo(rct.left, rct.bottom);
dc->LineTo(rct.left, rct.top);
dc->SelectObject(oldPen);
}
void CColorStaticEx::SetWindowText(LPCTSTR lpszString)
{
m_str = (CString)lpszString;
CRect rct;
GetWindowRect(&rct);
GetParent()->ScreenToClient(&rct);
GetParent()->InvalidateRect(&rct); //使区域无效 重绘防止出现文本重叠
}
void CColorStaticEx::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rct;
GetWindowRect(&rct);
ScreenToClient(&rct);
CPaintDC *pDC = &dc;
pDC->SetBkMode(TRANSPARENT);
/*内存绘图*/
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap m_MemMap;
m_MemMap.CreateCompatibleBitmap(pDC, rct.Width(), rct.Height());
memDC.SelectObject(&m_MemMap);
memDC.FillSolidRect(rct, RGB(236, 243, 246));
memDC.SetBkMode(TRANSPARENT);
if (m_curstr != m_str)
{
m_font.CreatePointFont(200, L"微软雅黑");
memDC.SetTextColor(m_text_high_color);
memDC.DrawText(m_str, rct, DT_VCENTER | DT_VCENTER | DT_SINGLELINE);
if (m_bDrawBoard) DrawBoard(&memDC,rct);
m_curstr = m_str;
}
else
{
memDC.SetTextColor(m_text_color);
memDC.DrawText(m_str, rct, DT_VCENTER | DT_VCENTER | DT_SINGLELINE);
if (m_bDrawBoard) DrawBoard(&memDC,rct);
}
/*复制到界面dc*/
pDC->BitBlt(rct.left, rct.top, rct.Width(), rct.Height(), &memDC, 0, 0, SRCCOPY);
/*清理内存*/
m_MemMap.DeleteObject();
memDC.DeleteDC();
}