【发布时间】:2013-03-07 04:09:14
【问题描述】:
我想知道 CClientDC 和 CWnd::GetDC 之间是否有任何区别。尽管存在资源分配和语义方面的事实,但两者似乎相当平等。
一个例子:
CClientDC dc(this); // "this" is sub-classed object of CWnd
CGdiObject* oldObj = dc.SelectStockObject(WHITE_BRUSH);
... do some with dc ...
dc.SelectObject(oldObj);
// Device object is stack object, ReleaseDC called automaticly
或
CDC* dc = this->GetDC(); // "this" is sub-classed object of CWnd
CGdiObject* oldObj = dc->SelectStockObject(WHITE_BRUSH);
... do some with dc ...
dc->SelectObject(oldObj);
// Device context belongs to a window class no need to call ReleaseDC
// I don't allocate dc, so I don't delete it
两个代码 sn-ps 看起来相同,只是语义不同,但如果有区别的话,区别在哪里?对于它们的使用,我有什么需要注意的。
GetDC() 是否只是 CClientDC(this) 的捷径? 我有点困惑。
编辑:在某些情况下,CClientDC() 返回的设备上下文与 GetDC() 返回的设备上下文不同,我想知道原因。
例子:
HRC hRC = wglCreateContext(GetDC()->m_hDC); // work's as expected.
但是
ClientDC dc(this)
HRC hRC = wglCreateContext(dc.m_hDC); // does not work as expected, output in
// clients device context not screen visible.
因此,同一窗口上的两个客户端设备上下文之间肯定存在细微差别,但 MSDN 没有提供这方面的信息。 CClientDC和GetDC见MSDN上的功能描述。
【问题讨论】:
-
花一点时间阅读
CWnd::GetDC()和CClientDC上的文档可能会回答您的问题。它们都是客户端设备上下文包装器。