Click here to Skip to main content
Click here to Skip to main content

Simple way to create non-rectangular shaped dialogs

, 6 Dec 2003
Rate this:
Please Sign up or sign in to vote.
This article outlines a simple way to create dialogs which are not rectangular in shape

Introduction

Normally Dialog Boxes are rectangular in shape. Various methods can be adopted to make them non-rectangular in shape. However, most of these methods are complicated and suited for application that uses skinning to create dialogs with the shape of a skin or image. If the required shape of the dialog is simple like a rectangle with rounded corners or an ellipse, then a much simpler method can be used. In this method multiple CRgn objects are created and then combined (union of regions) to create a compound region. The dialog is then given the shape of the compound region.

The Code

All the required code is in the OnInitDialog method of the dialog

Step 1: Set Dialog style

To change the shape of the dialog, in the OnInitDialog of the dialog the Caption and the Border of the dialog is removed.

    ...
   //  Remove caption and border
   SetWindowLong(m_hWnd, GWL_STYLE, GetWindowLong(m_hWnd, GWL_STYLE) 
        & (~(WS_CAPTION | WS_BORDER)));
   ...

Step 2: Create individual regions

Individual elliptic regions are then created using the coordinates of the dialog's window

    //  Get the rectangle
    CRect rect;
    GetWindowRect(&rect);
    int w = rect.Width();
    int h = rect.Height();
    
    CRgn rgn1;
    CRgn rgn2;

    //  Create the top ellipse 
    rgn1.CreateEllipticRgn(1, 1, w, h/2 + 30);

    //  Create the bottom ellipse 
    rgn2.CreateEllipticRgn(1, h/2 - 30, w, h);

Step 3: Combine the regions into one

The regions are combined to create a single region. The combination is actually a UNION of all the individual regions

    //  Combine the two ellipses 
    CombineRgn(rgn1, rgn1, rgn2, RGN_OR);

Step 4: Change the shape of the dialog to the region

The dialogs shape is changed using the following code

    //  Set the window region
    SetWindowRgn(static_cast<HRGN>(rgn1.GetSafeHandle()), TRUE);

Step 5: Cleaning up

The CRgn object needs to be detached from the region, or else the CRgn destructor closes the HRGN handle when rgn objects go out of scope

    rgn1.Detach();
    rgn2.Detach();

History

  • Initial version

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

Share

About the Author

abhinaba
Web Developer
United States United States
I just love coding. I started programming in 1995 with BASIC and then moved through Cobol, Pascal, Prolog, C, C++, VB, VC++ and now C#/.NET.
 
I received a Bachelor of Technology degree in Computer Science from University of Calcutta in 2001.
 
I worked for some time in Texas Instruments, Adobe Systems and now in Microsoft India Development Center in the Visual Studio Team Systems.
 
I am from the City of Joy, Kolkata in India, but now live and code Hyderabad.

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberonlybj22-Jul-10 23:24 
GeneralCreate a Non Rectangle shaped Dialog PinmemberDabara29-Jul-08 4:51 
GeneralSimple but helpful. PinmemberZhou'Liang10-Apr-08 16:23 
GeneralExcellent sample code PinmemberTigerTom21-Feb-08 0:44 
QuestionGood work, how about border? PinmemberRaj Gopal5-Dec-06 16:55 
QuestionHow to create non-rectangular shaped Buttons PinmemberPravinSingh19-May-06 2:11 
AnswerRe: How to create non-rectangular shaped Buttons Pinmemberabhinaba19-May-06 10:58 
QuestionHow to create complex shape windows? PinmemberTom42710-Mar-04 7:53 
AnswerRe: How to create complex shape windows? Pinmemberabhinaba12-Mar-04 2:27 
GeneralSomething so cool... PinmemberAndrew Lawrence2-Mar-04 9:16 
Generalvery good Pinmemberali_kh16-Dec-03 23:15 
GeneralVery Useful Pinmemberbkimble15-Dec-03 13:34 
GeneralThanks for the support Pinmemberabhinaba11-Dec-03 18:41 
GeneralWhat's new here? Useless article PinmemberClevedon_Peanut11-Dec-03 2:56 
GeneralRe: What's new here? Useless article Pinsusskriaz27-Jul-04 23:29 
GeneralRe: What's new here? Useless article PinmemberClevedon_Peanut28-Jul-04 0:59 
GeneralRe: What's new here? Useless article Pinmembergmohanraj11-Feb-10 2:01 
General[Message Deleted] PinsussAnonymous9-Dec-03 6:39 
GeneralRe: What's new here? Useless article. PinmemberTidhar9-Dec-03 21:02 
GeneralRe: What's new here? Useless article. Pinmemberabhinaba9-Dec-03 21:06 
GeneralRe: What's new here? Useless article. PinmemberTrollslayer5-Nov-06 6:55 
GeneralRe: What's new here? Useless article. PinmemberForogar15-Dec-03 2:20 
GeneralRe: What's new here? Useless article. Pinmemberbinyo6630-Jun-09 1:07 

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 | Mobile
Web04 | 2.8.141022.1 | Last Updated 7 Dec 2003
Article Copyright 2003 by abhinaba
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid