如何在C#中裁剪(制作一个矩形孔)面板 [英] How to crop (make a rectangular hole in) a panel in C#

查看:132
本文介绍了如何在C#中裁剪(制作一个矩形孔)面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好早上每个人

谁知道怎么裁剪(做一个长方形的洞)一个控件?



i尝试裁剪一个正方形在面板中间

为例如果我的面板大小为

 

Panel myPanel = new Panel(){Size = new Size(300,800),Location = new Point(0,0); BackColor = Color.FromArgb(0,0,0); }



i希望裁剪尺寸为250,250且位于25,25的广场



我尝试过:



 GraphicsPath graphicPath =  new  GraphicPath(); 
Rectangle myRec = new 矩形( new point( 25 25 ), new 大小( 250 250 ));
面板myPanel = 面板(){尺寸= 尺寸( 300 800 ),Location = new Point( 0 0 ); BackColor = Color.FromArgb( 0 0 0 < /跨度>); }
myPanel.Region = new 区域(myRec);





i知道通过这样做,

它只是将原始面板设置为一个新区域。

但有没有任何方法可以在面板中间裁剪矩形..

谢谢

解决方案

除了Graeme提到的好文章(真的值得点)制作透明控件 - 无闪烁 [ ^ ],以下内容:



如果你想打个洞,可能会对这篇文章感兴趣: Phil Wright:Component Factory :.NET2,透明控件 [ ^ ]

同样使用 WS_EX_TRANSPARENT 可能感兴趣: c# - 在Visual Studio窗体窗体中使用面板上的不透明度的任何技巧? - Stack Overflow [ ^ ]


[感谢评论 - 感谢RickZeeland的建议]



这个洞是否可以显示它背后的物体?像一个透明的洞?这样的事情:进行透明控制 - 没有闪烁[^] [ ^ ] ??? blockquote>

使用System.Drawing; 
使用System.Windows.Forms;

名称空间PathStuff
{
公共静态类PathExtensions
{
public static void MakeHole(this Control cntrl,Rectangle xrect)
{
var region = new Region(cntrl.ClientRectangle);
region.Exclude(xrect);

cntrl.Region = region;
}

public static void MakeHoleCenteredProportional(此Control cntrl,int scaleX,int scaleY)
{
var rect = new Rectangle();
rect.Size = cntrl.ClientRectangle.Size;
Region region = new Region(rect);

rect.Inflate(cntrl.Width /(scaleX * -2),cntrl.Height /(scaleY * -2));

region.Exclude(rect);

cntrl.Region = region;
}
}
}

使用PathStuff在表单中使用示例:

; 
namespace YourForm
{
private void Form1_Load(object sender,EventArgs e)
{
PathExtensions.MakeHole(this,new Rectangle(100,100,100,100)) ;

//或
PathExtensions.MakeHoleCenteredProportional(此控制cntrl,(this,2,2);

}
};

注意'MakeHoleCenteredProportional方法在Control中创建一个孔;它的大小由提供的比率决定:#2的值将创建长度或宽度,为Control的相应维度的一半大小。 br />


建议:对表格使用双缓冲,或者使用UserControls。


hello morning everyone
anyone know how to crop (make a rectangular hole in) a control?

i try to crop a square in the middle of the panel
for an example
if the size of my panel was

Panel myPanel = new Panel() { Size = new Size(300, 800), Location = new Point(0,0); BackColor = Color.FromArgb(0,0,0); }

i would like to crop a square with a size of 250, 250 and located at point 25,25

What I have tried:

GraphicsPath graphicPath = new GraphicPath();
Rectangle myRec = new Rectangle(new point(25,25), new Size(250,250));
Panel myPanel = new Panel() { Size = new Size(300, 800), Location = new Point(0,0); BackColor = Color.FromArgb(0,0,0); }
myPanel.Region = new Region(myRec);



i know by doing this,
it just set the original panel to a new region.
but is there any method to crop the rectangle in the middle of panel..
thank you

解决方案

In addition to the nice article mentioned by Graeme (which deserves points really) Making Transparent Controls - No Flickering[^] , the following:

If you want to "punch a hole", this article might be of interest: Phil Wright : Component Factory: .NET2, Transparent controls[^]
Also using WS_EX_TRANSPARENT might be of interest: c# - Any trick to use opacity on a panel in Visual Studio Window Form? - Stack Overflow[^]


[moved from comments - thanks for the suggestion RickZeeland]

Is the hole suppose to show objects behind it? Like a transparent hole? Something like this: Making Transparent Controls - No Flickering[^][^]???


using System.Drawing;
using System.Windows.Forms;

namespace PathStuff
{
    public static class PathExtensions
    {
        public static void MakeHole(this Control cntrl, Rectangle xrect)
        {
            var region = new Region(cntrl.ClientRectangle);
            region.Exclude(xrect);

            cntrl.Region = region;
        }

        public static void MakeHoleCenteredProportional(this Control cntrl, int scaleX, int scaleY)
        {
            var rect = new Rectangle();
            rect.Size = cntrl.ClientRectangle.Size;
            Region region = new Region(rect);

            rect.Inflate(cntrl.Width / (scaleX * -2), cntrl.Height / (scaleY * -2));

            region.Exclude(rect);

            cntrl.Region = region;
        }
    }
}

Sample uses in a Form:

using PathStuff;
namespace YourForm
{
    private void Form1_Load(object sender, EventArgs e)
    {
         PathExtensions.MakeHole(this, new Rectangle(100,100, 100, 100));

        // or
        PathExtensions.MakeHoleCenteredProportional(this Control cntrl, (this, 2, 2);
    
    }
};

Note the 'MakeHoleCenteredProportional method makes a hole centered in the Control; its size is determined by the ratios supplied: a value of #2 will create length, or width, of half the size of the corresponding dimension of the Control.

Suggestion: use double-buffering with Forms, or UserControls with this.


这篇关于如何在C#中裁剪(制作一个矩形孔)面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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