Click here to Skip to main content
6,611,284 members and growing! (20,421 online)
Email Password   helpLost your password?
Desktop Development » Dialogs and Windows » Dialogs     Intermediate

Simple way to create non-rectangular shaped dialogs

By abhinaba

This article outlines a simple way to create dialogs which are not rectangular in shape
VC6, Windows, Visual Studio, MFC, Dev
Posted:6 Dec 2003
Views:85,625
Bookmarked:67 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
34 votes for this article.
Popularity: 6.74 Rating: 4.40 out of 5
3 votes, 9.1%
1
1 vote, 3.0%
2

3
7 votes, 21.2%
4
22 votes, 66.7%
5

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

About the Author

abhinaba


Member
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.
Occupation: Web Developer
Location: United States United States

Other popular Dialogs and Windows articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
GeneralCreate a Non Rectangle shaped Dialog PinmemberDabara5:51 29 Jul '08  
GeneralSimple but helpful. PinmemberZhou'Liang17:23 10 Apr '08  
GeneralExcellent sample code PinmemberTigerTom1:44 21 Feb '08  
QuestionGood work, how about border? PinmemberRaj Gopal17:55 5 Dec '06  
QuestionHow to create non-rectangular shaped Buttons PinmemberPravinSingh3:11 19 May '06  
AnswerRe: How to create non-rectangular shaped Buttons Pinmemberabhinaba11:58 19 May '06  
GeneralHow to create complex shape windows? PinmemberTom4278:53 10 Mar '04  
GeneralRe: How to create complex shape windows? Pinmemberabhinaba3:27 12 Mar '04  
GeneralSomething so cool... PinmemberAndrew Lawrence10:16 2 Mar '04  
Generalvery good Pinmemberali_kh0:15 17 Dec '03  
GeneralVery Useful Pinmemberbkimble14:34 15 Dec '03  
GeneralThanks for the support Pinmemberabhinaba19:41 11 Dec '03  
GeneralWhat's new here? Useless article PinmemberClevedon_Peanut3:56 11 Dec '03  
GeneralRe: What's new here? Useless article Pinsusskriaz0:29 28 Jul '04  
GeneralRe: What's new here? Useless article PinmemberClevedon_Peanut1:59 28 Jul '04  
GeneralWhat's new here? Useless article. PinsussAnonymous7:39 9 Dec '03  
GeneralRe: What's new here? Useless article. PinmemberTidhar22:02 9 Dec '03  
GeneralRe: What's new here? Useless article. Pinmemberabhinaba22:06 9 Dec '03  
GeneralRe: What's new here? Useless article. PinmemberTrollslayer7:55 5 Nov '06  
GeneralRe: What's new here? Useless article. PinmemberForogar3:20 15 Dec '03  
GeneralRe: What's new here? Useless article. Pinmemberbinyo662:07 30 Jun '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Dec 2003
Editor: Nishant Sivakumar
Copyright 2003 by abhinaba
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project