Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Precision cursor 1

Status
Not open for further replies.

M444

Programmer
Feb 24, 2004
76
0
0
US
Hi,

I am looking for suggestions as to the best way to create a precision "cursor" within a panel.

The cursor needs to have two lines one which streches across the panel on the x axis and one that streches from the y axis at the current mouse coordinates.

Thanks
 
Make single pixel labels that span top to bottom.

On mouse over/hover move the labels position to the mouse position. NOTE, you will have "fun" determining the correct location co-ords. I will give you answer to this, if you can't figure it out, after you have tried. Figuring it out requires that you understand why you got the co-ords you got, when you asked for the mouse position co-ords.


Good Luck, and post your code when you get it started/working.

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Thank you for the fast response Qik3Coder.

This is an interesting method which I had not yet come across. I'm not sure that this is the best approach however.

I have experimented with the following thus far:
> Using DrawLine to draw a line to the panel
> Creating an icon at runtime using calls to CreateIconIndirect and GetIconInfo

Thank you.
 
Did you try it? Granted you are going to wind up wanting flag for the event code being on/off.

Do you have a little more detail on what you are trying to do?

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Qik3Coder,

This misuse of the label control is simple and works surprisingly well.

For my own sanity, instead of using a label I adapted your method a bit by creating a user control, ucLine, and setting its background color to black.

Everything else as you described.

Thanks!

 
sometimes you just need to step away from the problem.

I had a weird issue where i need two datagrids to scale, relative to each other and the form, but the default anchor wasn't working.

I threw a label on the form. with set width, then i ran a update on the datagrid's height, and position based on the label.

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
I'd be interested in seeing an example of the single pixel Label code. Sounds interesting. Anyway, I thought I would post this sample using a more "conventional" approach. I didn't know about the DrawReversibleLine method until I started researching this question.
Code:
Public Class Form1

    Private pnlPoint As Point = New Point(-1, -1)

    Private Sub Panel1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.MouseLeave
        DrawCrosshair(Panel1, pnlPoint) 'Erase crosshair
        pnlPoint.X = -1
    End Sub

    Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove

        If (pnlPoint.X > -1) Then DrawCrosshair(Panel1, pnlPoint)

        pnlPoint.X = e.X
        pnlPoint.Y = e.Y

        DrawCrosshair(Panel1, pnlPoint)

        Me.Text = e.Location.ToString

    End Sub

    Private Sub DrawCrosshair(ByVal OnControl As Control, ByVal AtPoint As Point)

        Dim p2sPoint As Point = OnControl.PointToScreen(AtPoint)

        Dim strHorzPoint As Point = New Point(p2sPoint.X - AtPoint.X, p2sPoint.Y)
        Dim endHorzPoint As Point = New Point(strHorzPoint.X + OnControl.Size.Width, p2sPoint.Y)
        ControlPaint.DrawReversibleLine(strHorzPoint, endHorzPoint, Color.Black)

        Dim strVertPoint As Point = New Point(p2sPoint.X, p2sPoint.Y - AtPoint.Y)
        Dim endVertPoint As Point = New Point(p2sPoint.X, strVertPoint.Y + OnControl.Size.Height)
        ControlPaint.DrawReversibleLine(strVertPoint, endVertPoint, Color.Black)

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

End Class
 
I tried using the code above as I have a very similar need. It seems to work great with one exception...

When the panel control is partially covered by a window from another application the cross hairs show up in the other application instead of only on the panel of my original window.

Do you know how to keep crosshairs on oringinal form only?

TIA!
 
You will have to convert this from C#, I didn't have VB installed at the house.

Throw a couple labels on the form, panel, whatever.
Name them lblX, and lblY. You don't have to do anything else.

Code:
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            lblX.Top = e.Location.Y;
            lblY.Left = e.Location.X;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblX.Height = 1;
            lblX.Width = this.Width;
            lblX.BackColor = Color.Black;
            lblX.Left = 0;
            lblX.AutoSize = false;
            
            lblY.BackColor = Color.Black;
            lblY.Width = 1;
            lblY.Height = this.Height;
            lblY.Top = 0;
            lblY.AutoSize = false;
            
        }

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top