在窗口中创建孔






4.78/5 (10投票s)
2000年2月7日

91328

1355
如何创建带有孔的窗口
假设你需要在一个窗口内部创建一个窗口或者一个孔洞,只是为了向外看并说:“你好” ;-)。
实现起来并不困难——实际上,它相当简单。关键是 SetWindowRgn
——它将窗口边框设置为指定的 CRgn
。你还可以反转 CRgn
(RGN_XOR
) 并在窗口内部创建一个孔洞。我创建了两个函数来实现这个功能:
void CreateHole(CRgn& rgn)
- 创建一个形状为CRgn
的孔洞void ClearHoles()
- 清除孔洞并将窗口恢复到其通常的形状
void CHolesDlg::CreateHole(CRgn& rgn)
{
CRgn WindowRgn;
CRgn HoleRgn;
static CRgn ThisRgn;
CRect WindowRect;
static bool Start = true;
GetWindowRect (WindowRect); // Get the Window rect
WindowRgn.CreateRectRgn (0,0,WindowRect.Width (), WindowRect.Height());
// initialize the hole region
HoleRgn.CreateRectRgn (0,0,0,0);
if (Start)
{
// initialize ThisRgn
ThisRgn.CreateRectRgn (0,0,0,0);
// First copy the region himself in ThisRgn
ThisRgn.CopyRgn (&rgn);
}
else
{
// Other times, Add the new region to ThisRgn
ThisRgn.CombineRgn (&ThisRgn, &rgn, RGN_OR);
}
Start = false;
// create the border of the hole(s) with ThisRgn and set it in that
// border by reversing it
HoleRgn.CombineRgn (&ThisRgn, &WindowRgn, RGN_XOR);
SetWindowRgn ((HRGN__*)HoleRgn.m_hObject, TRUE);
}
void CMyControl::ClearHoles()
{
CRect WindowRect;
CRgn WindowRgn;
GetWindowRect (WindowRect);
WindowRgn.CreateRectRgn (0,0,WindowRect.Width (), WindowRect.Height());
// Get back to the classic border
SetWindowRgn ((HRGN__*)WindowRgn.m_hObject, TRUE);
}
void CMyControl::OnClear()
{
// TODO: Add your control notification handler code here
CString s;
GetDlgItem (IDC_CLEAR)->GetWindowText (s);
if (s == "Clear Holes")
{
GetDlgItem (IDC_CLEAR)->SetWindowText ("Show Holes");
ClearHoles();
}
if (s == "Show Holes")
{
GetDlgItem (IDC_CLEAR)->SetWindowText ("Clear Holes");
ShowHoles();
}
}
在我的示例中,我展示了在对话框应用程序 (VC++ 6.0, Windows 98) 中创建 4 个形状各异的孔洞。它也应该适用于其他控件。
许可证
本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。
作者可能使用的许可证列表可以在此处找到。