简体   繁体   English

CClientDC是否保证将DC恢复到以前的状态?

[英]Does CClientDC guarantee restoring the DC back to its previous state?

I am using a CClientDC object, which serves as a wrapper for functions GetDC and ReleaseDC : 我正在使用CClientDC对象,该对象充当函数GetDCReleaseDC的包装:

  • Function GetDC is called from inside the CClientDC constructor CClientDC构造函数内部调用GetDC函数
  • Function ReleaseDC is called from inside the CClientDC destructor CClientDC析构函数内部调用函数ReleaseDC

In between, I manipulate the DC (changing pens, brushes, etc). 在这两者之间,我操纵DC(更换笔,刷子等)。

But I'm pretty sure that the CClientDC object does not restore the DC back to its previous state. 但是我很确定CClientDC对象不会将DC恢复到以前的状态。

This means I have to make sure of it myself. 这意味着我必须自己确保。 Is that correct? 那是对的吗?

Thank you. 谢谢。

Open wingdi.cpp from the MFC source code and look for the implementation of CClientDC : 从MFC源代码中打开wingdi.cpp ,然后寻找CClientDC的实现:

CClientDC::~CClientDC()
{
    ASSERT(m_hDC != NULL);
    ::ReleaseDC(m_hWnd, Detach());
}

You see that it only calls ReleaseDC , which does not restore the DC to its previous state. 您会看到它仅调用ReleaseDC而不会将DC恢复到其先前状态。 There is no way for CClientDC to know which GDI objects you changed. CClientDC无法知道您更改了哪些GDI对象。

If you want to save and restore the DC's state, there are special methods for this: CDC::SaveDC and CDC::RestoreDC . 如果要保存和恢复DC的状态,则有一些特殊的方法: CDC::SaveDCCDC::RestoreDC These are not called automatically from CDC or CClientDC —you need manually call them yourself. 这些不会从CDCCClientDC自动调用-您需要自己手动调用它们。

Or, you can save and restore each individual GDI object that you modify. 或者,您可以保存和还原修改的每个单独的GDI对象。 When you call SelectObject , the original object is returned. 调用SelectObject ,将返回原始对象。 You save this, and restore it as you deselect the object you were using. 您保存它,并在取消选择所使用的对象时将其还原。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM