Click to See Complete Forum and Search --> : MFC Dialog: How to create a non-rectangular dialog box?


Sonu Kapoor
February 13th, 2003, 02:48 PM
Q: How to create a non-rectangular dialog box?

A: The main idea is to use the 'SetWindowRgn()' function. Adding the following lines of code just before the 'return TRUE;' of the 'OnInitDialog()' will produce a dialog box that has rounded corners:


BOOL CYourDialog::OnInitDialog()
{
CDialog::OnInitDialog();

// Rest of initialization
// ...

// Create a 'CRgn' object
CRgn rgn;
CRect rect;
GetWindowRect(&rect);
rgn.CreateRoundRectRgn(0,
0,
rect.Width(),
rect.Height(),
30,
30);

// Set the windows region
SetWindowRgn(static_cast<HRGN>(rgn.GetSafeHandle()), TRUE);

// Detach the 'CRgn' object from the region or else the
// 'CRgn' destructor would close the 'HRGN' handle when 'rgn'
// goes out of scope
rgn.Detach();

return TRUE; // return TRUE unless you set the focus
// to a control
}

You can use a region of any shape that fits into your dialog rect.
<br><br>