注册 登录
编程论坛 C++教室

求助OnCtlColor的第二个参数

coldlxh 发布于 2013-01-31 22:05, 283 次点击
OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
这个函数是响应WM_CTLCOLOR消息的,是用于控件的绘制时向父类发送的一个消息,请求控件的绘制颜色.
在这个函数中.
我用了两个if

if (pWnd->GetDlgCtrlID() == IDC_EDIT1)//通过控件的指针调用GetDlgCtrlID()方法得到他的ID

if(GetDlgItem(IDC_EDIT1) == pWnd)//通过控件的ID得到该控件的指针
第一个可以成功,第二条竟然不能成功.我有点迷糊,竟然同一个控件的指针还能不一样
2 回复
#2
rjsp2013-02-01 08:25
每一个控件有一个唯一的Handle
而CWnd只是Handle的代理类,所以可能多个不同的CWnd控制的是同一个Handle
你可以试试 if( GetDlgItem(IDC_EDIT1)->GetSafeHandle() == pWnd->GetSafeHandle() )
#3
yuccn2013-02-01 12:20
if(GetDlgItem(IDC_EDIT1) == pWnd) 这个大多数情况下是不能成立的。
GetDlgItem(IDC_EDIT1)返回的可能是临时被创建的。

下面是msdn上面的说法

Return Value

--------------------------------------------------------------------------------



A pointer to the given control or child window. If no control with the integer ID given by the nID parameter exists, the value is NULL.

The returned pointer may be temporary and should not be stored for later use.

1