Click here to Skip to main content
11,496,146 members (517 online)
Click here to Skip to main content

Simple way to create non-rectangular shaped dialogs

, 6 Dec 2003 124K 2.7K 88
This article outlines a simple way to create dialogs which are not rectangular in shape
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

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 Pin
onlybj23-Jul-10 0:24
memberonlybj23-Jul-10 0:24 
GeneralCreate a Non Rectangle shaped Dialog Pin
Dabara29-Jul-08 5:51
memberDabara29-Jul-08 5:51 
GeneralSimple but helpful. Pin
Zhou'Liang10-Apr-08 17:23
memberZhou'Liang10-Apr-08 17:23 
GeneralExcellent sample code Pin
TigerTom21-Feb-08 1:44
memberTigerTom21-Feb-08 1:44 
QuestionGood work, how about border? Pin
Raj Gopal5-Dec-06 17:55
memberRaj Gopal5-Dec-06 17:55 
QuestionHow to create non-rectangular shaped Buttons Pin
PravinSingh19-May-06 3:11
memberPravinSingh19-May-06 3:11 
AnswerRe: How to create non-rectangular shaped Buttons Pin
abhinaba19-May-06 11:58
memberabhinaba19-May-06 11:58 
QuestionHow to create complex shape windows? Pin
Tom42710-Mar-04 8:53
memberTom42710-Mar-04 8:53 
AnswerRe: How to create complex shape windows? Pin
abhinaba12-Mar-04 3:27
memberabhinaba12-Mar-04 3:27 
GeneralSomething so cool... Pin
Andrew Lawrence2-Mar-04 10:16
memberAndrew Lawrence2-Mar-04 10:16 
Generalvery good Pin
ali_kh17-Dec-03 0:15
memberali_kh17-Dec-03 0:15 
GeneralVery Useful Pin
bkimble15-Dec-03 14:34
memberbkimble15-Dec-03 14:34 
GeneralThanks for the support Pin
abhinaba11-Dec-03 19:41
memberabhinaba11-Dec-03 19:41 
GeneralWhat's new here? Useless article Pin
Clevedon_Peanut11-Dec-03 3:56
memberClevedon_Peanut11-Dec-03 3:56 
GeneralRe: What's new here? Useless article Pin
kriaz28-Jul-04 0:29
susskriaz28-Jul-04 0:29 
GeneralRe: What's new here? Useless article Pin
Clevedon_Peanut28-Jul-04 1:59
memberClevedon_Peanut28-Jul-04 1:59 
GeneralRe: What's new here? Useless article Pin
gmohanraj11-Feb-10 3:01
membergmohanraj11-Feb-10 3:01 
General[Message Deleted] Pin
Anonymous9-Dec-03 7:39
sussAnonymous9-Dec-03 7:39 
GeneralRe: What's new here? Useless article. Pin
Tidhar9-Dec-03 22:02
memberTidhar9-Dec-03 22:02 
GeneralRe: What's new here? Useless article. Pin
abhinaba9-Dec-03 22:06
memberabhinaba9-Dec-03 22:06 
GeneralRe: What's new here? Useless article. Pin
Trollslayer5-Nov-06 7:55
memberTrollslayer5-Nov-06 7:55 
GeneralRe: What's new here? Useless article. Pin
Forogar15-Dec-03 3:20
memberForogar15-Dec-03 3:20 
GeneralRe: What's new here? Useless article. Pin
binyo6630-Jun-09 2:07
memberbinyo6630-Jun-09 2: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 | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 7 Dec 2003
Article Copyright 2003 by abhinaba
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid