This might get you started:
using System.Drawing;
using System.Windows.Forms;
namespace Jan14_2014_GuideLine
{
public enum GuideOrientation
{
horizontal,
vertical
}
public partial class Guideline : UserControl
{
public Guideline()
{
InitializeComponent();
}
public GuideOrientation Orientation { get; set; }
public int Thickness { get; set; }
public Color GColor { get; set; }
private bool mDown = false;
private int mX, mY;
public Guideline(GuideOrientation gOrientation, int gThick, Color gColor): base()
{
Orientation = gOrientation;
Thickness = gThick;
GColor = gColor;
if(Orientation == GuideOrientation.horizontal)
{
this.Height = gThick;
}
else
{
this.Width = gThick;
}
this.BackColor = gColor;
this.MouseDown += Guideline_MouseDown;
this.MouseUp += Guideline_MouseUp;
this.MouseMove += Guideline_MouseMove;
this.Click += Guideline_Click;
}
void Guideline_Click(object sender, System.EventArgs e)
{
MessageBox.Show("GuideLine Clicked");
}
private void Guideline_MouseDown(object sender, MouseEventArgs e)
{
mDown = true;
mX = e.X;
mY = e.Y;
}
private void Guideline_MouseMove(object sender, MouseEventArgs e)
{
if (mDown)
{
this.Left += e.X - mX;
this.Top += e.Y - mY;
}
}
private void Guideline_MouseUp(object sender, MouseEventArgs e)
{
mDown = false;
}
}
}
Example of how you would add a horizontal, and vertical, GuideLine to a Form:
Guideline gLineH;
Guideline gLineV;
private void Form1_Load(object sender, EventArgs e)
{
gLineH = new Guideline(GuideOrientation.horizontal, 1, Color.Red);
this.Controls.Add(gLineH);
gLineH.Location = new Point(0, 100);
gLineH.Width = this.Width;
gLineH.BringToFront();
gLineV = new Guideline(GuideOrientation.vertical, 1, Color.Red);
this.Controls.Add(gLineV);
gLineV.Location = new Point(100, 0);
gLineV.Height = this.Height;
gLineV.BringToFront();
}
Discussion:
1. these are movable at run-time via click-drag; if you make the width, or height, 1 pixel, depending on your system (monitor, resolution) you may have some difficult getting a mouse-hit to start them moving. in this example the guides are set to either the width, or height, of the Form.
2. a logical extension of this would be to make the guides re-sizable at run-time which would not be that hard to do.
3. other nice features to add would be:
a. moving by increments (detente), possibly being "docked" to a ruler control
b. when clicked, visually show some sign of being "selected"
c. maintain a collection of the guides which could be edited at run-time
d. put a tool-tip on the guides that would show location; or ... fancier ... put up a label that "tracks" the guide being moved showing the changing co-ordinates.
Other Approaches:
1. Drawing on the surface of the Form via the Paint Event ... I would avoid this.
2. Using Visual Basic's shape-drawing facilities to create the guides: this is not difficult to do in C#.
3. You could, if you wish, use something like a Label instead of a UserControl.
Is it "wasteful" or "expensive" to use a bunch of UserControls as guides: imho, no.