Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a modeless dialog that I have changed the shape into a roundrect using SetWindowRgn(). I would like to draw a colored border around it using FrameRgn. Here is the code I am using:
C++
BOOL CMyDlg::OnInitDialog() 
{
CDialog::OnInitDialog();

m_Brush.CreateSolidBrush(RGB(255,255,255));

CRect rcDialog;
GetClientRect(rcDialog);

// This Creates area assigned to Dialog: This goes directly below the above in OnInitDialog
m_rgnShape.CreateRoundRectRgn(rcDialog.TopLeft().x, rcDialog.TopLeft().y, rcDialog.BottomRight().x,
rcDialog.BottomRight().y, rcDialog.Width()/8, rcDialog.Height()/8);

::SetWindowRgn(GetSafeHwnd(), (HRGN)m_rgnShape, TRUE);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

void CMyDlg::OnPaint() 
{
CPaintDC dc(this); // device context for painting

CBrush brush;
brush.CreateSolidBrush(RGB(255,0,0));

dc.FrameRgn(&m_rgnShape, &brush, 2, 2);
}





Can anyone explain why the FrameRgn is not working, and maybe provide some sample code that will make it work.

Thanks in advance,
Posted
Updated 30-Apr-13 4:02am
v2

1 solution

I think the bug is apparent: you use some region m_rgnShape in two places: when you set the window shape (by the way, my credit for using a right approach: many try to use transparency, which gives limited functionality and more problematic), and when you frame it.

But let's look at the description of CDC::FrameRgn (which should be a wrapper of Windows API FrameRgn): it says, it draws a border around the region. Please see:
http://msdn.microsoft.com/en-us/library/vstudio/z45126ka.aspx[^],
http://msdn.microsoft.com/en-us/library/a48eab8d%28v=vs.80%29.aspx[^].

So, I think your frame is actually painted, only it is located in the invisible (or call it non-existing) area of your window, outside of its region.

Apparently, you would need another, smaller region for your frame. It should be nested in your window region and be at some uniform distance from it. This is easy to do for a round shape or combination of round and rectangular shapes, but it's quite hard to do precisely in case of other shapes, so be prepared for an ugly look and thinking on how to improve it. Either think of the mathematics of nearly-equidistant shapes (which is difficult, because regions are always combined from some shapes give by the API), or compensate the non-equidistant shapes by selection of different values for nWidth, nHeight parameters of FrameRgn, or give up the framing. I guess it's good to try, experiment a bit, to see how it looks.

There is one much more difficult problem about non-rectangular shapes: you will clearly see some "jagginess", because the contours of the shape are not anti-aliased (please see http://en.wikipedia.org/wiki/Aliasing[^]). My idea was to combine the same technique of non-rectangular shapes with transparency of the window, by using a narrow transparent area at the edges of the window. I never tried it though; the approach looks quite sophisticated…

—SA
 
Share this answer
 
v2
Comments
Leo Chapiro 30-Apr-13 10:47am    
Sounds logical +5 ;)
Sergey Alexandrovich Kryukov 30-Apr-13 10:56am    
Thank you.
—SA
greatsatya 30-Apr-13 11:15am    
Hi Alex,

Thanks for quick response. Could you please share some code snippet if you have or any reference. BTW, When I draw any text in that window, the jagginess I found.
Can I get rid from this?
Sergey Alexandrovich Kryukov 30-Apr-13 11:26am    
This is not my name and not related to my name.

No, I don't have any code at this time. Now, your question about jagged edges. You need to use GDI+ antialiasing. Please do the search using this keyword, you will find how to do it; I don't remember the detail at the moment as I always used it through other libraries.

For texts, please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd183499%28v=vs.85%29.aspx
(pay attention for ANTIALIASED_QUALITY and CLEARTYPE_QUALITY)

This is one well-known brute-force approach:
http://www.codeproject.com/Articles/21520/Antialiasing-Using-Windows-GDI

—SA
Sergey Alexandrovich Kryukov 30-Apr-13 11:26am    
Nevertheless, I answered you main question.

Will you accept it formally (green button)? — thanks.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900