保存 DC 的剪辑区域并在以后恢复它的正确方法是什么?

What's the correct way to save the clipping region of a DC and restore it later?

还记得this answer吗?事实证明,如果我单击应用了此代码的选项卡控件的按钮,整个 window 空白,直到我将鼠标悬停在上面。我确定这是因为未设置传递给 WM_CTLCOLORxxx 消息的设备上下文的剪辑区域:

(results from windows 7 64-bit)
on a normal redraw
window rect 435 301 591 324
client rect 4 96 160 119
clip rect 4 96 230 119

after clicking a button
window rect 435 301 591 324
client rect 4 96 160 119
clip rect 0 0 320 240 (this is the top level window's client rect)

所以现在我想暂时将 DC 剪辑到按钮的客户端矩形。我不想只调用 IntersectClipRect() 以防 Windows 的内部工作(或其他完全)通过不同的 cilp 矩形,所以我宁愿保存剪裁矩形并恢复它。我的问题是最好的方法是什么。

SaveDC()吗?

i = SaveDC(dc);
IntersectClipRect(dc, r.left, r.top, r.right, r.bottom);
// ...
RestoreDC(dc, i);

MSDN 的文档没有将裁剪列为使用 SaveDC() 保存的内容之一。

GetClipRgn()SelectClipRgn()吗?

rgn = CreateRectRegion(0, 0, 0, 0); // see also 
GetClipRgn(dc, rgn);
IntersectClipRect(dc, r.left, r.top, r.right, r.bottom);
// ...
SelectClipRgn(dc, rgn);

我不确定这是否真的会完全替换现有的裁剪区域,或者只是做另一个相交。

还是其他原因?

谢谢。

SaveDC()/RestoreDC()包括裁剪区域。