Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

MFC CRgn Example – An Elliptical Dialog

 

1. Introduction to MFC CRgn

One may come across plenty of Non-Rectangular window or user interaction dialog in their experience. Many of MP3 players in the market produce a non-rectangular fancy UI. In this example, we will create a round-shaped dialog with a username and password fields in it. The technique is very simple. Perform your drawing as usual and then use the CRgn class to create a region from your drawing. Then you can ask the window to have the boundaries defined by the MFC CRgn object.

2. Dialog Controls & Region

First, we create a dialog-based MFC application and name it as RoundWind. Once the dialog is ready, we resize it to 247×129. Then position the controls in the dialog as shown below:

Setting Up the Dialog For this MFC CRgn Example

Setting Up the Dialog For this MFC CRgn Example

We will display the controls in this dialog in such a way that they live in an elliptical area. Once the dialog-based application is ready with the controls, we can define the path for the region. We can create a region in multiple ways, and in this example, we will create a region from a path. Now we move to the coding part of this.

3. Displaying The Dialog in Elliptical Shape

First, we will switch to ‘Class View’. We will make modification on the MFC created dialog class so it will show itself as a round window. In our example, the dialog class is CRoundWndDlg. The below picture shows this:

CDialog Derived Class

CDialog Derived Class

3.1 Declare CRgn & CClientDC Members

In the Header file of the CRoundWindDlg we add two members. One is for the client device context CClientDC and another one is to specify the region MFC CRgn using the client device context. These declarations are below:

//Sample 01: Declare a device context and a Region instance
private:
CClientDC * m_clientDC;
CRgn m_rgn;

3.2 BeginPath & EndPath to Define MFC CRgn’s Path

In the OnInitDialog of the dialog class, we define the path using the CClientDC supplied drawing function. The drawing function which we call between the BeginPath and EndPath defines the path for the region.  To make the example simple, we use the ellipse function. Since it is enclosed by the BeginPath & EndPath, the ellipse is the path for our example, and we feed this to MFC CRgn later. But one can use the MoveTo, LineTo, and ArcTo etc., to define a complex path for the region. Remember, one should place all these drawing functions within the Begin and End path pair. The code below defines an elliptical path:

//Sample 02:Create the device context and define 
//the path for the region
m_clientDC = new CClientDC(this);
m_clientDC->BeginPath();
m_clientDC->Ellipse(0,25,400,250);
m_clientDC->EndPath();

3.3 CRgn’s Path and Elliptical MFC Dialog Window

Note that the drawing function Ellipse was enclosed in between BeginPath and EndPath. The drawing that is enclosed between begin & end pair will form the path for defining the Region. CreateFromPath function of the CRgn will generate the region out of the path defined by the Client Device Context. The below code defines a Region using MFC CRgn and region path:

//Sample 03:Create the region from the path
m_rgn.CreateFromPath(m_clientDC);
SetWindowRgn((HRGN) m_rgn,TRUE);

In our example, we created the region from the path which available through the CClientDC. The CreateFromPath takes the device context object and extracts the path specified in it. Once a region is ready, we can set that to any Window. In our example, we want to display the dialog as a round window and hence we are calling the SetWindowRgn function. As the function expects a Window Handle to a Region, we extracted it from the CRgn instance using the ‘(HRGN) m_rgn’ in-built casting operator. The second parameter TRUE specifies that we want to redraw the dialog instance.

When we execute the code, the output looks like the below one:

MFC Round Window Using CRgn

MFC Round Window Using CRgn

Source Code (From Google Drive): Download MFC Round Dialog Example

Categories: MFC

Tags: , , , ,

Do you like this Example? Share your thoughts!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.