[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Trouble with X and Y coordinates in PointToScreen and paint.

Posted on 2005-04-07
11
Medium Priority
?
461 Views
Last Modified: 2009-03-28
Haven't gotten our documentation yet so the frustration continues.  We are trying to click in a panel, drag the mouse while creating a dashed line rectable and when we click the mouse again it will paint a solid line rectangle.   Could anyone expain why, using some examples we found/given, the dashed box is offset from the upper left of the SCREEN but the paint places the solid rectangle correctly on the panel?  How do we get on the correct coordinate system or offset.

Jim
0
Comment
Question by:QualityData
11 Comments
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13731252
Im guessing that the release/2nd click get's it's coords based on the x and y coords of the pointer within the panel... but your paint event for the rectangle is using the form/screen graphics object... it will help if you paste up the problematic code
0
 
LVL 32

Expert Comment

by:Erick37
ID: 13731316
Normally the "rubberband" outline box is drawn directly to the screen, so you would use screen coordinates.  Once the mouse is released, the screen coords are converted to the window coords of your control so you can draw using the control's coords.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 13733383
Here is a bare bones click and drag rubberbanding demo app.  As S-Twiley and Erik37 have pointed out, the "rubberbanding" method (ControlPaint.DrawReversibleFrame) works in screen coordinates while the solid rectangle would be drawn using the clients (Panel/PictureBox) coordinates.  These conversions can be made with the following methods:

    PointToScreen() ' change client point coordinates to screen coordinates
    RectangleToScreen() ' change rectangle client coordinates to screen coordinates
    PointToClient() ' change screen point coordinates to client coordinates
    RectangleToClient() ' change screen rectangle coordinates to client coordinates



Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.PictureBox1.Location = New System.Drawing.Point(8, 8)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(616, 368)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19)
        Me.ClientSize = New System.Drawing.Size(632, 382)
        Me.Controls.Add(Me.PictureBox1)
        Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Name = "Form1"
        Me.Text = "RubberBanding in a PictureBox"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private startX As Integer
    Private startY As Integer
    Private lastX As Integer
    Private lastY As Integer
    Private firstBoxDrawn As Boolean = False
    Private creating As Boolean = False
    Private prevClip As Rectangle
    Private Boxes As New ArrayList

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button = MouseButtons.Left Then
            Dim p As Point = PictureBox1.PointToScreen(New Point(e.X, e.Y))
            startX = p.X
            startY = p.Y
        End If
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = MouseButtons.Left Then
            If Not creating Then
                creating = True
                firstBoxDrawn = False
                PictureBox1.Cursor.Hide()
                prevClip = Cursor.Clip
                Cursor.Clip = sender.RectangleToScreen(New Rectangle(0, 0, sender.Width - 1, sender.Height - 1))
            End If
            createBox(sender, e)
        End If
    End Sub

    Private Sub createBox(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim p As Point = sender.PointToScreen(New Point(e.X, e.Y))
        If Not firstBoxDrawn Then
            firstBoxDrawn = True
            ControlPaint.DrawReversibleFrame(New Rectangle(startX, startY, p.X - startX, p.Y - startY), Color.Black, FrameStyle.Dashed)
        Else
            ControlPaint.DrawReversibleFrame(New Rectangle(startX, startY, lastX - startX, lastY - startY), Color.Black, FrameStyle.Dashed)
            ControlPaint.DrawReversibleFrame(New Rectangle(startX, startY, p.X - startX, p.Y - startY), Color.Black, FrameStyle.Dashed)
        End If
        lastX = p.X
        lastY = p.Y
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        If e.Button = MouseButtons.Left Then
            If creating Then
                creating = False
                PictureBox1.Cursor.Show()
                Cursor.Clip = prevClip
                Dim r As Rectangle = New Rectangle(Math.Min(startX, lastX), Math.Min(startY, lastY), Math.Abs(lastX - startX), Math.Abs(lastY - startY))
                ControlPaint.DrawReversibleFrame(r, Color.Black, FrameStyle.Dashed)
                r = PictureBox1.RectangleToClient(r)
                Boxes.Add(r)
                PictureBox1.Refresh()
            End If
        End If
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Dim g As Graphics = e.Graphics
        Dim r As Rectangle
        For Each r In Boxes
            g.DrawRectangle(Pens.Black, r)
        Next
    End Sub

End Class
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:QualityData
ID: 13736030
I love you.  You should come up with some tutorial or something.  So far, without any quality documentation, it seems imposible to find this information.  Without this site and your answers Idle_Mind we'd be outta lukck.

Thanks Again,
Jim
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13736232
I'm pretty sure I'm not the only one who knows how to do these kinds of things...   =)

The information is out there...it's just in bits and pieces and usually not all put together in one place where it's obvious how to do things as a whole.  Coming from the VB6 side of things where you have to go to a pretty low level to accomplish these kinds of tasks makes it easier.
0
 

Author Comment

by:QualityData
ID: 13756587
I know you answered thios already but why doesn't this cooridinate system seem to work when I do just regular lines?
Jim
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13757620
You want to click and drag just simple lines in the PictureBox?
0
 

Author Comment

by:QualityData
ID: 13761859
Exactly, the coordinates are off again but I used esentially the same code as that for the rectangle but with ControlPaint.DrawReversibleLine for the drawline routine.
0
 

Author Comment

by:QualityData
ID: 13762089
I can get the correct start points and it is good when I'm dragging but when it draws the line it is offset.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13762769
Here is one way to do the previous example but for Lines:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.PictureBox1.Location = New System.Drawing.Point(8, 8)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(616, 368)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19)
        Me.ClientSize = New System.Drawing.Size(632, 382)
        Me.Controls.Add(Me.PictureBox1)
        Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Name = "Form1"
        Me.Text = "Reversible Lines in a PictureBox"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Class Line
        Public P1 As Point
        Public P2 As Point
    End Class

    Private L As Line
    Private firstLineDrawn As Boolean = False
    Private creating As Boolean = False
    Private prevClip As Rectangle
    Private Lines As New ArrayList

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button = MouseButtons.Left Then
            L = New Line
            L.P1 = New Point(e.X, e.Y)
        End If
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = MouseButtons.Left Then
            If Not creating Then
                creating = True
                firstLineDrawn = False
                PictureBox1.Cursor.Hide()
                prevClip = Cursor.Clip
                Cursor.Clip = sender.RectangleToScreen(New Rectangle(0, 0, sender.Width - 1, sender.Height - 1))
            End If
            drawLine(sender, e)
        End If
    End Sub

    Private Sub DrawLine(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim p As Point = New Point(e.X, e.Y)
        Dim pScreen As Point = sender.PointToScreen(p)
        Dim P1Screen As Point = sender.PointToScreen(L.P1)
        If Not firstLineDrawn Then
            firstLineDrawn = True
            ControlPaint.DrawReversibleLine(P1Screen, pScreen, Color.Black)
        Else
            ControlPaint.DrawReversibleLine(P1Screen, sender.PointToScreen(L.P2), Color.Black)
            ControlPaint.DrawReversibleLine(P1Screen, pScreen, Color.Black)
        End If
        L.P2 = p
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        If e.Button = MouseButtons.Left Then
            If creating Then
                creating = False
                PictureBox1.Cursor.Show()
                Cursor.Clip = prevClip
                Lines.Add(L)
            End If
        End If
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Dim g As Graphics = e.Graphics
        For Each L In Lines
            g.DrawLine(Pens.Black, L.P1, L.P2)
        Next
    End Sub

End Class

0
 

Author Comment

by:QualityData
ID: 13765103
Thanks again.  I was missing PointToScreen.  After that it worked fine.  This is semi chalenging stuff but it's fun learning new stuff.  I just don't like being this slow.  Also, it's my first time using Object Orientated Programming.  Wierd, wild stuff.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question