如何从 CDC 对象获取 HDC 对象?
I have an object, dc, of type CDC and I'd like to get an HDC object.
I read the MSDN documentation here, but don't really understand it.
Can someone provide me with a brief example/explanation on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您有 CDC 对象时,必要时它将隐式转换为 HDC:
如果您有指向 CDC 对象的指针,则使用函数 GetSafeHdc 看起来会更清晰:
When you have
CDC
object it will be implicitly converted toHDC
when necessary:If you have pointer to
CDC
object then using functionGetSafeHdc
will look more clear:CDC 类定义了
操作符 HDC()
,它允许编译器将 CDC 对象隐式转换为 HDC。 因此,如果您有 CDC* 和一个采用 HDC 的函数,那么您只需取消引用指针并将其发送到该函数即可。CDC class has
operator HDC()
defined which allows the compiler to convert a CDC object to HDC implicitly. Hence if you have CDC* and a function which takes HDC then you just dereference the pointer and send it to the function.CDC 是一个 C++ 类,在合理的近似范围内,它封装了一个 HDC,它是设备上下文的句柄。
您链接到的文档描述了转换运算符,它是类可以提供的 C++ 构造,以允许从类实例到其他类型的隐式转换。 在这种情况下,隐式转换会产生 CDC 实例封装的底层句柄 (HDC)。
您可以在任何需要转换为
HDC
的地方使用CDC
实例来执行转换。最简单:
CDC
is a C++ class which - to a reasonable approximation - encapsulates an HDC, which is a handle to a device context.The documenation which you link to describes a conversion
operator
, which is a C++ construct that classes can supply to allow implicit conversion from an instance of a class to some other type. In this case the implicit conversion results in the underlying handle (HDC) which theCDC
instance encapsulates.You can perform the conversion by using a
CDC
instance anywhere were it needs to be converted to anHDC
.Most simply:
只需分配它即可。
Just assign it.