Click here to Skip to main content
15,892,643 members
Articles / Desktop Programming / MFC
Article

Changing the Default form of Dialog Window

Rate me:
Please Sign up or sign in to vote.
2.00/5 (4 votes)
24 Dec 20042 min read 45.1K   14   2
Changing the default form of dialog window making it as a circle instead of rectangular form.

Image 1

Figure(1.1)

Image 2

Figure(1.2)

Introduction

If you compile your program the first time, you will see that the created window's shape (form) is rectangular(figure1.1). So if you want to change its default form to circular (See figure1.2) form or any other form do the following steps…

To do this, we will use SetWindowRgn() to set the window region.

Using the code

  1. Create or declare a variable of type (CRgn) & (CRect).
  2. Write this code in OnInitDialog() message handler function.
CMyDialog::OnInitDialog()
{
    CRect m_rect;
    CRgn m_rgn;
    GetClientRect(&m_rect);
    m_rect.NormalizeRect();
    m_rgn.CreateEllipticRgn(10,10,m_rect.Width(),m_rect.Height()+10);
    SetWindowRgn(m_rgn,TRUE);
}
/////

First, GetClientRect() copies the client coordinates of the CWnd client area into the only passed argument LPRECT. It copies the client area of the window into the only passed argument that is of type LPRECT. Then, we use CreateEllipticRgn() to create or specify an elliptical region in a bounding rectangle see figure(2). So you pass the four edges of the rectangle to this function.

Image 3

Figure (2)

In this figure, we create the blue circular form and omit the red components of the general rectangular form.

  • Assume the whole window is the red rectangle shown above and the customized form (circular form) you want your dialog to be taken is the blue circle.
  • To specify the form or change the form of your dialog, call the CWnd member function SetWindowRgn().

SetWindowRgn() sets a window's region and it takes two arguments, first is a handle to the specified region using CreateEllipticRgn() function and the second argument specifies whether to redraw the window after setting the region. If you use “TRUE” as the second argument then OS redraws the window after specifying the region.

Snippet Code (for self study)

Change the contents of OnInitDialog() message handler function to the following and see the changes made to your dialog when compliling and running your application… it will be like the following figures:

CMyDialog::OnInitDalog()

CMyDialog::OnInitDialog()
{
    CRgn m_rgn1,m_rgn2,result;
    CRect rect
    //getting the dimensions of our dialog & 
    //saving the dimensions in rect member…
    GetClientRect( &rect);
    m_rgn1.CreateEllipticRgn(10,10,rect.Width()-150, rect.Height());
    m_rgn2.CreateEllipticRgn(rect.Width()/2 – 50 , 
           10 , rect.Width() , rect.Height());
    VERIFY(result.CreateRectRgn( 0, 0 , 200 , 200 ));
    Int nCombineResult= result.CombineRgn( &m_rgn1 , m_rgn2 , RGN_OR);
    ASSERT(nCombineResult !=ERROR || nCombineResult != NULLREGION);
    SetWindowRgn( result, TRUE);
}

Resulting dialog will be in the following form:

Image 4

For the program linked with this article, it will be posted after at least one week …

Sorry because I am very busy in my exams…

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
Technical Lead Ministry of Higher Education
Egypt Egypt
My name is mohamed, & I'm from Egypt,in the faculty of computers.I began with visual c++ for two years ago.when I began with it I found it is very hard toundertand but when continue with it I find
it's very easy .
So,hopes to write to me...

Comments and Discussions

 
GeneralError in SetWindowRgn usage Pin
Michael Dunn24-Dec-04 5:19
sitebuilderMichael Dunn24-Dec-04 5:19 
GeneralRe: Error in SetWindowRgn usage Pin
Star_Friend2-Aug-05 5:42
Star_Friend2-Aug-05 5:42 

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.