送分啦:怎么改变Static Text 的背景颜色?

tobato 2001-11-21 07:11:59
气死我了!搞了老半天搞不定
...全文
591 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
tobato 2001-11-28
  • 打赏
  • 举报
回复
我来给分
tobato 2001-11-23
  • 打赏
  • 举报
回复
我看过做得最好得代码是从CStatic派生Class做得。效果很好!

"屁话,我的代码怎么不好使? "?? 你式过了吗?我可是Copy你得代码啊!确实不好

使,我都没有改过,Static得ID都是和你的代码一样得!

chenzhou35 2001-11-23
  • 打赏
  • 举报
回复
easy:

在头文件里定义一个CBrush brush;
在该窗口初始化时:
brush.CreateSolidBrush(RGB(255,0,0));
再重载:
HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

// TODO: Change any attributes of the DC here
if(pWnd->m_hWnd == m_static.m_hWnd )
{
pDC->SetBkColor(RGB(255,0,0));
return (HBRUSH)brush;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}

快!给分。
truman_lau 2001-11-22
  • 打赏
  • 举报
回复
能不能效果更好点?
重载对ON_CTLCOLOR消息的处理,用pDC->SetBKcolor改变文本框的背景色时,只有有文字输出的地方才有背景色,有谁知道怎样使整个文本框都变色?我在处理SDI的ON_ERASEBKGROUND消息是也遇到了同样的问题?
cat_dog 2001-11-22
  • 打赏
  • 举报
回复
这么长的代码?
nicolas 2001-11-22
  • 打赏
  • 举报
回复
上面的m_stInfo定义:CLabel m_stInfo;
nicolas 2001-11-22
  • 打赏
  • 举报
回复
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);
Jedi 2001-11-22
  • 打赏
  • 举报
回复
在classwizard中重载,WM_CTLCOLOR
if(nCtlColor==CTLCOLOR_STATIC)
pDC->SetBkColor(RGB(255,0,0));
stevenW 2001-11-22
  • 打赏
  • 举报
回复
屁话,我的代码怎么不好使?
tobato 2001-11-22
  • 打赏
  • 举报
回复
综合大家的招:
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

CStatic* pStatic1;
CStatic* pStatic2;
pStatic1 = (CStatic*)GetDlgItem(IDC_STATICINFO1);
pStatic2 = (CStatic*)GetDlgItem(IDC_STATICINFO2);
// TODO: Change any attributes of the DC here
if(nCtlColor==CTLCOLOR_STATIC)
{
if(pWnd->m_hWnd == pStatic1->m_hWnd)
pDC->SetBkColor(RGB(255,255,255));
if(pWnd->m_hWnd == pStatic2->m_hWnd)
pDC->SetBkColor(RGB(255,255,255));
}
// TODO: Return a different brush if the default is not desired
return hbr;

但是还是不能完全变成白色, 还差一点点是灰色的,怪难看的!
还有没有办法?
tobato 2001-11-22
  • 打赏
  • 举报
回复
to stevenW(无情的雨) 你的代码不好使,
再高楼上的是全部变色。
stevenW 2001-11-22
  • 打赏
  • 举报
回复
HBRUSH CF2Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

// TODO: Change any attributes of the DC here
if(pWnd==(CWnd*)GetDlgItem(IDC_STATIC1))
pDC->SetBkColor(RGB(0,255,0));
// TODO: Return a different brush if the default is not desired
return hbr;
}
chenzhou35 2001-11-22
  • 打赏
  • 举报
回复
OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor );
有窗口指针传进来的。
if(pWnd.m_hWnd == m_SpecialStatic.m_hWnd)
pDC->SetBkColor(RGB(255,0,0));
tobato 2001-11-22
  • 打赏
  • 举报
回复
那会不会全都变了? 我要变特定Static Text的颜色阿!!
Flysnow 2001-11-21
  • 打赏
  • 举报
回复
晚了一步 ,同上
cdq 2001-11-21
  • 打赏
  • 举报
回复
同意wavecheng(CryingOwl)。。。
在classwizard中重载,WM_CTLCOLOR
wavecheng 2001-11-21
  • 打赏
  • 举报
回复
响应WM_CTLCOLOR;
if(nCtlColor==CTLCOLOR_STATIC)
pDC->SetBkColor(RGB(255,0,0));
mzm100 2001-11-21
  • 打赏
  • 举报
回复
自己从CStatic派生一个类,重载OnPain()

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧