jon*_*ham 7 c++ opengl graphics winapi device-context
一旦我们通过GetDC获得上下文设备使用C++编程.如果我们在不调用ReleaseDC的情况下退出程序,可能会发生什么不好的事情?
来自文档
ReleaseDC 函数释放设备上下文 (DC),将其释放以供其他应用程序使用。ReleaseDC功能的效果取决于DC的类型。它仅释放公共 DC 和窗口 DC。它对阶级或私人 DC 没有影响。
正如您所看到的,如果其他应用程序可以访问同一 DC,则可能需要它。
无论如何,对于此类事情使用 C++ RAII 惯用法是个好主意。考虑这个类:
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;
};
Run Code Online (Sandbox Code Playgroud)
通过这个类,你可以这样做:
{
ScopedDC dc(GetDC());
//do stuff with dc.get();
} //DC is automatically released here, even in case of exceptions
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2584 次 |
| 最近记录: |