在非矩形窗口中创建边框 [英] Creating border in non rectangular window

查看:102
本文介绍了在非矩形窗口中创建边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无模式对话框,我已使用SetWindowRgn()将形状更改为圆形。我想使用FrameRgn在它周围画一个彩色边框。这是我正在使用的代码:

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:

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);
}











任何人都可以解释为什么FrameRgn不工作,并且可能会提供一些示例代码来使其工作。



提前致谢,






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

Thanks in advance,

推荐答案

我认为这个错误很明显:你在两个地方使用了一些区域 m_rgnShape :当你设置窗口形状时(顺便说一下) ,我认为使用正确的方法:许多尝试使用透明度,这会提供有限的功能和更多的问题),当你构建它时。



但是让''看看 CDC :: FrameRgn 的描述(它应该是Windows API的包装 FrameRgn ):它说,它在区域周围画一个边界。请参阅:

http://msdn.microsoft.com/en -us / library / vstudio / z45126ka.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/a48eab8d%28v=vs.80%29.aspx [ ^ ]。



所以,我认为你的画框实际上是画的,只是它位于窗口的不可见(或称之为不存在)的区域之外。



显然,您需要另一个较小的区域。它应该嵌套在你的窗口区域,并与它保持一定的距离。对于圆形或圆形和矩形形状的组合来说,这很容易做到,但是在其他形状的情况下很难做到,所以要为丑陋的外观做准备并思考如何改进它。要么想到几乎等距的形状的数学(这很困难,因为区域总是由API提供的某些形状组合),或者通过选择 nWidth <的不同值来补偿非等距形状/ code>, nHeight 参数 FrameRgn ,或放弃框架。我想尝试,尝试一下,看看它的外观是好的。



关于非矩形形状还有一个更难的问题:你会清楚地看到一些jagginess,因为形状的轮廓不是抗锯齿(请参阅 http ://en.wikipedia.org/wiki/Aliasing [ ^ ])。我的想法是通过在窗口边缘使用一个狭窄的透明区域,将非矩形形状的相同技术与窗口的透明度结合起来。我从来没有尝试过;这种方法看起来相当复杂......



-SA
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


这篇关于在非矩形窗口中创建边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆