Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to use guideline on my ruler which i have added in my windows Application.
Please give any solution by which i can obtain it.
I have use ruler from link "http://www.codeproject.com/Articles/4369/Ruler-Control

Thanks
Posted
Updated 4-Feb-21 1:29am
v2
Comments
BillWoodruff 13-Jan-14 7:15am    
Please specify clearly exactly what functionality you wish associated with this "guideline."

Do you want to have multiple possible guidelines, to be able to click on them and move them do you want to have the guideline "snap" to certain predefined units of width, or height ... and so on.
Brian Lowery 1-Feb-21 13:02pm    
I have implemented your code but I do not see either the horizontal or vertical grids.

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;

            // note the necessity in VS 2013, FrameWork 4.5
            // to wire-up these EventHandlers here !
            // ask Microsoft why :)
            this.MouseDown += Guideline_MouseDown;
            this.MouseUp += Guideline_MouseUp;
            this.MouseMove += Guideline_MouseMove;

            this.Click += Guideline_Click;
        }

        void Guideline_Click(object sender, System.EventArgs e)
        {
            // for testing only ...
            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.
 
Share this answer
 
v3
Comments
Member 9013369 14-Jan-14 2:42am    
Its Working ,but when i move to guideline from one points to other it removes image behind it.
How could it be resolved.
Brian Lowery 1-Feb-21 18:42pm    
I have tried implementing this code in winform with C Sharp but was unable to see the rulers appear.

Has anyone been able to implement this?

Thank you!

Brian
Brian, If you are using that code sample as shown, it works. However if you are drag-dropping the Guideline usercontrol from the ToolBox onto a Form at design-time, then the Mouse handlers are never wired-up for the GuideLine instance.

edit: now using .NET 4.8, it seems something has changed : the Event Handlers are getting wired-up when it's drag-dropped onto the Form. or created in code. ! have to get my head to go back six years, somehow :)

I made the assumption in this quick sketch that the programmer would be creating these at run-time, and would specify the parameters in the ctor, and set initial size and location in code.

I am dealing with some health issues right now, but, hopefully, will revise the code soon as I have time: if it is shown in the ToolBox, you should be able to drag-drop onto a Form

Also, this quick sketch omits the GuideLine Mouse events from raising events their host container can subscribe to: in real-world use, you'd definitely want to do that.

Please describe in detail how you are using the control, now.

Try this change in the GuideLine ctor with parameters:
C#
public Guideline(GuideOrientation gOrientation, int gThick, Color gColor)
{
    if (!DesignMode)
    {
        this.MouseDown += Guideline_MouseDown;
        this.MouseUp += Guideline_MouseUp;
        this.MouseMove += Guideline_MouseMove;

        this.Click += Guideline_Click;
    }

    Orientation = gOrientation;
    Thickness = gThick;
    GColor = gColor;

    if(Orientation == GuideOrientation.horizontal)
    {
        this.Height = gThick;
    }
    else
    {
        this.Width = gThick;
    }

    this.BackColor = gColor;

}
 
Share this answer
 
Comments
Dave Kreskowiak 4-Feb-21 10:07am    
7 years old...
BillWoodruff 4-Feb-21 13:15pm    
Dave, I am responding to Brian's request for help (in a comment) posted 2 days ago :) Since it's a long response including code, I thought answering with solution rather than comment was an appropriate format.
Dave Kreskowiak 4-Feb-21 16:54pm    
Whoops. Missed that.
BillWoodruff 4-Feb-21 18:51pm    
Hey, Considering the state of this body keeping a mind afloat, right now, I'm glad you've got my back :)

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