用户控件与自定义形状 [英] User Control with custom Shape

查看:108
本文介绍了用户控件与自定义形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个具有线条形状的用户控件



I need a user control that have a line shape

private void LineControl_Load(object sender, EventArgs e)
{
            GraphicsPath myPath = new GraphicsPath();

            myPath.AddLine(StartPoint, EndPoint);

            Region myRegion = new Region(myPath);

            this.Region = myRegion;

}





这不起作用



This doesn't work

推荐答案

基本上,您使用的是正确的方法。这是给它一个非矩形形状的唯一(有效)方法。



唯一的问题是:一行无法制作可以使用的有效2D形状作为有效的控制区域。你需要创建一个圆形的形状,不一定是简单的连接,但它应该是一个坚实的2D形状。这意味着,如果它可能是一个具有一个或多个整体的紧密轮廓,或其他东西。作为使用由线条构成的路径的替代方法,您可以从简单的基本形状开始,如矩形或椭圆形,然后使用AND / OR集合理论操作向其中添加或减去其他形状。请参阅:

http://msdn.microsoft.com /en-us/library/system.drawing.region.aspx [ ^ ]。



请参阅允许您从矩形或图形路径开始的构造函数(正如您所做的那样,只使用固定封闭路径),然后查看方法排除联盟异或;使用其他方法来改变地区,等等。







至于线形,没有这样的东西。该想法的最接近的表示将是椭圆形矩形,其具有一些有限的非零宽度,表示线的宽度。它只是数学抽象线的宽度为零,但是,屏幕表示需要一些宽度。



你可以从矩形开始,但是,一般情况下,您需要使用适当的转换将其倾斜到所需的角度:

http://msdn.microsoft.com/en-us/library/system.drawing.region.transform.aspx [ ^ ]。



例如:

Basically, you are using a correct approach. This is the only (valid) method of giving it a non-rectangular shape.

The only problem is: one line cannot make a valid 2D shape which could be used as a valid control region. You need to create a circular shape, not necessarily simply-conntected, but it should be a "solid" 2D shape. It means, if could be a close contour with one or more wholes in it, or something else. As an alternative to using a path made of lines, you can start with a simple base shape as rectangle or ellipse, and then add or subtract other shapes to/from it, using AND/OR set-theory operations. Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.region.aspx[^].

Please see the constructors which allows you to start with a rectangle or a graphic path (as you did, only use a "solid" closed path), and then, see the methods Exclude, Union and Xor; use other methods to transform the region, and so on.



As to the "line shape", there is no such thing. The closest representation of the idea would be an oblong rectangle with some finite non-zero width representing the "width of a line". It's only the mathematical abstract "line" has zero width, but, say, on-screen representation requires some width.

You can start with the rectangle, but, in general case, you need to tilt it to required angle using appropriate Transform:
http://msdn.microsoft.com/en-us/library/system.drawing.region.transform.aspx[^].

For example:
using System.Drawing;

//...

Region region = new Region(new Rectangle(x, y, width, 4));
System.Drawing.Drawing2D.Matrix shiftAndRotation = new System.Drawing.Drawing2D.Matrix();
shiftAndRotation.Rotate(requiredAngle);
shiftAndRotation.Translate(deltaX, deltaY); // shift
region.Transform = shiftAndRotation;
//...





-SA


或者您可以使用它:

更好的线路控制

[ ^ ]
Or you could use this:
a better line control
[^]


我在Visual Basic中找到Power Pack一个线形和形状容器这个容器可以像用户控件一样使用,例如:



I found in Visual Basic Power Pack a "Line Shape" and "Shape Container" this container can be used like a user control, for example:

ShapeContainer container = new ShapeContainer();
LineShape lineShape1 = new LineShape();
lineShape1.SelectionColor = this.BackColor;
lineShape1.X1 = 10;
lineShape1.X2 = 200;
lineShape1.Y1 = 30;
lineShape1.Y2 = 100;


container.Shapes.AddRange(new Microsoft.VisualBasic.PowerPacks.Shape[] {
lineShape1});

Point location = new Point();
location.X = 10;
location.Y = 20;

container.Location = location;

this.Controls.Add(container);

container.Location = location;





编辑:



它不起作用,因为容器没有起点和终点只有一个位置......它会有一个线形,但仍然是一个控件,就像一个文本框......





It doesn't work, because the container will no have a "Start Point and a EndPoint" will have only a Location... It will have a line shape, but still be a control, like a textbox...


这篇关于用户控件与自定义形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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