本站启用国内永久域名: www.LewenSky.cn,请大家使用新域名访问网站!
8月14

MFC中改变STATIC文字的颜色

| |
23:14MFC/VC++  From: KingsamChen
这个问题MS让很多人伤透了脑筋~偶最初也因为不知道如何改变颜色而浪费了很多时间~后来经过Google+MSDN和自己的摸索,找到了其中的一种方法。(MFC中实现同一种效果往往有很多方法)

  首先我们在对话框中添加OnCtlColor消息映射函数,函数作用/函数原型/参数解释请看MSDN描述~
引用

CWnd::OnCtlColor See Also
CWnd Overview | Class Members | Hierarchy Chart | CDC::SetBkColor
The framework calls this member function when a child control is about to be drawn.

afx_msg HBRUSH OnCtlColor(
   CDC* pDC,
   CWnd* pWnd,
   UINT nCtlColor
);
Parameters
pDC
Contains a pointer to the display context for the child window. May be temporary.
pWnd
Contains a pointer to the control asking for the color. May be temporary.
nCtlColor
Contains one of the following values, specifying the type of control:
CTLCOLOR_BTN   Button control
CTLCOLOR_DLG   Dialog box
CTLCOLOR_EDIT   Edit control
CTLCOLOR_LISTBOX   List-box control
CTLCOLOR_MSGBOX   Message box
CTLCOLOR_SCROLLBAR   Scroll-bar control
CTLCOLOR_STATIC   Static control
Return Value
OnCtlColor must return a handle to the brush that is to be used for painting the control background.


然后我们在对话框上放置两个STATIC控件,ID分别为:IDC_STCCOLOR和IDC_STCTWO。然后在对话框的类中添加成员变量(别告诉我你不知道怎么添加……),变量需要两个,每个成员变量对应一个控件~

  ps:变量种类要选择控件变量~这要的控件变量的类型一般是CStatic~

  然后我们在刚才的那个消息映射函数中写下下列代码:
引用

HBRUSH CMFCDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

  if (GetDlgItem(IDC_STCCOLOR) == pWnd)
  {
    pDC->SetTextColor(RGB(255, 0, 0));
  }
  else if (GetDlgItem(IDC_STCTWO) == pWnd)  
  {
    pDC->SetTextColor(RGB(0, 0, 255));
  }

  return hbr;
}

这里之所以要先判断句柄,是因为我们要针对某一个控件进行设置。如果你直接使用pDC->SetTextColor来设置,那么MFC会遍历窗体的所有控件,然后把颜色设置成一样。

  至于为什么要添加控件的成员变量,我们可以在对话框的CPP文件中发现这段代码:
引用

void CMFCDialogDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_STCCOLOR, m_cstcColor);
DDX_Control(pDX, IDC_STCTWO, m_cstcTwo);
}

DDE数据交换呵,如果你把两个成员变量的DDE交换给他注释掉,调式后就会发现,颜色无法改变了。

  至于为什么,我也不知道。可能是MFC的某种机制吧~其实之前我想过这个是否是因为颜色是状态量,需要一个变量来维持。但是文字内容也是状态量,为什么不需要添加成员变量就能够改变?如果谁知到,麻烦请告诉我~



出处:KingsamChen
转载时必须以链接形式注明出处及本声明!


最新日志 随机日志 综合排行
  • OFFICE 2010 VOL...
  • 格式工厂绿色版 V2.90
  • Password Invent...
  • VideoCacheView ...
  • 电脑店U盘启动盘制作工具 3....
  • 阿里旺旺AliIM2011_7...
  • 极点五笔十周年纪念版
  • PPTV网络电视3.1.1.0...
  • WinRAR注册方法
  • 紫光华宇拼音输入法 6.8.0...
  • CCTVLive 1.02 绿...
  • Pcomm Pro (串口通讯...
  • PPS网络电视 v2.6.86...
  • MyLastSearch 1....
  • 超级巡警 V4.0 build...
  • [大战外星人/怪兽大战外星人]...
  • [03-16]每日绿茶软件友情...
  • ACDSee Pro 4.0....
  • 暴风影音2012 在线高清影院...
  • 酷狗音乐2010新春版(屏蔽酷...
  • 安装软件时出现系统提示&quo...
  • XP与XP无线共享设置(笔记本...
  • ZineMaker目录模板大集...
  • 没有不可能 轻松实现PDG转P...
  • CString转char的方法...
  • [转]MSDN精简版、MSDN...
  • 将IE、FIREFOR (火狐...
  • VC多线程编程(转)
  • MyEclipse6.5下载地...
  • Diskeeper 2010 ...
  • Tags: ,
    阅读(3267) | 评论(0) | 引用(0)
    发表评论
    表情
    emotemotemotemotemot
    emotemotemotemotemot
    emotemotemotemotemot
    emotemotemotemotemot
    emotemotemotemotemot
    打开HTML
    打开UBB
    打开表情
    隐藏
    记住我
    昵称   密码   游客无需密码
    网址   电邮   [注册]