作者: 乔克斯
查看: 4774|回复: 9

【异形控件】Control.Region 属性实现异形

[复制链接]
乔克斯 发表于 2014-7-31 11:43:54 | 显示全部楼层 |阅读模式
查看: 4774|回复: 9
Control.Region 属性实现异形
Region属性:获取或设置与控件关联的窗口区域。
命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
属性值与控件关联的窗口或控件的 Region

备注

窗口区域是操作系统允许在其中进行绘制的窗口内的像素的集合。操作系统不显示位于窗口区域之外的窗口的任何部分。控件的区域的坐标相对于控件的左上角,而不是相对于控件的工作区。

注意

该区域包含的像素的集合可以是不连续的。


示例

下面的代码示例阐释了如何通过创建圆形按钮来使用 Region 属性。若要运行该示例,请将下面的代码粘贴到一个包含 roundButton 按钮的窗体中。该示例要求 Paint 事件已连接到该示例中定义的事件处理程序。


C#代码:
[C#] 纯文本查看 复制代码
// This method will change the square button to a circular button by 
// creating a new circle-shaped GraphicsPath object and setting it 
// to the RoundButton objects region.
private void roundButton_Paint(object sender, 
    System.Windows.Forms.PaintEventArgs e)
{

    System.Drawing.Drawing2D.GraphicsPath buttonPath = 
        new System.Drawing.Drawing2D.GraphicsPath();

    // Set a new rectangle to the same size as the button's 
    // ClientRectangle property.
    System.Drawing.Rectangle newRectangle = roundButton.ClientRectangle;

    // Decrease the size of the rectangle.
    newRectangle.Inflate(-10, -10);
    
    // Draw the button's border.
    e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle);

    // Increase the size of the rectangle to include the border.
    newRectangle.Inflate( 1,  1);

    // Create a circle within the new rectangle.
    buttonPath.AddEllipse(newRectangle);
            
    // Set the button's Region property to the newly created 
    // circle region.
    roundButton.Region = new System.Drawing.Region(buttonPath);

}

评分

参与人数 2金钱 +2 收起 理由
夏怀罂c + 1 感谢LZ对论坛做出的贡献~
xiaobo + 1 感谢分享,LZ辛苦了~

查看全部评分

回复 论坛版权

使用道具 举报

 楼主| 乔克斯 发表于 2014-8-1 10:49:09 | 显示全部楼层

这是微软开发文档的介绍= =。
xiaobo 发表于 2014-8-1 09:52:22 | 显示全部楼层
有图就更好咯~
xiaobo 发表于 2014-8-1 10:52:07 | 显示全部楼层
乔克斯 发表于 2014-8-1 10:49
这是微软开发文档的介绍= =。

是啊,但是你搞这个不如直接写个示例,学起来多方便
bug 发表于 2014-8-2 11:02:43 | 显示全部楼层
管你发的什么都必须回复一遍
 楼主| 乔克斯 发表于 2014-8-2 11:05:43 | 显示全部楼层
bug 发表于 2014-8-2 11:02
管你发的什么都必须回复一遍

aaaaasuper 发表于 2018-9-2 00:17:55 | 显示全部楼层
有半透明窗体没,并且不抖动
wp231356720 发表于 2018-12-29 15:03:36 | 显示全部楼层
看不懂   太难了点吧  号复杂啊  大哥大姐们
orcfootman 发表于 2019-3-1 18:06:13 | 显示全部楼层
看不到效果图呢?这个是只适合于按钮控件么?
zycmartin 发表于 2019-4-2 11:15:49 | 显示全部楼层
现在还有人用么?
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-3-29 19:11, Processed in 0.632018 second(s), 31 queries , Gzip On.

快速回复 返回顶部 返回列表