Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a user control that have a line shape

C#
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
Posted

Or you could use this:
a better line control
[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Oct-13 17:07pm    
It looks like an interesting work of you, but could you explain how it that related to the OP's problem

The problem is really simple: the region of non-null measure cannot be defined by just one straight line segment, it should be at least one close contour, but even this is simpler than that, as OP could start with some base shape and cut out pieces to it, or OR some more areas to it, as I explained already.

—SA
Ron Beyer 10-Oct-13 17:11pm    
It relates to the first sentence: "I need a user control that have a line shape", the project is a custom control that has a line shape and transparency around the control. Your comment is 1000% correct, I was just offering an alternative to rolling his own.
Sergey Alexandrovich Kryukov 10-Oct-13 17:17pm    
I know. I put an update to take it into account. Please see, after [EDIT]. This is really a simple thing...
—SA
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.

[EDIT]

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:
C#
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
 
Share this answer
 
v2
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;


EDIT:

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...
 
Share this answer
 
v4

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