TransparentStatic.h:class CTransparentStatic : public CStatic
{
// Construction
public:
CTransparentStatic();// Attributes
public:// Operations
public:
CFont* m_pfont; //字体指针
COLORREF m_crColor; //字颜色
UINT m_nFormat; //字位置// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTransparentStatic)
//}}AFX_VIRTUAL// Implementation
public:
virtual ~CTransparentStatic();protected:
CFont m_font; //字体// Generated message map functions
//{{AFX_MSG(CTransparentStatic)
afx_msg void OnPaint();
//}}AFX_MSGDECLARE_MESSAGE_MAP()
};
TransparentStatic.cpp://///////////////////////////////////////////////////////////////////////////
// CTransparentStaticCTransparentStatic::CTransparentStatic()
{
m_font.CreateFont
(
15, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"Tahoma" // lpszFacename
);
m_pfont = &m_font;
m_crColor = RGB(255, 255, 255);
m_nFormat = DT_LEFT;
}CTransparentStatic::~CTransparentStatic()
{
}BEGIN_MESSAGE_MAP(CTransparentStatic, CStatic)
//{{AFX_MSG_MAP(CTransparentStatic)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CTransparentStatic message handlersvoid CTransparentStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting// 取得位置
CRect client_rect;
GetClientRect(client_rect);// 取得文本
CString szText;
GetWindowText(szText);// 取得字体,并选入设备文件
dc.SelectObject(m_pfont);// 用透明背景填充设备文件
dc.SetBkMode(TRANSPARENT);// 显示文字
dc.SetTextColor(m_crColor);
dc.DrawText(szText, client_rect, m_nFormat);
}
代码比较简单,其实就是在OnPaint事件内重新填充背景及显示文字.如果想在程序中动态改变字体,颜色等信息,在修改了成员变量之后调用Invalidate(FALSE)刷新即可.现在又想到其实可以弄得更简单一些,写一个成员函数,传参数进去,在里面完成变量的修改和窗口刷新过程,这样一句话就够了.恩,过两天考试完了改写一下^_^/////////////////////////////////////////////////////////////////////////////ok,考试全部OVER了~现在贴上改进之后的CTransparentStatic:TransparentStatic.h:class CTransparentStatic : public CStatic
{
// Construction
public:
CTransparentStatic();// Attributes
public:// Operations
public:
void SetOutward(CFont* pfont, COLORREF crColor, UINT nFormat);CFont* GetpFont();
COLORREF GetColor();
UINT GetFormat();// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTransparentStatic)
//}}AFX_VIRTUAL// Implementation
public:
virtual ~CTransparentStatic();protected:
CFont* m_pfont; //字体指针
COLORREF m_crColor; //字颜色
UINT m_nFormat; //字位置// Generated message map functions
//{{AFX_MSG(CTransparentStatic)
afx_msg void OnPaint();
//}}AFX_MSGDECLARE_MESSAGE_MAP()
};TransparentStatic.cpp://///////////////////////////////////////////////////////////////////////////
// CTransparentStaticCTransparentStatic::CTransparentStatic()
{
m_pfont = new CFont;
m_pfont->CreateFont
(
15, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"Tahoma" // lpszFacename
);
m_crColor = RGB(255, 255, 255);
m_nFormat = DT_LEFT;
}CTransparentStatic::~CTransparentStatic()
{
delete m_pfont;
}void CTransparentStatic::SetOutward(CFont* pfont, COLORREF crColor, UINT nFormat)
{
if(pfont)
{
//获得LOGFONT
LOGFONT lf;
pfont ->GetLogFont(&lf);
//删除原先字体,创建新字体
delete m_pfont;
m_pfont = new CFont;
m_pfont ->CreateFontIndirect(&lf);
}
if(crColor)
m_crColor = crColor;
if(nFormat)
m_nFormat = nFormat;Invalidate(FALSE);
}CFont* CTransparentStatic::GetpFont()
{
return m_pfont;
}COLORREF CTransparentStatic::GetColor()
{
return m_crColor;
}UINT CTransparentStatic::GetFormat()
{
return m_nFormat;
}BEGIN_MESSAGE_MAP(CTransparentStatic, CStatic)
//{{AFX_MSG_MAP(CTransparentStatic)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CTransparentStatic message handlersvoid CTransparentStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting// 取得位置
CRect client_rect;
GetClientRect(client_rect);// 取得文本
CString szText;
GetWindowText(szText);// 取得字体,并选入设备文件
dc.SelectObject(m_pfont);// 用透明背景填充设备文件
dc.SetBkMode(TRANSPARENT);// 显示文字
dc.SetTextColor(m_crColor);
dc.DrawText(szText, client_rect, m_nFormat);
}
这次利用SetOutward成员函数来设置并刷新窗口,这样直接调用SetOutward就可以动态的调整文本的外观了.
比如调整字体颜色为红色:m_Static.SetOutward(NULL, RGB(255, 0, 0), NULL);即可~
{
// Construction
public:
CTransparentStatic();// Attributes
public:// Operations
public:
CFont* m_pfont; //字体指针
COLORREF m_crColor; //字颜色
UINT m_nFormat; //字位置// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTransparentStatic)
//}}AFX_VIRTUAL// Implementation
public:
virtual ~CTransparentStatic();protected:
CFont m_font; //字体// Generated message map functions
//{{AFX_MSG(CTransparentStatic)
afx_msg void OnPaint();
//}}AFX_MSGDECLARE_MESSAGE_MAP()
};
TransparentStatic.cpp://///////////////////////////////////////////////////////////////////////////
// CTransparentStaticCTransparentStatic::CTransparentStatic()
{
m_font.CreateFont
(
15, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"Tahoma" // lpszFacename
);
m_pfont = &m_font;
m_crColor = RGB(255, 255, 255);
m_nFormat = DT_LEFT;
}CTransparentStatic::~CTransparentStatic()
{
}BEGIN_MESSAGE_MAP(CTransparentStatic, CStatic)
//{{AFX_MSG_MAP(CTransparentStatic)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CTransparentStatic message handlersvoid CTransparentStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting// 取得位置
CRect client_rect;
GetClientRect(client_rect);// 取得文本
CString szText;
GetWindowText(szText);// 取得字体,并选入设备文件
dc.SelectObject(m_pfont);// 用透明背景填充设备文件
dc.SetBkMode(TRANSPARENT);// 显示文字
dc.SetTextColor(m_crColor);
dc.DrawText(szText, client_rect, m_nFormat);
}
代码比较简单,其实就是在OnPaint事件内重新填充背景及显示文字.如果想在程序中动态改变字体,颜色等信息,在修改了成员变量之后调用Invalidate(FALSE)刷新即可.现在又想到其实可以弄得更简单一些,写一个成员函数,传参数进去,在里面完成变量的修改和窗口刷新过程,这样一句话就够了.恩,过两天考试完了改写一下^_^/////////////////////////////////////////////////////////////////////////////ok,考试全部OVER了~现在贴上改进之后的CTransparentStatic:TransparentStatic.h:class CTransparentStatic : public CStatic
{
// Construction
public:
CTransparentStatic();// Attributes
public:// Operations
public:
void SetOutward(CFont* pfont, COLORREF crColor, UINT nFormat);CFont* GetpFont();
COLORREF GetColor();
UINT GetFormat();// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTransparentStatic)
//}}AFX_VIRTUAL// Implementation
public:
virtual ~CTransparentStatic();protected:
CFont* m_pfont; //字体指针
COLORREF m_crColor; //字颜色
UINT m_nFormat; //字位置// Generated message map functions
//{{AFX_MSG(CTransparentStatic)
afx_msg void OnPaint();
//}}AFX_MSGDECLARE_MESSAGE_MAP()
};TransparentStatic.cpp://///////////////////////////////////////////////////////////////////////////
// CTransparentStaticCTransparentStatic::CTransparentStatic()
{
m_pfont = new CFont;
m_pfont->CreateFont
(
15, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"Tahoma" // lpszFacename
);
m_crColor = RGB(255, 255, 255);
m_nFormat = DT_LEFT;
}CTransparentStatic::~CTransparentStatic()
{
delete m_pfont;
}void CTransparentStatic::SetOutward(CFont* pfont, COLORREF crColor, UINT nFormat)
{
if(pfont)
{
//获得LOGFONT
LOGFONT lf;
pfont ->GetLogFont(&lf);
//删除原先字体,创建新字体
delete m_pfont;
m_pfont = new CFont;
m_pfont ->CreateFontIndirect(&lf);
}
if(crColor)
m_crColor = crColor;
if(nFormat)
m_nFormat = nFormat;Invalidate(FALSE);
}CFont* CTransparentStatic::GetpFont()
{
return m_pfont;
}COLORREF CTransparentStatic::GetColor()
{
return m_crColor;
}UINT CTransparentStatic::GetFormat()
{
return m_nFormat;
}BEGIN_MESSAGE_MAP(CTransparentStatic, CStatic)
//{{AFX_MSG_MAP(CTransparentStatic)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CTransparentStatic message handlersvoid CTransparentStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting// 取得位置
CRect client_rect;
GetClientRect(client_rect);// 取得文本
CString szText;
GetWindowText(szText);// 取得字体,并选入设备文件
dc.SelectObject(m_pfont);// 用透明背景填充设备文件
dc.SetBkMode(TRANSPARENT);// 显示文字
dc.SetTextColor(m_crColor);
dc.DrawText(szText, client_rect, m_nFormat);
}
这次利用SetOutward成员函数来设置并刷新窗口,这样直接调用SetOutward就可以动态的调整文本的外观了.
比如调整字体颜色为红色:m_Static.SetOutward(NULL, RGB(255, 0, 0), NULL);即可~
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货