5,667,575 members and growing! (14,244 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, C++, Windows, Visual Studio, MFC, Dev

Posted: 6 Dec 2003
Updated: 6 Dec 2003
Views: 74,523
Bookmarked: 53 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
34 votes for this Article.
Popularity: 6.61 Rating: 4.31 out of 5
3 votes, 9.1%
1
2 votes, 6.1%
2
0 votes, 0.0%
3
7 votes, 21.2%
4
21 votes, 63.6%
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


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
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
GeneralCreate a Non Rectangle shaped DialogmemberDabara5:51 29 Jul '08  
GeneralSimple but helpful.memberZhou'Liang17:23 10 Apr '08  
GeneralExcellent sample codememberTigerTom1:44 21 Feb '08  
QuestionGood work, how about border?memberRaj Gopal17:55 5 Dec '06  
QuestionHow to create non-rectangular shaped ButtonsmemberPravinSingh3:11 19 May '06  
AnswerRe: How to create non-rectangular shaped Buttonsmemberabhinaba11:58 19 May '06  
GeneralHow to create complex shape windows?memberTom4278:53 10 Mar '04  
GeneralRe: How to create complex shape windows?memberabhinaba3:27 12 Mar '04  
GeneralSomething so cool...memberAndrew Lawrence10:16 2 Mar '04  
Generalvery goodmemberali_kh0:15 17 Dec '03  
GeneralVery Usefulmemberbkimble14:34 15 Dec '03  
GeneralThanks for the supportmemberabhinaba19:41 11 Dec '03  
GeneralWhat's new here? Useless articlememberClevedon_Peanut3:56 11 Dec '03  
GeneralRe: What's new here? Useless articlesusskriaz0:29 28 Jul '04  
GeneralRe: What's new here? Useless articlememberClevedon_Peanut1:59 28 Jul '04  
GeneralWhat's new here? Useless article.sussAnonymous7:39 9 Dec '03  
GeneralRe: What's new here? Useless article.memberTidhar22:02 9 Dec '03  
GeneralRe: What's new here? Useless article.memberabhinaba22:06 9 Dec '03  
GeneralRe: What's new here? Useless article.memberTrollslayer7:55 5 Nov '06  
GeneralRe: What's new here? Useless article.memberForogar3:20 15 Dec '03  

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-2008
Web18 | Advertise on the Code Project