如何从 CDC 对象获取 HDC 对象?

发布于 2024-07-30 09:53:32 字数 210 浏览 3 评论 0原文

我有一个 CDC 类型的对象 dc,我想获取一个 HDC 对象。

我在此处阅读了 MSDN 文档,但没有不太明白。

有人可以为我提供一个关于如何执行此操作的简短示例/解释吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寻梦旅人 2024-08-06 09:53:32

当您有 CDC 对象时,必要时它将隐式转换为 HDC:

CDC dc;
HDC hdc = dc; // HDC hdc = dc.operator HDC();

如果您有指向 CDC 对象的指针,则使用函数 GetSafeHdc 看起来会更清晰:

CDC* pdc = SOME;
HDC hdc = pdc->GetSafeHdc();

When you have CDC object it will be implicitly converted to HDC when necessary:

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* pdc = SOME;
HDC hdc = pdc->GetSafeHdc();
堇年纸鸢 2024-08-06 09:53:32

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.

自我难过 2024-08-06 09:53:32

CDC 是一个 C++ 类,在合理的近似范围内,它封装了一个 HDC,它是设备上下文的句柄。

您链接到的文档描述了转换运算符,它是类可以提供的 C++ 构造,以允许从类实例到其他类型的隐式转换。 在这种情况下,隐式转换会产生 CDC 实例封装的底层句柄 (HDC)。

您可以在任何需要转换为 HDC 的地方使用 CDC 实例来执行转换。

最简单:

void f( const CDC& cdc )
{
    HDC hdc = cdc;

    // use hdc here
}

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 the CDC instance encapsulates.

You can perform the conversion by using a CDC instance anywhere were it needs to be converted to an HDC.

Most simply:

void f( const CDC& cdc )
{
    HDC hdc = cdc;

    // use hdc here
}
烂人 2024-08-06 09:53:32
HDC hDC = dc;
HDC hDC = dc;
一念一轮回 2024-08-06 09:53:32

只需分配它即可。

CDC cdc = something.
HDC hdc = cdc;
if (hdc != 0)
{
  //success...
}

Just assign it.

CDC cdc = something.
HDC hdc = cdc;
if (hdc != 0)
{
  //success...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文