Click here to Skip to main content
15,881,380 members
Articles / Desktop Programming / MFC
Article

Simple way to create non-rectangular shaped dialogs

Rate me:
Please Sign up or sign in to vote.
4.81/5 (41 votes)
6 Dec 20031 min read 154.6K   3.3K   93   23
This article outlines a simple way to create dialogs which are not rectangular in shape

Image 1

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


Written By
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
Bhavin Jagad22-Jul-10 23:24
Bhavin Jagad22-Jul-10 23:24 
shaped window and draggable
GeneralCreate a Non Rectangle shaped Dialog Pin
Dabara29-Jul-08 4:51
Dabara29-Jul-08 4:51 
GeneralSimple but helpful. Pin
Zhou'Liang10-Apr-08 16:23
Zhou'Liang10-Apr-08 16:23 
GeneralExcellent sample code Pin
TigerTom21-Feb-08 0:44
TigerTom21-Feb-08 0:44 
QuestionGood work, how about border? Pin
Super Hornet5-Dec-06 16:55
Super Hornet5-Dec-06 16:55 
QuestionHow to create non-rectangular shaped Buttons Pin
PravinSingh19-May-06 2:11
PravinSingh19-May-06 2:11 
AnswerRe: How to create non-rectangular shaped Buttons Pin
abhinaba19-May-06 10:58
abhinaba19-May-06 10:58 
QuestionHow to create complex shape windows? Pin
Tanmay Sathe10-Mar-04 7:53
Tanmay Sathe10-Mar-04 7:53 
AnswerRe: How to create complex shape windows? Pin
abhinaba12-Mar-04 2:27
abhinaba12-Mar-04 2:27 
GeneralSomething so cool... Pin
Andrew Lawrence2-Mar-04 9:16
Andrew Lawrence2-Mar-04 9:16 
Generalvery good Pin
ali khalilvand16-Dec-03 23:15
ali khalilvand16-Dec-03 23:15 
GeneralVery Useful Pin
bkimble15-Dec-03 13:34
bkimble15-Dec-03 13:34 
GeneralThanks for the support Pin
abhinaba11-Dec-03 18:41
abhinaba11-Dec-03 18:41 
GeneralWhat's new here? Useless article Pin
Pete Goodsall11-Dec-03 2:56
Pete Goodsall11-Dec-03 2:56 
GeneralRe: What's new here? Useless article Pin
kriaz27-Jul-04 23:29
kriaz27-Jul-04 23:29 
GeneralRe: What's new here? Useless article Pin
Pete Goodsall28-Jul-04 0:59
Pete Goodsall28-Jul-04 0:59 
GeneralRe: What's new here? Useless article Pin
gmohanraj11-Feb-10 2:01
gmohanraj11-Feb-10 2:01 
General[Message Deleted] Pin
Anonymous9-Dec-03 6:39
Anonymous9-Dec-03 6:39 
GeneralRe: What's new here? Useless article. Pin
tidhar9-Dec-03 21:02
tidhar9-Dec-03 21:02 
GeneralRe: What's new here? Useless article. Pin
abhinaba9-Dec-03 21:06
abhinaba9-Dec-03 21:06 
GeneralRe: What's new here? Useless article. Pin
Trollslayer5-Nov-06 6:55
mentorTrollslayer5-Nov-06 6:55 
GeneralRe: What's new here? Useless article. Pin
  Forogar  15-Dec-03 2:20
professional  Forogar  15-Dec-03 2:20 
GeneralRe: What's new here? Useless article. Pin
binyo6630-Jun-09 1:07
binyo6630-Jun-09 1:07 

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

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