这可以通过使用SetWindowRgn
API调用很简单地完成。它所做的是定义系统允许绘图出现的区域。
作为一个简单的例子,让我们在我们的窗口之一打孔。以下操作可以在窗口的WM_CREATE处理程序中完成:
case WM_CREATE:
{
// Get the window rect
RECT r;
GetWindowRect(m_hwnd, &r);
MapWindowPoints(NULL, m_hwnd, reinterpret_cast<LPPOINT>(&r), 2);
// Work out the size of the window
LONG w = r.right - r.left;
LONG h = r.bottom - r.top;
// Create a rectangular region to cover the window (almost)
HRGN hRgn = CreateRectRgnIndirect(&r);
// and a smaller elliptical window
r.left += w/4;
r.right -= w/4;
r.top += h/4;
r.bottom -= h/4;
HRGN rgnCirc = CreateEllipticRgnIndirect(&r);
// Now we combine the two regions, using XOR to create a hole
int cres = CombineRgn(hRgn, rgnCirc, hRgn, RGN_XOR);
// And set the region.
SetWindowRgn(m_hwnd, hRgn, TRUE);
}
break;
一些重要注意事项。传递给SetWindowRgn
的区域来自系统拥有的那个点,因此不要对其执行任何更多操作。另外,如果窗口调整大小,则需要修改区域 - 我只将WM_CREATE
中的示例作为示例。
关于上述的另一个小警告,它没有正确地计算窗口大小......正如我所说的,这只是一个在窗口中打洞的例子。
最后,我尝试了一个简单的Direct-X程序,它也适用于此。 Hoorah!
不知道它如何与DirectX一起工作,但您可以在线查看“SetWindowRgn”,一个WinAPI以...设置窗口区域。 – icabod
这看起来很酷,你可以举一个小例子吗? (如拿出一个像素或其他东西?) –