在阅读msdn中关于“Graphic Objects”的一篇文章时,发现了这样几句话:
“Each graphic-object class in the class library has a cast operator that will cast an MFC object to the associated Windows handle. The resulting handle is valid until the associated object detaches it. Use the object’s Detach member function to detach the handle.
The following code casts a CPen object to a Windows handle:
CPen myPen;
myPen.CreateSolidPen( PS_COSMETIC, 1, RGB(255,255,0) );
HPEN hMyPen = (HPEN) myPen; ”
其中的cast使我产生了联想,于是,我打开VC,并找到了CPen的类定义,在其中发现了这样一个函数的声明:
"operator HPEN() const;" ,
啊,难道这就是实现上面语句“HPEN hMyPen = (HPEN) myPen”的依据吗?
于是,我又找到了CBrush的类定义,并找到了一个类似的函数声明:
"operator HBRUSH() const;"
呵,答案已经在我心中了:)