Let's say you have to create a window inside a window or a hole, just to look outside and say: "Hi" ;-).
It's not so difficult to accomplish - actually, it's pretty easy. The keyword is SetWindowRgn
- which sets the window border to the specified CRgn
. You can also invert the CRgn
(RGN_XOR
) and create a hole inside the window. I've created 2 functions for this:
void CreateHole(CRgn& rgn)
- creates the hole in the shape of CRgn
void ClearHoles()
- clear the holes and return the window to its usual shape
void CHolesDlg::CreateHole(CRgn& rgn)
{
CRgn WindowRgn;
CRgn HoleRgn;
static CRgn ThisRgn;
CRect WindowRect;
static bool Start = true;
GetWindowRect (WindowRect); WindowRgn.CreateRectRgn (0,0,WindowRect.Width (), WindowRect.Height());
HoleRgn.CreateRectRgn (0,0,0,0);
if (Start)
{
ThisRgn.CreateRectRgn (0,0,0,0);
ThisRgn.CopyRgn (&rgn);
}
else
{
ThisRgn.CombineRgn (&ThisRgn, &rgn, RGN_OR);
}
Start = false;
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());
SetWindowRgn ((HRGN__*)WindowRgn.m_hObject, TRUE);
}
void CMyControl::OnClear()
{
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();
}
}
In my example, I show 4 shaped holes in a dialog-based application (VC++ 6.0, Windows 98). It should work well for other controls as well.
License
This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.
A list of licenses authors might use can be found here.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.