首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

GetDlgCtrlID()怎么确定id

2012-09-10 
GetDlgCtrlID()如何确定id在OnCtrlColor中调用GetDlgCtrlID()如果有很多个子窗口如何确定返回的是哪个窗口

GetDlgCtrlID()如何确定id
在OnCtrlColor中调用GetDlgCtrlID() 如果有很多个子窗口 如何确定返回的是哪个窗口? 难道跟传入参数dc有关? 为什么这时候dc 也是确定的

[解决办法]
每个‘子窗口’都有ID。所以知道ID就是知道了窗口。
OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
[解决办法]

C/C++ code
HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {  // Call the base class implementation first! Otherwise, it may  // undo what we are trying to accomplish here.  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  // Are we painting the IDC_MYSTATIC control? We can use  // CWnd::GetDlgCtrlID() to perform the most efficient test.  if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)  {    // Set the text color to red.    pDC->SetTextColor(RGB(255, 0, 0));    // Set the background mode for text to transparent     // so background will show thru.    pDC->SetBkMode(TRANSPARENT);    // Return handle to our CBrush object.    hbr = m_brush;  }  return hbr;}
[解决办法]
pWnd->GetDlgCtrlID()==控件id
[解决办法]
情况是这样的:
每个控件都是继承CWnd,资源编号是唯一的,pDC也是各自控件都有的。

楼上这样重载,等于说页面内的所有该类型控件都出触发OnCtlColor函数

只有当符合该资源编号的时候,才会重新设置背景色和字体色。
[解决办法]
使用CWnd::OnCtlColor
HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor );

pDC 包含了子窗口的显示设备环境的指针。可能是临时的。 
pWnd消息发送者指针
nCtlColor 包含了下列值,指定了控件的类型:
CTLCOLOR_BTN 按钮控件 ·
CTLCOLOR_DLG 对话框 ·
CTLCOLOR_EDIT 编辑控件 ·
CTLCOLOR_LISTBOX 列表框控件
CTLCOLOR_MSGBOX 消息框 
CTLCOLOR_STATIC 静态控件等 

[解决办法]
比如下面这一段代码就可以同时判断你的控件的类型(nCtlColor == CTLCOLOR_EDITpWnd)和ID(pWnd-> GetDlgCtrlID() == IDC_EDIT1),
C/C++ code
if((pWnd-> GetDlgCtrlID() == IDC_EDIT1) && (nCtlColor == CTLCOLOR_EDIT)&&(m_num1=="pass"))    {    pDC-> SetTextColor(RGB(255,0,0)); //设置红色的文本    pDC-> SetBkColor(RGB(0,0,0)); //设置黑色的文本背景    pDC-> SetBkMode(OPAQUE); //设置是否为不透明  HBRUSH m_brMine = CreateSolidBrush(RGB(255,255,0)); //设置黄色的控件背景    return m_brMine; //作为约定,返回背景色对应的刷子句柄    } 

热点排行