Click here to Skip to main content
11,502,547 members (38,893 online)
Click here to Skip to main content

Creating holes in a window

, 6 Feb 2000 CPOL 67.5K 1.1K 32
Rate this:
Please Sign up or sign in to vote.
How to create a window with holes in it
  • Download source files - 12 Kb

    Sample Image - holes.gif

    Lets say you have to create a window inside a window or a hole, just to look outsite and say: "Hi" Wink | ;-) .

    It's not so diffcult 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 it's usual shape.
    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();
        }
    }
    

    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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Share

    About the Author

    Amir Zalzberg

    Israel Israel
    No Biography provided

    Comments and Discussions

     
    GeneralNice! Pin
    Abu Mami1-May-10 8:53
    memberAbu Mami1-May-10 8:53 
    Questioncan i use SetWindowrgn to create hoels on the desktop Pin
    udayt23-Feb-05 23:35
    memberudayt23-Feb-05 23:35 
    GeneralChild Windows Pin
    John Ulvr9-Mar-03 5:58
    memberJohn Ulvr9-Mar-03 5:58 
    Do you know if this can be done for a child-dialog? Specifically, I don't want one of my Edit box pointers to overwrite a bitmap I have placed on one of the corners, but setting the region didn't seem to stop it. Frown | :( Any ideas whether this is possible? Am I missing something?


    John

    QuestionRegular Windows? Pin
    Wayne Fuller8-Feb-00 4:43
    sussWayne Fuller8-Feb-00 4:43 
    AnswerRe: Regular Windows? Pin
    Wayne Fuller8-Feb-00 4:50
    sussWayne Fuller8-Feb-00 4:50 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.150520.1 | Last Updated 7 Feb 2000
    Article Copyright 2000 by Amir Zalzberg
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid