windows 如何从 CDC 对象获取 HDC 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1234303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get an HDC object from a CDC object?
提问by samoz
回答by Kirill V. Lyadvinsky
When you have CDC
object it will be implicitly converted to HDC
when necessary:
当您拥有CDC
对象时,它将HDC
在必要时隐式转换为:
CDC dc;
HDC hdc = dc; // HDC hdc = dc.operator HDC();
If you have pointer to CDC
object then using function GetSafeHdc
will look more clear:
如果您有指向CDC
对象的指针,则使用函数GetSafeHdc
看起来会更清晰:
CDC* pdc = SOME;
HDC hdc = pdc->GetSafeHdc();
回答by Naveen
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 类已operator HDC()
定义允许编译器将 CDC 对象隐式转换为 HDC。因此,如果您有 CDC* 和一个采用 HDC 的函数,那么您只需取消引用指针并将其发送给函数。
回答by CB Bailey
CDC
is a C++ class which - to a reasonable approximation - encapsulates an HDC, which is a handle to a device context.
CDC
是一个 C++ 类 - 合理地近似 - 封装了一个 HDC,它是一个设备上下文的句柄。
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 the CDC
instance encapsulates.
您链接到的文档描述了转换operator
,这是一种 C++ 构造,类可以提供该构造以允许从类的实例到其他类型的隐式转换。在这种情况下,隐式转换会产生CDC
实例封装的底层句柄 (HDC) 。
You can perform the conversion by using a CDC
instance anywhere were it needs to be converted to an HDC
.
您可以在CDC
任何需要将其转换为HDC
.
Most simply:
最简单的:
void f( const CDC& cdc )
{
HDC hdc = cdc;
// use hdc here
}
回答by bdonlan
HDC hDC = dc;
回答by ChrisW
Just assign it.
只分配它。
CDC cdc = something.
HDC hdc = cdc;
if (hdc != 0)
{
//success...
}