怎样把 STATIC 静态文本的背景改变颜色,解决了我会把分加满!
我看论坛上有这个东西,我VC水平实在不行,根本不会用
HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
UINT id=pWnd->GetDlgCtrlID();
if (id==IDC_STATIC1) //改变 id为IDC_EMAIL static 的颜色
{
pDC->SetTextColor(RGB(0,0,255));
}
return hbr;
}
我就想做三个按钮,点击第一个,静态框变红色;点击第二个,静态框变绿色;点第三个,静态框变红黄色,我怎么来使用上面的函数,请大虾给细节指导!
问题点数:100、回复次数:10Top
1 楼Featured(我握着爱情的门票静静排队……)回复于 2005-04-04 18:59:52 得分 2
不要用这个函数,直接
SetBkColorTop
2 楼flyelf(空谷清音)回复于 2005-04-04 19:05:37 得分 0
用一个变量来记录当前的状态Top
3 楼waterczh(三十年河东三十年河西,我TMD是掉到河里了)回复于 2005-04-04 19:17:05 得分 2
设置一个变量,在按钮响应函数里分别赋值,在OnCtlColor里根据不同变量值返回不同颜色,按下按钮时刷新窗口就有了Top
4 楼__A__(無羈の心)回复于 2005-04-04 19:30:29 得分 0
改变背景不用这么复杂啊,你写的那个是改变字的颜色的吧~~~Top
5 楼zhaotao0982(victor)回复于 2005-04-04 20:33:10 得分 0
《mfc程序设计》上有代码,自己找找看!Top
6 楼bodan(dan)回复于 2005-04-04 21:30:25 得分 0
我怎么获取我静态文本的句柄?用GetDC吗?怎么用的,给写个小实例吧!Top
7 楼swordning(while(1) money++;)回复于 2005-04-04 22:29:14 得分 3
CWnd *pWnd;
pWnd=(CWnd*)GetDlgItem(IDC_STATIC1);
CDC *dc=pWnd->GetDC();Top
8 楼wshcdr(dd)回复于 2005-04-04 22:50:32 得分 3
HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
UINT id=pWnd->GetDlgCtrlID();
if (id==IDC_STATIC1) //改变 id为IDC_EMAIL static 的颜色
{
return CBrush(RGB(255,0,0)) ;
}
return hbr;
}Top
9 楼signoft(晴天)回复于 2005-04-04 23:45:19 得分 20
如果实现楼主的问题我想不能用OnCtlColor这个方法,这个方法虽然能够改变背景颜色,但是不能够动态改变,楼上已经有了很多解决方法,也可以重载一个CStatic类,可以重写很多有用的操作,代码如下-------------->
1.头文件
#if !defined(AFX_LABEL_H__A4EABEC5_2E8C_11D1_B79F_00805F9ECE10__INCLUDED_)
#define AFX_LABEL_H__A4EABEC5_2E8C_11D1_B79F_00805F9ECE10__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// Label.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CLabel window
enum FlashType {None, Text, Background };
class CLabel : public CStatic
{
// Construction
public:
CLabel();
CLabel& SetBkColor(COLORREF crBkgnd);
CLabel& SetTextColor(COLORREF crText);
CLabel& SetText(const CString& strText);
CLabel& SetFontBold(BOOL bBold);
CLabel& SetFontName(const CString& strFont);
CLabel& SetFontUnderline(BOOL bSet);
CLabel& SetFontItalic(BOOL bSet);
CLabel& SetFontSize(int nSize);
CLabel& SetSunken(BOOL bSet);
CLabel& SetBorder(BOOL bSet);
CLabel& FlashText(BOOL bActivate);
CLabel& FlashBackground(BOOL bActivate);
CLabel& SetLink(BOOL bLink);
CLabel& SetLinkCursor(HCURSOR hCursor);
// Attributes
public:
protected:
void ReconstructFont();
COLORREF m_crText;
HBRUSH m_hBrush;
HBRUSH m_hwndBrush;
LOGFONT m_lf;
CFont m_font;
CString m_strText;
BOOL m_bState;
BOOL m_bTimer;
BOOL m_bLink;
FlashType m_Type;
HCURSOR m_hCursor;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CLabel)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CLabel();
// Generated message map functions
//{{AFX_MSG(CLabel)
protected:
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_LABEL_H__A4EABEC5_2E8C_11D1_B79F_00805F9ECE10__INCLUDED_)
2.实现文件
// Label.cpp : implementation file
//
#include "stdafx.h"
#include "Resource.h"
#include "Label.h"
#include "globaltype.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLabel
CLabel::CLabel()
{
m_crText = GetSysColor(COLOR_WINDOWTEXT);
m_hBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_lf),&m_lf);
m_font.CreateFontIndirect(&m_lf);
m_bTimer = FALSE;
m_bState = FALSE;
m_bLink = TRUE;
m_hCursor = NULL;
m_Type = None;
m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
}
CLabel::~CLabel()
{
m_font.DeleteObject();
::DeleteObject(m_hBrush);
}
CLabel& CLabel::SetText(const CString& strText)
{
SetWindowText(strText);
return *this;
}
CLabel& CLabel::SetTextColor(COLORREF crText)
{
m_crText = crText;
RedrawWindow();
return *this;
}
CLabel& CLabel::SetFontBold(BOOL bBold)
{
m_lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
ReconstructFont();
RedrawWindow();
return *this;
}
CLabel& CLabel::SetFontUnderline(BOOL bSet)
{
m_lf.lfUnderline = bSet;
ReconstructFont();
RedrawWindow();
return *this;
}
CLabel& CLabel::SetFontItalic(BOOL bSet)
{
m_lf.lfItalic = bSet;
ReconstructFont();
RedrawWindow();
return *this;
}
CLabel& CLabel::SetSunken(BOOL bSet)
{
if (!bSet)
ModifyStyleEx(WS_EX_STATICEDGE,0,SWP_DRAWFRAME);
else
ModifyStyleEx(0,WS_EX_STATICEDGE,SWP_DRAWFRAME);
return *this;
}
CLabel& CLabel::SetBorder(BOOL bSet)
{
if (!bSet)
ModifyStyle(WS_BORDER,0,SWP_DRAWFRAME);
else
ModifyStyle(0,WS_BORDER,SWP_DRAWFRAME);
return *this;
}
CLabel& CLabel::SetFontSize(int nSize)
{
nSize*=-1;
m_lf.lfHeight = nSize;
ReconstructFont();
RedrawWindow();
return *this;
}
CLabel& CLabel::SetBkColor(COLORREF crBkgnd)
{
if (m_hBrush)
::DeleteObject(m_hBrush);
m_hBrush = ::CreateSolidBrush(crBkgnd);
return *this;
}
CLabel& CLabel::SetFontName(const CString& strFont)
{
strcpy(m_lf.lfFaceName,strFont);
ReconstructFont();
RedrawWindow();
return *this;
}
BEGIN_MESSAGE_MAP(CLabel, CStatic)
//{{AFX_MSG_MAP(CLabel)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
//2001-10-12-feng---------------------
ON_WM_PAINT()
//------------------------------------
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLabel message handlers
HBRUSH CLabel::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Change any attributes of the DC here
// TODO: Return a non-NULL brush if the parent's handler should not be called
if (CTLCOLOR_STATIC == nCtlColor)
{
pDC->SelectObject(&m_font);
pDC->SetTextColor(m_crText);
pDC->SetBkMode(TRANSPARENT);
}
if (m_Type == Background)
{
if (!m_bState)
return m_hwndBrush;
}
return m_hBrush;
}
void CLabel::ReconstructFont()
{
m_font.DeleteObject();
BOOL bCreated = m_font.CreateFontIndirect(&m_lf);
ASSERT(bCreated);
}
CLabel& CLabel::FlashText(BOOL bActivate)
{
if (m_bTimer)
{
SetWindowText(m_strText);
KillTimer(1);
}
if (bActivate)
{
GetWindowText(m_strText);
m_bState = FALSE;
m_bTimer = TRUE;
SetTimer(1,500,NULL);
m_Type = Text;
}
return *this;
}
CLabel& CLabel::FlashBackground(BOOL bActivate)
{
if (m_bTimer)
KillTimer(1);
if (bActivate)
{
m_bState = FALSE;
m_bTimer = TRUE;
SetTimer(1,500,NULL);
m_Type = Background;
}
return *this;
}
void CLabel::OnTimer(UINT nIDEvent)
{
m_bState = !m_bState;
switch (m_Type)
{
case Text:
if (m_bState)
SetWindowText("");
else
SetWindowText(m_strText);
break;
case Background:
InvalidateRect(NULL,FALSE);
UpdateWindow();
break;
}
CStatic::OnTimer(nIDEvent);
}
CLabel& CLabel::SetLink(BOOL bLink)
{
m_bLink = bLink;
if (bLink)
ModifyStyle(0,SS_NOTIFY);
else
ModifyStyle(SS_NOTIFY,0);
return *this;
}
void CLabel::OnLButtonDown(UINT nFlags, CPoint point)
{
CString strLink;
GetWindowText(strLink);
//2001-10-12--------------------------------------------------
if (strlen((char *)(LPCSTR)strLink)>0)
//------------------------------------------------------------
ShellExecute(NULL,"open",strLink,NULL,NULL,SW_SHOWNORMAL);
CStatic::OnLButtonDown(nFlags, point);
}
BOOL CLabel::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (m_hCursor)
{
::SetCursor(m_hCursor);
return TRUE;
}
return CStatic::OnSetCursor(pWnd, nHitTest, message);
}
CLabel& CLabel::SetLinkCursor(HCURSOR hCursor)
{
m_hCursor = hCursor;
return *this;
}
3.用法(假如m_stInfo是CLabel)
m_stInfo
.SetFontName("宋体")
.SetFontSize(12)
.SetTextColor(RGB(0,255,0))
.SetFontUnderline(FALSE)
.SetBkColor(RGB(0,0,0))
.SetFontItalic(FALSE)
.SetFontBold(FALSE)
.SetBorder(FALSE)
.SetSunken(TRUE);
Top
10 楼mengqimxz(雨)回复于 2005-04-05 08:29:23 得分 70
去网上搜索一下有好多(CLabel.h,CLabel.cpp)添加到你的项目中。然后给你的静态文本框的ID值不要用默认的,然后就可以给他添控制变量了(CLabel型的控制变量),
如你的控制变量为m_LoadLab_Con
m_LoadLab_Con.SetFontSize(40)
.SetBkColor(RGB(0,0,0))
.SetFontName("黑体")
.SetTextColor(RGB(121,251,220));
上面的你就可以看懂了吧,你的程序可以设置标志位来改变静态文本框的颜色了。Top




