什么坏事情可能发生,而不调用ReleaseDC? [英] What bad things may happen without calling to ReleaseDC?

查看:181
本文介绍了什么坏事情可能发生,而不调用ReleaseDC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++编程,一旦我们通过GetDC获取上下文设备使用。

Programming with C++, once we get the context device by GetDC to use. What bad things may happen if we exit the program without calling to ReleaseDC?

推荐答案

文档


ReleaseDC函数释放设备上下文(DC),释放
以供其他应用程序使用。 ReleaseDC函数
的效果取决于DC的类型。它只释放公共和窗口DC。它有
对类或私人DC没有影响。

The ReleaseDC function releases a device context (DC), freeing it for use by other applications. The effect of the ReleaseDC function depends on the type of DC. It frees only common and window DCs. It has no effect on class or private DCs.

如你所见, ,如果其他应用程序可以访问同一个DC。

As you can see, it may be needed if other applications can access the same DC.

在任何情况下,最好使用C ++ RAII成语这种东西。考虑这个类:

In any case, it's good idea to use C++ RAII idiom for this kind of things. Consider this class:

class ScopedDC
{
   public:
      ScopedDC(HDC handle):handle(handle){}
      ~ScopedDC() { ReleaseDC(handle); }
      HDC get() const {return handle; }
   //disable copying. Same can be achieved by deriving from boost::noncopyable
   private:
      ScopedDC(const ScopedDC&);
      ScopedDC& operator = (const ScopedDC&); 

   private:
      HDC handle;
};

有了这个类,你可以这样做:

With this class you can do this:

{
   ScopedDC dc(GetDC());
   //do stuff with dc.get();
}  //DC is automatically released here, even in case of exceptions

这篇关于什么坏事情可能发生,而不调用ReleaseDC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆