none
Zoom window in that picture box by mouse RRS feed

  • Question

  • Hi,

    I draw an vector graphics in a picture box and I want to zoom window in that picture box by mouse. How to write a code to zoom window in that picture box and how to back the drawing to its original state.

    The below code is only suggest and maybe need some additional code or some improvement.

    The below code provide Xini = 0 and Yini = 0, That the zoom window start from point (0,0).

    I don't know my mistake but maybe mouse left button double click does not read e.X and e.y?

    Is it possible to make left button click instead of left button double click?

    Can we store the the value of Xcorner and Ycorner at the start mouse down, to make it equal to Xini and Yini? 

    I need also to improve that code.

    Dim Xini As Integer
        Dim Yini As Integer
        Dim Xcorner As Integer
        Dim Ycorner As Integer
        Dim MouseStatus As Integer
        Dim Mimi As Integer
    Private Sub PictureBox2_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox2.Paint
    Dim mpen4 As Pen = New Pen(Color.LightGray, 2.0F)
                mpen4.DashStyle = Drawing2D.DashStyle.Dash
                If MouseStatus = 1 And CheckBox1.Checked = True Then
                    e.Graphics.DrawRectangle(mpen4, Xini, Yini, Xcorner - Xini, Ycorner - Yini)
                End If
    Private Sub PictureBox2_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseDown
            If CheckBox1.Checked = True Then
                MouseStatus = 1
                Xcorner = e.X
                Ycorner = e.Y
            End If
        End Sub
        Private Sub PictureBox2_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseMove
            If CheckBox1.Checked = True And MouseStatus = 1 Then
                Xcorner = e.X
                Ycorner = e.Y
                PictureBox2.Invalidate()
            End If
        End Sub
        Private Sub PictureBox2_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseUp
            If CheckBox1.Checked = True And MouseStatus = 1 Then
                Xcorner = e.X
                Ycorner = e.Y
                If e.Button = MouseButtons.Right = True Then
                    Mimi = 4
                Else
                    Mimi = 1
                End If
            End If
        End Sub
        Private Sub PictureBox2_MousedoubleClick(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseDoubleClick
            If e.Button = MouseButtons.Left = True Then
                If CheckBox1.Checked = True Then
                    Xini = e.X
                    Yini = e.Y
                Else
                End If
            End If
        End Sub


    Kind Regards,


    Hany Metry







    Tuesday, November 12, 2019 2:01 AM

Answers

  • "Is it possible to make left button click instead of left button double click?"

    Yes.

    Hi,

    I have a check box at nearly top left corner of picture box and I avoid to use left button click to avoid when I check box = true, start Xini and Yini = 0,0



    Hany Metry


    Hany,

    Yes your questions are valid sort of. You just have to look and learn it now.

    It is easier to show the code than to describe it in written English.

    Study the order of the mouse events here:

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/mouse-events-in-windows-forms

    Recall I have mentioned that sometimes it will be easier to enter coordinates by text than using a mouse. It depends. What are you defining? The structure section shape like an I beam? Even a rectangle can be hard to define exactly with the mouse. If the size is 11.72 x 43.33 that is hard to click with a mouse. It depends.

    So you may decide you dont want to use the mouse for some things??? Coordinates might easier for some tasks.

    Or not.

    You may need to do it both ways and see what seems best.

    This example only works for a zoom rectangle drawn downward from left to right.

    The example is mostly the same as the previous one I posted with line tools.

    The example makes the controls just cut and paste the code into an empty form. Change the form name as required.

    Note how by setting ScaleWidth and Corner point you set the view window. Same a previous examples I show. But in the other examples we did not change the view.

    'mouse zoom in rectangle fence v1 with positive y axis, large values
    
    Public Class Form5
        Private WithEvents Picturebox1 As New PictureBox With {.Parent = Me, .BackColor = Color.White}
        Private WithEvents ZoomCb As New CheckBox With {.Parent = Me, .FlatStyle = FlatStyle.Flat,
            .Location = New Point(260, 10), .Size = New Size(60, 24), .Text = "Zoom", .Appearance = Appearance.Button,
            .BackColor = Color.AntiqueWhite, .TextAlign = ContentAlignment.MiddleCenter, .Name = "Zoom"}
    
        Private ScaleWidth As Single = 100000
        Private ScaleRatio As Single = 1
        Private Yoffset As Single
        Private Corner As New Point(0, 0)
        Private GridStep As Single = ScaleWidth / 5
        Private SnapStep As Single = GridStep
        Private MouseDownPt, MouseMovePt, ZoomMouseDownPt, ZoomMouseMovePt As PointF
        Private MouseStatus As Integer
        Private ToolCbCancel As Boolean = False
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(400, 300)
            DoubleBuffered = True
            Text = "Zoom Rectangle Fence"
            BackColor = Color.BurlyWood
            ZoomCb.FlatAppearance.CheckedBackColor = Color.LightSalmon
    
            'draw initial view
            Form2_Resize(0, Nothing)
            Picturebox1.Focus()
        End Sub
    
        Private Sub Form2_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Dim border As Integer = 20
            Picturebox1.Location = New Point(border, 50)
            Picturebox1.Size = New Size(ClientSize.Width - (2 * border), ClientSize.Height - (50 + border))
            Yoffset = ScaleWidth * (Picturebox1.ClientSize.Height / Picturebox1.ClientSize.Width)
            Picturebox1.Invalidate()
    
        End Sub
    
        Private Sub Picturebox1_MouseDown(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseDown
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If ZoomCb.Checked Then
                'keep zoom independant of drawing tools to allow nesting of zoom with tools.
                ZoomMouseDownPt = pt
                ZoomMouseMovePt = ZoomMouseDownPt
            End If
    
            MouseStatus = 1
            MouseMovePt = pt
    
            Picturebox1.Invalidate()
            Picturebox1.Focus()
        End Sub
    
        Private Sub Picturebox1_MouseMove(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseMove
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If MouseStatus = 1 Then
    
                If ZoomCb.Checked Then
                    ZoomMouseMovePt = pt
                    Picturebox1.Invalidate()
    
                End If
            End If
    
        End Sub
    
        Private Sub Picturebox1_MouseUp(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseUp
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If ZoomCb.Checked Then
                ZoomMouseMovePt = pt
                ZoomCb.Checked = False
    
                'set scale based on zoom rect
                Select Case MouseStatus
                    Case 1
                        ZoomMouseMovePt = pt
                        ZoomCb.Checked = False
    
                        'set scale based on zoom rect width
                        Dim dx As Integer = ZoomMouseMovePt.X - ZoomMouseDownPt.X
                        Dim dy As Integer = ZoomMouseMovePt.Y - ZoomMouseDownPt.Y
                        ScaleWidth = dx
    
                        'set origin
                        Corner.X = ZoomMouseDownPt.X
                        Corner.Y = ZoomMouseDownPt.Y + dy
    
                        'update yoffset etc
                        Form2_Resize(0, Nothing)
    
                End Select
            End If
    
            MouseStatus = 0
            Picturebox1.Invalidate()
        End Sub
    
        Private Sub Picturebox1_MouseWheel(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseWheel
            ScaleRatio += Math.Sign(e.Delta) * 0.1
            If ScaleRatio < 0.000001 Then ScaleRatio = 0.000001
            If ScaleRatio > 10000 Then ScaleRatio = 10000
    
            Picturebox1.Invalidate()
        End Sub
    
        Private Sub Picturebox1_MouseEnter(sender As Object, e As EventArgs) Handles Picturebox1.MouseEnter
            Picturebox1.Focus()
        End Sub
    
        Private Sub Picturebox1_Paint(sender As Object, e As PaintEventArgs) Handles Picturebox1.Paint
            With e.Graphics
    
                .Clear(Color.White)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
                Dim sf As Single = CSng(Picturebox1.ClientSize.Width / (ScaleRatio * ScaleWidth))
                .ScaleTransform(sf, sf)
                .TranslateTransform(-Corner.X, Corner.Y)
    
                DrawGrid(e.Graphics)
    
                'draw x just for zooming example objects
                .DrawLine(Pens.Green, 40000, Yoffset - 20000, 60000, Yoffset - 40000)
                .DrawLine(Pens.Green, 40000, Yoffset - 40000, 60000, Yoffset - 20000)
    
                If MouseStatus = 1 Then
                    If ZoomCb.Checked Then
    
                        'convert for positive yaxis and rectf
                        Dim rect As New Rectangle(ZoomMouseDownPt.X, Yoffset - ZoomMouseDownPt.Y,
                                              ZoomMouseMovePt.X - ZoomMouseDownPt.X,
                                              ZoomMouseDownPt.Y - ZoomMouseMovePt.Y)
    
                        .DrawRectangle(Pens.Red, rect)
                    Else
                        'draw other tracers if active
    
                    End If
                End If
            End With
        End Sub
    
        Private Sub DrawGrid(g As Graphics)
            'setup drawing corners etc
            Dim x1 As Single = RoundToIncrement(Corner.X - GridStep, CInt(GridStep))
            Dim y1 As Single = RoundToIncrement(Corner.Y - GridStep, CInt(GridStep))
            Dim sw As Single = CSng(ScaleRatio * ScaleWidth + (2 * GridStep))
            Dim pxlSize As Single = ScaleRatio * ScaleWidth / Picturebox1.ClientSize.Width
    
            With g
                Using pg As New Pen(Color.DarkGray, CSng(pxlSize / 6)),
                        pgrn As New Pen(Color.Green, CSng(pxlSize / 6)),
                        f As New Font("arial", CSng(11 * pxlSize)),
                        br As New SolidBrush(Color.DimGray)
    
                    'draw origin
                    .DrawLine(pgrn, -pxlSize, Yoffset, pxlSize, Yoffset)
                    .DrawLine(pgrn, 0, Yoffset - (-pxlSize), 0, Yoffset - pxlSize)
    
                    'x axis grid
                    For x As Single = x1 To CSng(x1 + sw) Step GridStep
                        .DrawLine(pg, x, Yoffset - y1, x, Yoffset - CSng(y1 + sw))
                        .DrawString(x.ToString, f, br, x, Yoffset - (Corner.Y + (20 * pxlSize)))
                    Next
    
                    For y As Single = y1 To CSng(y1 + sw) Step GridStep
                        .DrawLine(pg, x1, Yoffset - y, CSng(x1 + sw), Yoffset - y)
                        .DrawString(y.ToString, f, br, Corner.X, Yoffset - y)
                    Next
                End Using
            End With
        End Sub
    
        Private Function RoundToIncrement(theValue As Double, roundIncrement As Integer) As Integer
            'ie value = 1433 roundinc = 100 returns 1400
            Return CInt((CDbl(theValue) + (0.5 * roundIncrement)) / roundIncrement) * roundIncrement
        End Function
    
        Private Function GetScalePtFromClientPt(pt As PointF) As PointF
            'convert screen pixels to scale drawing coords
            Dim sf As Double = Picturebox1.ClientSize.Width / (ScaleWidth * ScaleRatio)
            Return New PointF(CSng(Corner.X + (pt.X / sf)), CSng(Corner.Y + (Yoffset - pt.Y / sf)))
        End Function
    End Class


    • Edited by tommytwotrain Friday, November 15, 2019 11:59 AM new gif
    • Marked as answer by Hany Metry Friday, November 15, 2019 12:40 PM
    Friday, November 15, 2019 11:43 AM

All replies

  • Hi

    Do you not think that you should mention the fact that you do not have a mouse with a wheel? Or

    do you want the next set of contributors to go through that whole thing and waste their time!

    ALSO, the thread title is just that - a TITLE, not an essay.


    Regards Les, Livingston, Scotland

    Tuesday, November 12, 2019 2:18 AM
  • Private Sub PictureBox2_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseUp
            If CheckBox1.Checked = True And MouseStatus = 1 Then
                If e.Button = MouseButtons.Right = True Then
                    Mimi = 4
                Else
                    Mimi = 1
                End If
            End If
            MouseStatus = 0
        End Sub

    Hi,

    The above is modification of the code.


    Hany Metry

    Tuesday, November 12, 2019 5:21 AM
  • Hany,

    I either dont understand or cant reproduce the problem.

    You dont need - true in:

        If e.Button = MouseButtons.Left = True Then


    Doubleclick is awkward I would not use it.

    You can use combination key press to get more options ie

      <Ctrl Key Pressed Down> + <Left Mouse Button Down>

    could be used to initiate zoom in fence.

    "Is it possible to make left button click instead of left button double click?"

    Yes.

    'Can we store the the value of Xcorner and Ycorner at the start mouse down, to make it equal to Xini and Yini? "

    Yes.

    PS there is no mouseup with doubleclick. So its not the same.
    Wednesday, November 13, 2019 11:41 AM
  • Hi,

    "How to write a code to zoom window in that picture box and how to back the drawing to its original state."

    I don't quite understand, can you explain it for me please?

    From which point does it start drawing each time it is drawn? Is it the point at which the last mouse ended drawing or the point at which the current mouse tried to press?

    In addition, what kind of original state do you want to return to by double clicking the mouse?

    Of cause you can store the the value of Xcorner and Ycorner at the start mouse down to make it equal to Xini and Yini.

    I tried to use your code and then coded for you, but I am sorry that these problems have trapped me.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 14, 2019 6:51 AM
    Moderator
  • Hi,

    "How to write a code to zoom window in that picture box and how to back the drawing to its original state."

    I don't quite understand, can you explain it for me please?

    From which point does it start drawing each time it is drawn? Is it the point at which the last mouse ended drawing or the point at which the current mouse tried to press?

    In addition, what kind of original state do you want to return to by double clicking the mouse?

    Of cause you can store the the value of Xcorner and Ycorner at the start mouse down to make it equal to Xini and Yini.

    I tried to use your code and then coded for you, but I am sorry that these problems have trapped me.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Hi,

    The program draw vector graphics of unknown shape with its reinforcement in a picture box but for large size shapes the graphics was not clear and to see the graphics, you must zoom+ by draw a rectangle with two opposite corner two points (Xini,Yini and Xcorner,Ycorner) and the part chosen by shall be redrawn in the full size of picture box scale to fit.

     The code above is only select the two opposite corner and draw a rectangle and not draw shape again, that the code to draw shape is not shown.

    Original state is to back to draw full graphics without zoom (the graphics before zoom).

    My code to select the two opposite corner need some improvement and it is still initial and it is not final.

    Kind Regards,


    Hany Metry


    • Edited by Hany Metry Friday, November 15, 2019 5:17 AM
    Thursday, November 14, 2019 8:06 AM
  • 'Can we store the the value of Xcorner and Ycorner at the start mouse down, to make it equal to Xini and Yini? "

    Yes.

    PS there is no mouseup with doubleclick. So its not the same.

    Hi,

    Instead of having (Xini,Yini) and if we are able to store Xcorner & Ycorner at start of mouse down. Then we can cancel mouse double click event which is not good.

    But how to write code to store Xcorner & Ycorner at start of mouse down.

    Your previous code of the canceled thread was draw the rectangle very correct but the problem is that (Xini,Yini) = (Xcorner,Ycorner) and you told me before at thread which draw lines by mouse that we can store each value of mouse draw.

    So, If we can store the value of Xcorner,Ycorner at start of mouse down, then (Xini,Yini) shall not be find by mouse while it will be find = Xcorner,Ycorner after start drawing first corner by mouse during mouse down.

    Please write the code.

    Kind Regards,


    Hany Metry



    • Edited by Hany Metry Friday, November 15, 2019 8:03 AM
    Friday, November 15, 2019 5:28 AM
  • Hany,

    I either dont understand or cant reproduce the problem.

    Hi,

    Actually I have a check box above left top corner of the picture box and when I check the check box = true, (some times I check box =true by double click) the mouse start draw at 0,0.

    Kind Regards,


    Hany Metry



    • Edited by Hany Metry Friday, November 15, 2019 8:03 AM
    Friday, November 15, 2019 5:34 AM
  • Doubleclick is awkward I would not use it.

    You can use combination key press to get more options ie

      <Ctrl Key Pressed Down> + <Left Mouse Button Down>

    could be used to initiate zoom in fence.

    Hi,

    I see that mouse Double Click  is difficult, so  <Ctrl Key Pressed Down> + <Left Mouse Button Down> will be still difficult as well as to remove your hand from <Ctrl Key Pressed Down>, that take time, so the Xini and Yini will be changed.

    And for that reason, I need to store Xcorner and Ycorner at start of mouse down and cancel Xini and Yini.


    Hany Metry



    • Edited by Hany Metry Friday, November 15, 2019 8:03 AM
    Friday, November 15, 2019 5:42 AM
  • "Is it possible to make left button click instead of left button double click?"

    Yes.

    Hi,

    I have a check box at nearly top left corner of picture box and I avoid to use left button click to avoid when I check box = true, start Xini and Yini = 0,0



    Hany Metry


    • Edited by Hany Metry Friday, November 15, 2019 8:03 AM
    Friday, November 15, 2019 5:47 AM
  • "Is it possible to make left button click instead of left button double click?"

    Yes.

    Hi,

    I have a check box at nearly top left corner of picture box and I avoid to use left button click to avoid when I check box = true, start Xini and Yini = 0,0



    Hany Metry


    Hany,

    Yes your questions are valid sort of. You just have to look and learn it now.

    It is easier to show the code than to describe it in written English.

    Study the order of the mouse events here:

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/mouse-events-in-windows-forms

    Recall I have mentioned that sometimes it will be easier to enter coordinates by text than using a mouse. It depends. What are you defining? The structure section shape like an I beam? Even a rectangle can be hard to define exactly with the mouse. If the size is 11.72 x 43.33 that is hard to click with a mouse. It depends.

    So you may decide you dont want to use the mouse for some things??? Coordinates might easier for some tasks.

    Or not.

    You may need to do it both ways and see what seems best.

    This example only works for a zoom rectangle drawn downward from left to right.

    The example is mostly the same as the previous one I posted with line tools.

    The example makes the controls just cut and paste the code into an empty form. Change the form name as required.

    Note how by setting ScaleWidth and Corner point you set the view window. Same a previous examples I show. But in the other examples we did not change the view.

    'mouse zoom in rectangle fence v1 with positive y axis, large values
    
    Public Class Form5
        Private WithEvents Picturebox1 As New PictureBox With {.Parent = Me, .BackColor = Color.White}
        Private WithEvents ZoomCb As New CheckBox With {.Parent = Me, .FlatStyle = FlatStyle.Flat,
            .Location = New Point(260, 10), .Size = New Size(60, 24), .Text = "Zoom", .Appearance = Appearance.Button,
            .BackColor = Color.AntiqueWhite, .TextAlign = ContentAlignment.MiddleCenter, .Name = "Zoom"}
    
        Private ScaleWidth As Single = 100000
        Private ScaleRatio As Single = 1
        Private Yoffset As Single
        Private Corner As New Point(0, 0)
        Private GridStep As Single = ScaleWidth / 5
        Private SnapStep As Single = GridStep
        Private MouseDownPt, MouseMovePt, ZoomMouseDownPt, ZoomMouseMovePt As PointF
        Private MouseStatus As Integer
        Private ToolCbCancel As Boolean = False
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(400, 300)
            DoubleBuffered = True
            Text = "Zoom Rectangle Fence"
            BackColor = Color.BurlyWood
            ZoomCb.FlatAppearance.CheckedBackColor = Color.LightSalmon
    
            'draw initial view
            Form2_Resize(0, Nothing)
            Picturebox1.Focus()
        End Sub
    
        Private Sub Form2_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Dim border As Integer = 20
            Picturebox1.Location = New Point(border, 50)
            Picturebox1.Size = New Size(ClientSize.Width - (2 * border), ClientSize.Height - (50 + border))
            Yoffset = ScaleWidth * (Picturebox1.ClientSize.Height / Picturebox1.ClientSize.Width)
            Picturebox1.Invalidate()
    
        End Sub
    
        Private Sub Picturebox1_MouseDown(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseDown
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If ZoomCb.Checked Then
                'keep zoom independant of drawing tools to allow nesting of zoom with tools.
                ZoomMouseDownPt = pt
                ZoomMouseMovePt = ZoomMouseDownPt
            End If
    
            MouseStatus = 1
            MouseMovePt = pt
    
            Picturebox1.Invalidate()
            Picturebox1.Focus()
        End Sub
    
        Private Sub Picturebox1_MouseMove(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseMove
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If MouseStatus = 1 Then
    
                If ZoomCb.Checked Then
                    ZoomMouseMovePt = pt
                    Picturebox1.Invalidate()
    
                End If
            End If
    
        End Sub
    
        Private Sub Picturebox1_MouseUp(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseUp
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If ZoomCb.Checked Then
                ZoomMouseMovePt = pt
                ZoomCb.Checked = False
    
                'set scale based on zoom rect
                Select Case MouseStatus
                    Case 1
                        ZoomMouseMovePt = pt
                        ZoomCb.Checked = False
    
                        'set scale based on zoom rect width
                        Dim dx As Integer = ZoomMouseMovePt.X - ZoomMouseDownPt.X
                        Dim dy As Integer = ZoomMouseMovePt.Y - ZoomMouseDownPt.Y
                        ScaleWidth = dx
    
                        'set origin
                        Corner.X = ZoomMouseDownPt.X
                        Corner.Y = ZoomMouseDownPt.Y + dy
    
                        'update yoffset etc
                        Form2_Resize(0, Nothing)
    
                End Select
            End If
    
            MouseStatus = 0
            Picturebox1.Invalidate()
        End Sub
    
        Private Sub Picturebox1_MouseWheel(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseWheel
            ScaleRatio += Math.Sign(e.Delta) * 0.1
            If ScaleRatio < 0.000001 Then ScaleRatio = 0.000001
            If ScaleRatio > 10000 Then ScaleRatio = 10000
    
            Picturebox1.Invalidate()
        End Sub
    
        Private Sub Picturebox1_MouseEnter(sender As Object, e As EventArgs) Handles Picturebox1.MouseEnter
            Picturebox1.Focus()
        End Sub
    
        Private Sub Picturebox1_Paint(sender As Object, e As PaintEventArgs) Handles Picturebox1.Paint
            With e.Graphics
    
                .Clear(Color.White)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
                Dim sf As Single = CSng(Picturebox1.ClientSize.Width / (ScaleRatio * ScaleWidth))
                .ScaleTransform(sf, sf)
                .TranslateTransform(-Corner.X, Corner.Y)
    
                DrawGrid(e.Graphics)
    
                'draw x just for zooming example objects
                .DrawLine(Pens.Green, 40000, Yoffset - 20000, 60000, Yoffset - 40000)
                .DrawLine(Pens.Green, 40000, Yoffset - 40000, 60000, Yoffset - 20000)
    
                If MouseStatus = 1 Then
                    If ZoomCb.Checked Then
    
                        'convert for positive yaxis and rectf
                        Dim rect As New Rectangle(ZoomMouseDownPt.X, Yoffset - ZoomMouseDownPt.Y,
                                              ZoomMouseMovePt.X - ZoomMouseDownPt.X,
                                              ZoomMouseDownPt.Y - ZoomMouseMovePt.Y)
    
                        .DrawRectangle(Pens.Red, rect)
                    Else
                        'draw other tracers if active
    
                    End If
                End If
            End With
        End Sub
    
        Private Sub DrawGrid(g As Graphics)
            'setup drawing corners etc
            Dim x1 As Single = RoundToIncrement(Corner.X - GridStep, CInt(GridStep))
            Dim y1 As Single = RoundToIncrement(Corner.Y - GridStep, CInt(GridStep))
            Dim sw As Single = CSng(ScaleRatio * ScaleWidth + (2 * GridStep))
            Dim pxlSize As Single = ScaleRatio * ScaleWidth / Picturebox1.ClientSize.Width
    
            With g
                Using pg As New Pen(Color.DarkGray, CSng(pxlSize / 6)),
                        pgrn As New Pen(Color.Green, CSng(pxlSize / 6)),
                        f As New Font("arial", CSng(11 * pxlSize)),
                        br As New SolidBrush(Color.DimGray)
    
                    'draw origin
                    .DrawLine(pgrn, -pxlSize, Yoffset, pxlSize, Yoffset)
                    .DrawLine(pgrn, 0, Yoffset - (-pxlSize), 0, Yoffset - pxlSize)
    
                    'x axis grid
                    For x As Single = x1 To CSng(x1 + sw) Step GridStep
                        .DrawLine(pg, x, Yoffset - y1, x, Yoffset - CSng(y1 + sw))
                        .DrawString(x.ToString, f, br, x, Yoffset - (Corner.Y + (20 * pxlSize)))
                    Next
    
                    For y As Single = y1 To CSng(y1 + sw) Step GridStep
                        .DrawLine(pg, x1, Yoffset - y, CSng(x1 + sw), Yoffset - y)
                        .DrawString(y.ToString, f, br, Corner.X, Yoffset - y)
                    Next
                End Using
            End With
        End Sub
    
        Private Function RoundToIncrement(theValue As Double, roundIncrement As Integer) As Integer
            'ie value = 1433 roundinc = 100 returns 1400
            Return CInt((CDbl(theValue) + (0.5 * roundIncrement)) / roundIncrement) * roundIncrement
        End Function
    
        Private Function GetScalePtFromClientPt(pt As PointF) As PointF
            'convert screen pixels to scale drawing coords
            Dim sf As Double = Picturebox1.ClientSize.Width / (ScaleWidth * ScaleRatio)
            Return New PointF(CSng(Corner.X + (pt.X / sf)), CSng(Corner.Y + (Yoffset - pt.Y / sf)))
        End Function
    End Class


    • Edited by tommytwotrain Friday, November 15, 2019 11:59 AM new gif
    • Marked as answer by Hany Metry Friday, November 15, 2019 12:40 PM
    Friday, November 15, 2019 11:43 AM
  • Hi Mr: Tommy,

    Thank you very much.

    Really you are brilliant in all graphics and charts work. 

    I will try to understand the code and re-write it as appropriate to my case after doing some things else (Finish printing report, print screen, adjust some default of the program and adjust other graphics drawing).

    I expect that I will try to understand that after nearly 4-7 days.

    If I have a question to understand the code, I will raise it, and please reply to me.

    Kind Regards,


    Hany Metry


    • Edited by Hany Metry Friday, November 15, 2019 1:42 PM print screen in addition to print report
    Friday, November 15, 2019 12:53 PM
  • Dear Mr Tommy,

    As per your reply below, I want to high light the followings:

    1- Irregular shapes may be rectangle with layout of reinforcement not regular or maybe box section or maybe U shapes or maybe L section or maybe I section or maybe channel section or maybe T section or any polygon shapes and it is not a beam but it is a column (the maximum number of input coordinates for the shape allowed by the program is 30).

    2- It is more difficult to input the reinforcement bars at its center by mouse and it is also difficult to input bars by coordinates because number of bars allowed in my program are 2000 bars, so by coordinates it is impossible and by mouse it is impossible, and in my program the user shall input bars by array (just inform program by maximum spacing between bars, cover of reinforcement and diameter of bars).

     3- At other screen, the arrayed bars shall be shown in picture box and list view by its serial number (shown in both list view and picture box) the user may modify some bars diameters by increase the diameter of corner bars for example and may delete some bars by make the spacing at some parts = double spacing).

    All the modification by delete or modify the diameter shall be done from list view.

    4- This version will be finished about 15 December 2019. 

    5- Maybe I will make new version of the program by adding input data by mouse at the next version of that program but after finish this version of that program, I will start new version of footing program and after finish new version of footing program, I will start new version of pile cap program and after finish new version of pile cap program, maybe I will back again to column program to make new version by input data by mouse.

    6- That version of column program started at 1st of October 2019 and expected to be finished by 15 December 2019 (expected to takes 2.5 months) including all structural engineering works and all numerical integrations, ....

    7- I am graduated from faculty of engineering, civil engineering and I have a post graduate study in structural engineering and I am not graduated from computer science and all the my learning in programming is done from Forums site and searching in the internet.

    Kind Regards,

     


    Hany Metry

    Friday, November 15, 2019 2:46 PM
  • Dear Mr Tommy,

    As per your reply below, I want to high light the followings:

    1- Irregular shapes may be rectangle with layout of reinforcement not regular or maybe box section or maybe U shapes or maybe L section or maybe I section or maybe channel section or maybe T section or any polygon shapes and it is not a beam but it is a column (the maximum number of input coordinates for the shape allowed by the program is 30).

    2- It is more difficult to input the reinforcement bars at its center by mouse and it is also difficult to input bars by coordinates because number of bars allowed in my program are 2000 bars, so by coordinates it is impossible and by mouse it is impossible, and in my program the user shall input bars by array (just inform program by maximum spacing between bars, cover of reinforcement and diameter of bars).

     3- At other screen, the arrayed bars shall be shown in picture box and list view by its serial number (shown in both list view and picture box) the user may modify some bars diameters by increase the diameter of corner bars for example and may delete some bars by make the spacing at some parts = double spacing).

    All the modification by delete or modify the diameter shall be done from list view.

    4- This version will be finished about 15 December 2019. 

    5- Maybe I will make new version of the program by adding input data by mouse at the next version of that program but after finish this version of that program, I will start new version of footing program and after finish new version of footing program, I will start new version of pile cap program and after finish new version of pile cap program, maybe I will back again to column program to make new version by input data by mouse.

    6- That version of column program started at 1st of October 2019 and expected to be finished by 15 December 2019 (expected to takes 2.5 months) including all structural engineering works and all numerical integrations, ....

    7- I am graduated from faculty of engineering, civil engineering and I have a post graduate study in structural engineering and I am not graduated from computer science and all the my learning in programming is done from Forums site and searching in the internet.

    Kind Regards,

     


    Hany Metry

    Dear Mr. Tommy,

    To input data by mouse, I must do the following:

    1- Able to delete any bar(s) by select it by mouse (select window or select one bar).

    2- Able to make array of reinforcement by mouse by selection of any part of the irregular shape by mouse and ask the program to make array of reinforcement for that part.

    3- I must do pan and zoom out and zoom in.

    4- Able to modify any bar(s) diameter by select it by mouse (select window or select one bar)

    5- Able to move any bar(s) by mouse (select window or select one bar).

    6- Able to move any part of concrete section by any dimension and able to close the section again.

    7- Able to add any shape or part of shape to the concrete section by select add.l

    So it will be a drawing project and if I will be able to do all the above, then input shape and reinforcement by mouse will be very useful.

    But all the above is too difficult to do it now with my qualification of using mouse to draw and modify,...

    Kind Regards,



    Hany Metry


    • Edited by Hany Metry Tuesday, November 19, 2019 5:11 AM Replace you by I
    Friday, November 15, 2019 5:41 PM
  • So you are now ordering bespoke software from TommyTwoTrain!

    Well well, what ever next!

    *

    That list of instructions are for YOU to do, not a shopping list to try and get somebody else to do.

    I suppose you expect to sell the application once you get someone else to write it for you!


    Regards Les, Livingston, Scotland

    Friday, November 15, 2019 5:45 PM
  • So you are now ordering bespoke software from TommyTwoTrain!

    Well well, what ever next!

    *

    That list of instructions are for YOU to do, not a shopping list to try and get somebody else to do.

    I suppose you expect to sell the application once you get someone else to write it for you!


    Regards Les, Livingston, Scotland

    I will try to do all the above in the 3rd version of that program but not that version.

    Hany Metry

    Friday, November 15, 2019 5:54 PM

  • Hany,

    Yes your questions are valid sort of. You just have to look and learn it now.

    It is easier to show the code than to describe it in written English.

    Study the order of the mouse events here:

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/mouse-events-in-windows-forms

    Recall I have mentioned that sometimes it will be easier to enter coordinates by text than using a mouse. It depends. What are you defining? The structure section shape like an I beam? Even a rectangle can be hard to define exactly with the mouse. If the size is 11.72 x 43.33 that is hard to click with a mouse. It depends.

    So you may decide you dont want to use the mouse for some things??? Coordinates might easier for some tasks.

    Or not.

    You may need to do it both ways and see what seems best.

    This example only works for a zoom rectangle drawn downward from left to right.

    The example is mostly the same as the previous one I posted with line tools.

    The example makes the controls just cut and paste the code into an empty form. Change the form name as required.

    Note how by setting ScaleWidth and Corner point you set the view window. Same a previous examples I show. But in the other examples we did not change the view.

    'mouse zoom in rectangle fence v1 with positive y axis, large values
    
    Public Class Form5
        Private WithEvents Picturebox1 As New PictureBox With {.Parent = Me, .BackColor = Color.White}
        Private WithEvents ZoomCb As New CheckBox With {.Parent = Me, .FlatStyle = FlatStyle.Flat,
            .Location = New Point(260, 10), .Size = New Size(60, 24), .Text = "Zoom", .Appearance = Appearance.Button,
            .BackColor = Color.AntiqueWhite, .TextAlign = ContentAlignment.MiddleCenter, .Name = "Zoom"}
    
        Private ScaleWidth As Single = 100000
        Private ScaleRatio As Single = 1
        Private Yoffset As Single
        Private Corner As New Point(0, 0)
        Private GridStep As Single = ScaleWidth / 5
        Private SnapStep As Single = GridStep
        Private MouseDownPt, MouseMovePt, ZoomMouseDownPt, ZoomMouseMovePt As PointF
        Private MouseStatus As Integer
        Private ToolCbCancel As Boolean = False
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(400, 300)
            DoubleBuffered = True
            Text = "Zoom Rectangle Fence"
            BackColor = Color.BurlyWood
            ZoomCb.FlatAppearance.CheckedBackColor = Color.LightSalmon
    
            'draw initial view
            Form2_Resize(0, Nothing)
            Picturebox1.Focus()
        End Sub
    
        Private Sub Form2_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Dim border As Integer = 20
            Picturebox1.Location = New Point(border, 50)
            Picturebox1.Size = New Size(ClientSize.Width - (2 * border), ClientSize.Height - (50 + border))
            Yoffset = ScaleWidth * (Picturebox1.ClientSize.Height / Picturebox1.ClientSize.Width)
            Picturebox1.Invalidate()
    
        End Sub
    
        Private Sub Picturebox1_MouseDown(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseDown
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If ZoomCb.Checked Then
                'keep zoom independant of drawing tools to allow nesting of zoom with tools.
                ZoomMouseDownPt = pt
                ZoomMouseMovePt = ZoomMouseDownPt
            End If
    
            MouseStatus = 1
            MouseMovePt = pt
    
            Picturebox1.Invalidate()
            Picturebox1.Focus()
        End Sub
    
        Private Sub Picturebox1_MouseMove(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseMove
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If MouseStatus = 1 Then
    
                If ZoomCb.Checked Then
                    ZoomMouseMovePt = pt
                    Picturebox1.Invalidate()
    
                End If
            End If
    
        End Sub
    
        Private Sub Picturebox1_MouseUp(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseUp
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If ZoomCb.Checked Then
                ZoomMouseMovePt = pt
                ZoomCb.Checked = False
    
                'set scale based on zoom rect
                Select Case MouseStatus
                    Case 1
                        ZoomMouseMovePt = pt
                        ZoomCb.Checked = False
    
                        'set scale based on zoom rect width
                        Dim dx As Integer = ZoomMouseMovePt.X - ZoomMouseDownPt.X
                        Dim dy As Integer = ZoomMouseMovePt.Y - ZoomMouseDownPt.Y
                        ScaleWidth = dx
    
                        'set origin
                        Corner.X = ZoomMouseDownPt.X
                        Corner.Y = ZoomMouseDownPt.Y + dy
    
                        'update yoffset etc
                        Form2_Resize(0, Nothing)
    
                End Select
            End If
    
            MouseStatus = 0
            Picturebox1.Invalidate()
        End Sub
    
        Private Sub Picturebox1_MouseWheel(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseWheel
            ScaleRatio += Math.Sign(e.Delta) * 0.1
            If ScaleRatio < 0.000001 Then ScaleRatio = 0.000001
            If ScaleRatio > 10000 Then ScaleRatio = 10000
    
            Picturebox1.Invalidate()
        End Sub
    
        Private Sub Picturebox1_MouseEnter(sender As Object, e As EventArgs) Handles Picturebox1.MouseEnter
            Picturebox1.Focus()
        End Sub
    
        Private Sub Picturebox1_Paint(sender As Object, e As PaintEventArgs) Handles Picturebox1.Paint
            With e.Graphics
    
                .Clear(Color.White)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
                Dim sf As Single = CSng(Picturebox1.ClientSize.Width / (ScaleRatio * ScaleWidth))
                .ScaleTransform(sf, sf)
                .TranslateTransform(-Corner.X, Corner.Y)
    
                DrawGrid(e.Graphics)
    
                'draw x just for zooming example objects
                .DrawLine(Pens.Green, 40000, Yoffset - 20000, 60000, Yoffset - 40000)
                .DrawLine(Pens.Green, 40000, Yoffset - 40000, 60000, Yoffset - 20000)
    
                If MouseStatus = 1 Then
                    If ZoomCb.Checked Then
    
                        'convert for positive yaxis and rectf
                        Dim rect As New Rectangle(ZoomMouseDownPt.X, Yoffset - ZoomMouseDownPt.Y,
                                              ZoomMouseMovePt.X - ZoomMouseDownPt.X,
                                              ZoomMouseDownPt.Y - ZoomMouseMovePt.Y)
    
                        .DrawRectangle(Pens.Red, rect)
                    Else
                        'draw other tracers if active
    
                    End If
                End If
            End With
        End Sub
    
        Private Sub DrawGrid(g As Graphics)
            'setup drawing corners etc
            Dim x1 As Single = RoundToIncrement(Corner.X - GridStep, CInt(GridStep))
            Dim y1 As Single = RoundToIncrement(Corner.Y - GridStep, CInt(GridStep))
            Dim sw As Single = CSng(ScaleRatio * ScaleWidth + (2 * GridStep))
            Dim pxlSize As Single = ScaleRatio * ScaleWidth / Picturebox1.ClientSize.Width
    
            With g
                Using pg As New Pen(Color.DarkGray, CSng(pxlSize / 6)),
                        pgrn As New Pen(Color.Green, CSng(pxlSize / 6)),
                        f As New Font("arial", CSng(11 * pxlSize)),
                        br As New SolidBrush(Color.DimGray)
    
                    'draw origin
                    .DrawLine(pgrn, -pxlSize, Yoffset, pxlSize, Yoffset)
                    .DrawLine(pgrn, 0, Yoffset - (-pxlSize), 0, Yoffset - pxlSize)
    
                    'x axis grid
                    For x As Single = x1 To CSng(x1 + sw) Step GridStep
                        .DrawLine(pg, x, Yoffset - y1, x, Yoffset - CSng(y1 + sw))
                        .DrawString(x.ToString, f, br, x, Yoffset - (Corner.Y + (20 * pxlSize)))
                    Next
    
                    For y As Single = y1 To CSng(y1 + sw) Step GridStep
                        .DrawLine(pg, x1, Yoffset - y, CSng(x1 + sw), Yoffset - y)
                        .DrawString(y.ToString, f, br, Corner.X, Yoffset - y)
                    Next
                End Using
            End With
        End Sub
    
        Private Function RoundToIncrement(theValue As Double, roundIncrement As Integer) As Integer
            'ie value = 1433 roundinc = 100 returns 1400
            Return CInt((CDbl(theValue) + (0.5 * roundIncrement)) / roundIncrement) * roundIncrement
        End Function
    
        Private Function GetScalePtFromClientPt(pt As PointF) As PointF
            'convert screen pixels to scale drawing coords
            Dim sf As Double = Picturebox1.ClientSize.Width / (ScaleWidth * ScaleRatio)
            Return New PointF(CSng(Corner.X + (pt.X / sf)), CSng(Corner.Y + (Yoffset - pt.Y / sf)))
        End Function
    End Class


    Hi Mr. Tommy,

    I just try to run your code in new Form from half an hour.

    I did not understand the code yet.

    But I noticed the following:

    1- The zoom window is slightly down from the chosen red rectangle, please advice.

    2- I did not find any tool or mouse click to back the view to its original state (before zoom), can you advice how to back the zoom to its original state before zoom.

    3- What is mouse enter. i.e. its right click or left click?

    Kind Regards,


    Hany Metry

    Tuesday, November 19, 2019 3:15 AM
  • "Is it possible to make left button click instead of left button double click?"

    Yes.

    Hi,

    I have a check box at nearly top left corner of picture box and I avoid to use left button click to avoid when I check box = true, start Xini and Yini = 0,0



    Hany Metry


    Hany,

    Yes your questions are valid sort of. You just have to look and learn it now.

    It is easier to show the code than to describe it in written English.

    Study the order of the mouse events here:

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/mouse-events-in-windows-forms

    Recall I have mentioned that sometimes it will be easier to enter coordinates by text than using a mouse. It depends. What are you defining? The structure section shape like an I beam? Even a rectangle can be hard to define exactly with the mouse. If the size is 11.72 x 43.33 that is hard to click with a mouse. It depends.

    So you may decide you dont want to use the mouse for some things??? Coordinates might easier for some tasks.

    Or not.

    You may need to do it both ways and see what seems best.

    This example only works for a zoom rectangle drawn downward from left to right.

    The example is mostly the same as the previous one I posted with line tools.

    The example makes the controls just cut and paste the code into an empty form. Change the form name as required.

    Note how by setting ScaleWidth and Corner point you set the view window. Same a previous examples I show. But in the other examples we did not change the view.

    'mouse zoom in rectangle fence v1 with positive y axis, large values
    
    Public Class Form5
        Private WithEvents Picturebox1 As New PictureBox With {.Parent = Me, .BackColor = Color.White}
        Private WithEvents ZoomCb As New CheckBox With {.Parent = Me, .FlatStyle = FlatStyle.Flat,
            .Location = New Point(260, 10), .Size = New Size(60, 24), .Text = "Zoom", .Appearance = Appearance.Button,
            .BackColor = Color.AntiqueWhite, .TextAlign = ContentAlignment.MiddleCenter, .Name = "Zoom"}
    
        Private ScaleWidth As Single = 100000
        Private ScaleRatio As Single = 1
        Private Yoffset As Single
        Private Corner As New Point(0, 0)
        Private GridStep As Single = ScaleWidth / 5
        Private SnapStep As Single = GridStep
        Private MouseDownPt, MouseMovePt, ZoomMouseDownPt, ZoomMouseMovePt As PointF
        Private MouseStatus As Integer
        Private ToolCbCancel As Boolean = False
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(400, 300)
            DoubleBuffered = True
            Text = "Zoom Rectangle Fence"
            BackColor = Color.BurlyWood
            ZoomCb.FlatAppearance.CheckedBackColor = Color.LightSalmon
    
            'draw initial view
            Form2_Resize(0, Nothing)
            Picturebox1.Focus()
        End Sub
    
        Private Sub Form2_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Dim border As Integer = 20
            Picturebox1.Location = New Point(border, 50)
            Picturebox1.Size = New Size(ClientSize.Width - (2 * border), ClientSize.Height - (50 + border))
            Yoffset = ScaleWidth * (Picturebox1.ClientSize.Height / Picturebox1.ClientSize.Width)
            Picturebox1.Invalidate()
    
        End Sub
    
        Private Sub Picturebox1_MouseDown(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseDown
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If ZoomCb.Checked Then
                'keep zoom independant of drawing tools to allow nesting of zoom with tools.
                ZoomMouseDownPt = pt
                ZoomMouseMovePt = ZoomMouseDownPt
            End If
    
            MouseStatus = 1
            MouseMovePt = pt
    
            Picturebox1.Invalidate()
            Picturebox1.Focus()
        End Sub
    
        Private Sub Picturebox1_MouseMove(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseMove
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If MouseStatus = 1 Then
    
                If ZoomCb.Checked Then
                    ZoomMouseMovePt = pt
                    Picturebox1.Invalidate()
    
                End If
            End If
    
        End Sub
    
        Private Sub Picturebox1_MouseUp(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseUp
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If ZoomCb.Checked Then
                ZoomMouseMovePt = pt
                ZoomCb.Checked = False
    
                'set scale based on zoom rect
                Select Case MouseStatus
                    Case 1
                        ZoomMouseMovePt = pt
                        ZoomCb.Checked = False
    
                        'set scale based on zoom rect width
                        Dim dx As Integer = ZoomMouseMovePt.X - ZoomMouseDownPt.X
                        Dim dy As Integer = ZoomMouseMovePt.Y - ZoomMouseDownPt.Y
                        ScaleWidth = dx
    
                        'set origin
                        Corner.X = ZoomMouseDownPt.X
                        Corner.Y = ZoomMouseDownPt.Y + dy
    
                        'update yoffset etc
                        Form2_Resize(0, Nothing)
    
                End Select
            End If
    
            MouseStatus = 0
            Picturebox1.Invalidate()
        End Sub
    
        Private Sub Picturebox1_MouseWheel(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseWheel
            ScaleRatio += Math.Sign(e.Delta) * 0.1
            If ScaleRatio < 0.000001 Then ScaleRatio = 0.000001
            If ScaleRatio > 10000 Then ScaleRatio = 10000
    
            Picturebox1.Invalidate()
        End Sub
    
        Private Sub Picturebox1_MouseEnter(sender As Object, e As EventArgs) Handles Picturebox1.MouseEnter
            Picturebox1.Focus()
        End Sub
    
        Private Sub Picturebox1_Paint(sender As Object, e As PaintEventArgs) Handles Picturebox1.Paint
            With e.Graphics
    
                .Clear(Color.White)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
                Dim sf As Single = CSng(Picturebox1.ClientSize.Width / (ScaleRatio * ScaleWidth))
                .ScaleTransform(sf, sf)
                .TranslateTransform(-Corner.X, Corner.Y)
    
                DrawGrid(e.Graphics)
    
                'draw x just for zooming example objects
                .DrawLine(Pens.Green, 40000, Yoffset - 20000, 60000, Yoffset - 40000)
                .DrawLine(Pens.Green, 40000, Yoffset - 40000, 60000, Yoffset - 20000)
    
                If MouseStatus = 1 Then
                    If ZoomCb.Checked Then
    
                        'convert for positive yaxis and rectf
                        Dim rect As New Rectangle(ZoomMouseDownPt.X, Yoffset - ZoomMouseDownPt.Y,
                                              ZoomMouseMovePt.X - ZoomMouseDownPt.X,
                                              ZoomMouseDownPt.Y - ZoomMouseMovePt.Y)
    
                        .DrawRectangle(Pens.Red, rect)
                    Else
                        'draw other tracers if active
    
                    End If
                End If
            End With
        End Sub
    
        Private Sub DrawGrid(g As Graphics)
            'setup drawing corners etc
            Dim x1 As Single = RoundToIncrement(Corner.X - GridStep, CInt(GridStep))
            Dim y1 As Single = RoundToIncrement(Corner.Y - GridStep, CInt(GridStep))
            Dim sw As Single = CSng(ScaleRatio * ScaleWidth + (2 * GridStep))
            Dim pxlSize As Single = ScaleRatio * ScaleWidth / Picturebox1.ClientSize.Width
    
            With g
                Using pg As New Pen(Color.DarkGray, CSng(pxlSize / 6)),
                        pgrn As New Pen(Color.Green, CSng(pxlSize / 6)),
                        f As New Font("arial", CSng(11 * pxlSize)),
                        br As New SolidBrush(Color.DimGray)
    
                    'draw origin
                    .DrawLine(pgrn, -pxlSize, Yoffset, pxlSize, Yoffset)
                    .DrawLine(pgrn, 0, Yoffset - (-pxlSize), 0, Yoffset - pxlSize)
    
                    'x axis grid
                    For x As Single = x1 To CSng(x1 + sw) Step GridStep
                        .DrawLine(pg, x, Yoffset - y1, x, Yoffset - CSng(y1 + sw))
                        .DrawString(x.ToString, f, br, x, Yoffset - (Corner.Y + (20 * pxlSize)))
                    Next
    
                    For y As Single = y1 To CSng(y1 + sw) Step GridStep
                        .DrawLine(pg, x1, Yoffset - y, CSng(x1 + sw), Yoffset - y)
                        .DrawString(y.ToString, f, br, Corner.X, Yoffset - y)
                    Next
                End Using
            End With
        End Sub
    
        Private Function RoundToIncrement(theValue As Double, roundIncrement As Integer) As Integer
            'ie value = 1433 roundinc = 100 returns 1400
            Return CInt((CDbl(theValue) + (0.5 * roundIncrement)) / roundIncrement) * roundIncrement
        End Function
    
        Private Function GetScalePtFromClientPt(pt As PointF) As PointF
            'convert screen pixels to scale drawing coords
            Dim sf As Double = Picturebox1.ClientSize.Width / (ScaleWidth * ScaleRatio)
            Return New PointF(CSng(Corner.X + (pt.X / sf)), CSng(Corner.Y + (Yoffset - pt.Y / sf)))
        End Function
    End Class


    Hi Mr. Tommy,

    1- What is the function of the code below.

    Picturebox1.Focus()


    Kind Regards,


    Hany Metry



    • Edited by Hany Metry Tuesday, November 19, 2019 5:12 AM
    Tuesday, November 19, 2019 4:01 AM

  • Hany,

    Yes your questions are valid sort of. You just have to look and learn it now.

    It is easier to show the code than to describe it in written English.

    Study the order of the mouse events here:

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/mouse-events-in-windows-forms

    Recall I have mentioned that sometimes it will be easier to enter coordinates by text than using a mouse. It depends. What are you defining? The structure section shape like an I beam? Even a rectangle can be hard to define exactly with the mouse. If the size is 11.72 x 43.33 that is hard to click with a mouse. It depends.

    So you may decide you dont want to use the mouse for some things??? Coordinates might easier for some tasks.

    Or not.

    You may need to do it both ways and see what seems best.

    This example only works for a zoom rectangle drawn downward from left to right.

    The example is mostly the same as the previous one I posted with line tools.

    The example makes the controls just cut and paste the code into an empty form. Change the form name as required.

    Note how by setting ScaleWidth and Corner point you set the view window. Same a previous examples I show. But in the other examples we did not change the view.

    'mouse zoom in rectangle fence v1 with positive y axis, large values
    
    Public Class Form5
        Private WithEvents Picturebox1 As New PictureBox With {.Parent = Me, .BackColor = Color.White}
        Private WithEvents ZoomCb As New CheckBox With {.Parent = Me, .FlatStyle = FlatStyle.Flat,
            .Location = New Point(260, 10), .Size = New Size(60, 24), .Text = "Zoom", .Appearance = Appearance.Button,
            .BackColor = Color.AntiqueWhite, .TextAlign = ContentAlignment.MiddleCenter, .Name = "Zoom"}
    
        Private ScaleWidth As Single = 100000
        Private ScaleRatio As Single = 1
        Private Yoffset As Single
        Private Corner As New Point(0, 0)
        Private GridStep As Single = ScaleWidth / 5
        Private SnapStep As Single = GridStep
        Private MouseDownPt, MouseMovePt, ZoomMouseDownPt, ZoomMouseMovePt As PointF
        Private MouseStatus As Integer
        Private ToolCbCancel As Boolean = False
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(400, 300)
            DoubleBuffered = True
            Text = "Zoom Rectangle Fence"
            BackColor = Color.BurlyWood
            ZoomCb.FlatAppearance.CheckedBackColor = Color.LightSalmon
    
            'draw initial view
            Form2_Resize(0, Nothing)
            Picturebox1.Focus()
        End Sub
    
        Private Sub Form2_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Dim border As Integer = 20
            Picturebox1.Location = New Point(border, 50)
            Picturebox1.Size = New Size(ClientSize.Width - (2 * border), ClientSize.Height - (50 + border))
            Yoffset = ScaleWidth * (Picturebox1.ClientSize.Height / Picturebox1.ClientSize.Width)
            Picturebox1.Invalidate()
    
        End Sub
    
        Private Sub Picturebox1_MouseDown(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseDown
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If ZoomCb.Checked Then
                'keep zoom independant of drawing tools to allow nesting of zoom with tools.
                ZoomMouseDownPt = pt
                ZoomMouseMovePt = ZoomMouseDownPt
            End If
    
            MouseStatus = 1
            MouseMovePt = pt
    
            Picturebox1.Invalidate()
            Picturebox1.Focus()
        End Sub
    
        Private Sub Picturebox1_MouseMove(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseMove
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If MouseStatus = 1 Then
    
                If ZoomCb.Checked Then
                    ZoomMouseMovePt = pt
                    Picturebox1.Invalidate()
    
                End If
            End If
    
        End Sub
    
        Private Sub Picturebox1_MouseUp(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseUp
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
    
            If ZoomCb.Checked Then
                ZoomMouseMovePt = pt
                ZoomCb.Checked = False
    
                'set scale based on zoom rect
                Select Case MouseStatus
                    Case 1
                        ZoomMouseMovePt = pt
                        ZoomCb.Checked = False
    
                        'set scale based on zoom rect width
                        Dim dx As Integer = ZoomMouseMovePt.X - ZoomMouseDownPt.X
                        Dim dy As Integer = ZoomMouseMovePt.Y - ZoomMouseDownPt.Y
                        ScaleWidth = dx
    
                        'set origin
                        Corner.X = ZoomMouseDownPt.X
                        Corner.Y = ZoomMouseDownPt.Y + dy
    
                        'update yoffset etc
                        Form2_Resize(0, Nothing)
    
                End Select
            End If
    
            MouseStatus = 0
            Picturebox1.Invalidate()
        End Sub
    
        Private Sub Picturebox1_MouseWheel(sender As Object, e As MouseEventArgs) Handles Picturebox1.MouseWheel
            ScaleRatio += Math.Sign(e.Delta) * 0.1
            If ScaleRatio < 0.000001 Then ScaleRatio = 0.000001
            If ScaleRatio > 10000 Then ScaleRatio = 10000
    
            Picturebox1.Invalidate()
        End Sub
    
        Private Sub Picturebox1_MouseEnter(sender As Object, e As EventArgs) Handles Picturebox1.MouseEnter
            Picturebox1.Focus()
        End Sub
    
        Private Sub Picturebox1_Paint(sender As Object, e As PaintEventArgs) Handles Picturebox1.Paint
            With e.Graphics
    
                .Clear(Color.White)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
                Dim sf As Single = CSng(Picturebox1.ClientSize.Width / (ScaleRatio * ScaleWidth))
                .ScaleTransform(sf, sf)
                .TranslateTransform(-Corner.X, Corner.Y)
    
                DrawGrid(e.Graphics)
    
                'draw x just for zooming example objects
                .DrawLine(Pens.Green, 40000, Yoffset - 20000, 60000, Yoffset - 40000)
                .DrawLine(Pens.Green, 40000, Yoffset - 40000, 60000, Yoffset - 20000)
    
                If MouseStatus = 1 Then
                    If ZoomCb.Checked Then
    
                        'convert for positive yaxis and rectf
                        Dim rect As New Rectangle(ZoomMouseDownPt.X, Yoffset - ZoomMouseDownPt.Y,
                                              ZoomMouseMovePt.X - ZoomMouseDownPt.X,
                                              ZoomMouseDownPt.Y - ZoomMouseMovePt.Y)
    
                        .DrawRectangle(Pens.Red, rect)
                    Else
                        'draw other tracers if active
    
                    End If
                End If
            End With
        End Sub
    
        Private Sub DrawGrid(g As Graphics)
            'setup drawing corners etc
            Dim x1 As Single = RoundToIncrement(Corner.X - GridStep, CInt(GridStep))
            Dim y1 As Single = RoundToIncrement(Corner.Y - GridStep, CInt(GridStep))
            Dim sw As Single = CSng(ScaleRatio * ScaleWidth + (2 * GridStep))
            Dim pxlSize As Single = ScaleRatio * ScaleWidth / Picturebox1.ClientSize.Width
    
            With g
                Using pg As New Pen(Color.DarkGray, CSng(pxlSize / 6)),
                        pgrn As New Pen(Color.Green, CSng(pxlSize / 6)),
                        f As New Font("arial", CSng(11 * pxlSize)),
                        br As New SolidBrush(Color.DimGray)
    
                    'draw origin
                    .DrawLine(pgrn, -pxlSize, Yoffset, pxlSize, Yoffset)
                    .DrawLine(pgrn, 0, Yoffset - (-pxlSize), 0, Yoffset - pxlSize)
    
                    'x axis grid
                    For x As Single = x1 To CSng(x1 + sw) Step GridStep
                        .DrawLine(pg, x, Yoffset - y1, x, Yoffset - CSng(y1 + sw))
                        .DrawString(x.ToString, f, br, x, Yoffset - (Corner.Y + (20 * pxlSize)))
                    Next
    
                    For y As Single = y1 To CSng(y1 + sw) Step GridStep
                        .DrawLine(pg, x1, Yoffset - y, CSng(x1 + sw), Yoffset - y)
                        .DrawString(y.ToString, f, br, Corner.X, Yoffset - y)
                    Next
                End Using
            End With
        End Sub
    
        Private Function RoundToIncrement(theValue As Double, roundIncrement As Integer) As Integer
            'ie value = 1433 roundinc = 100 returns 1400
            Return CInt((CDbl(theValue) + (0.5 * roundIncrement)) / roundIncrement) * roundIncrement
        End Function
    
        Private Function GetScalePtFromClientPt(pt As PointF) As PointF
            'convert screen pixels to scale drawing coords
            Dim sf As Double = Picturebox1.ClientSize.Width / (ScaleWidth * ScaleRatio)
            Return New PointF(CSng(Corner.X + (pt.X / sf)), CSng(Corner.Y + (Yoffset - pt.Y / sf)))
        End Function
    End Class


    Hi Mr. Tommy,

    I just try to run your code in new Form from half an hour.

    I did not understand the code yet.

    But I noticed the following:

    1- The zoom window is slightly down from the chosen red rectangle, please advice.

    2- I did not find any tool or mouse click to back the view to its original state (before zoom), can you advice how to back the zoom to its original state before zoom.

    3- What is mouse enter. i.e. its right click or left click?

    Kind Regards,


    Hany Metry

    Hi Mr Tommy,

    I did not find any mistake in the following code but why the zoom is slightly up from the chosen rectangle

    Corner.X = ZoomMouseDownPt.X
                        Corner.Y = ZoomMouseDownPt.Y + dy

    Please advice.


    Hany Metry

    Tuesday, November 19, 2019 7:20 AM
  • Hi Mr Tommy,

    I did not find any mistake in the following code but why the zoom is slightly up from the chosen rectangle

    Corner.X = ZoomMouseDownPt.X
                        Corner.Y = ZoomMouseDownPt.Y + dy

    Please advice.


    Hany Metry

    Because in the example the view ScaleWidth is set to = Picturebox.width to zoom in.

    When you set the corner to the mousedownpt you will get the width of the view set within the width of the picturebox. However since the red zoom rectangle is not the same height aand width as the view window, the zoom selection rectangle will not fit over the view window perfectly.

    Best one can do is fit the zoom rectangle height to the view window height and then center the rect width into the view window rectangle or visa versa when the width larger than height.

    ie the two rectangles do not have the same width/height aspect ratio.

    So in this animation below of the same zoom as the animation in a post above, you can see the red zoom rect is a square and the view window is a rectangle wider than height so some of the red rectangle height is cut off in the wide view window.

    One and mess with how to center the two views for a long time.

    You can add a button that says zoom out and then set the view window back to the original size. Not hard to do at all.


    Tuesday, November 19, 2019 12:05 PM
  • Hi Mr Tommy,

    I did not find any mistake in the following code but why the zoom is slightly up from the chosen rectangle

    Corner.X = ZoomMouseDownPt.X
                        Corner.Y = ZoomMouseDownPt.Y + dy

    Please advice.


    Hany Metry

    Because in the example the view ScaleWidth is set to = Picturebox.width to zoom in.

    When you set the corner to the mousedownpt you will get the width of the view set within the width of the picturebox. However since the red zoom rectangle is not the same height aand width as the view window, the zoom selection rectangle will not fit over the view window perfectly.

    Best one can do is fit the zoom rectangle height to the view window height and then center the rect width into the view window rectangle or visa versa when the width larger than height.

    ie the two rectangles do not have the same width/height aspect ratio.

    So in this animation below of the same zoom as the animation in a post above, you can see the red zoom rect is a square and the view window is a rectangle wider than height so some of the red rectangle height is cut off in the wide view window.

    One and mess with how to center the two views for a long time.

    You can add a button that says zoom out and then set the view window back to the original size. Not hard to do at all.


    Hi Mr Tommy,

    If I dont draw by this picture box so I don't need Yoffset and the drawing shall be from left top corner, what the changes in the below code.

    .ScaleTransform(sf, sf)
                .TranslateTransform(-Corner.X, Corner.Y)

    I see negative value of corner X, why?

    Kind Regards,


    Hany Metry

    Tuesday, November 19, 2019 1:34 PM
  • Hany,

    "so I don't need Yoffset and the drawing shall be from left top corner, what the changes in the below code."


    Add a negative to cornery in the translate transform statement. In other words use yoffset = 0.


    I see negative value of corner X, why?


    Because you want to move the origin to the left. That is what the TranslateTransform does. It simply moves the origin of the view window coordinate system. ScaleTransform enlarges or reduces the scale of the window.

    You should try things and see. Change it to negative or remove the negative. Experiment. Put in some values like +- 10000 and see what happens to the view.

    You should practice with small one form examples like I show so you can learn just those transforms and zooming and etc. If you learn simple examples you can visualize how it will work in your big project and you will understand it better. Then just add things one at a time to your big project, run it , debug it, repeat.

    Tuesday, November 19, 2019 2:02 PM
  • Hany,

    "so I don't need Yoffset and the drawing shall be from left top corner, what the changes in the below code."


    Add a negative to cornery in the translate transform statement. In other words use yoffset = 0.


    I see negative value of corner X, why?


    Because you want to move the origin to the left. That is what the TranslateTransform does. It simply moves the origin of the view window coordinate system. ScaleTransform enlarges or reduces the scale of the window.

    You should try things and see. Change it to negative or remove the negative. Experiment. Put in some values like +- 10000 and see what happens to the view.

    You should practice with small one form examples like I show so you can learn just those transforms and zooming and etc. If you learn simple examples you can visualize how it will work in your big project and you will understand it better. Then just add things one at a time to your big project, run it , debug it, repeat.

    Hi Mr Tommy,

    What are my mistake in the below code that I draw relative to picturebox height and picture box width to draw relative to size of picture box and relative to size of drawing.

    I dont need Yoffset 

    Just quick look

    Dim sf As Single = CSng(PictureBox2.ClientSize.Width / (ScaleRatio * ScaleWidth))
            e.Graphics.ScaleTransform(sf, sf)
            e.Graphics.TranslateTransform(-Corner.X, -Corner.Y)
            If (Ymax - Ymin) / (Xmax - Xmin) <= Convert.ToDouble(PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width) Then
                For vv = 0 To Numcoor - 1
                    Xcoor11(vv) = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + ((Math.Abs(Xmin) + Xcoor(vv)) / Convert.ToDouble(b)) * 0.85 * PictureBox2.ClientSize.Width)
                    Ycoor11(vv) = CInt((PictureBox2.ClientSize.Height * 0.44) / sf - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - Ycoor(vv)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                Next
                X3a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b) - 0.085 * (PictureBox2.ClientSize.Width / sf))
                X4a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b) + 0.085 * (PictureBox2.ClientSize.Width / sf))
                Y3a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                Y4a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax + 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                Y5a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                X5a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b))
            Else
                For vv = 0 To Numcoor - 1
                    Xcoor11(vv) = CInt(0.5 * (PictureBox2.ClientSize.Width / sf) + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + Xcoor(vv)) / Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf))
                    Ycoor11(vv) = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - Ycoor(vv)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                Next
                X3a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) - 0.1 * Convert.ToDouble(b)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf)
                X4a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + 0.1 * Convert.ToDouble(b)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf)
                Y3a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                Y4a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax + 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                X5a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin)) / Convert.ToDouble(t)) * (0.85 * PictureBox2.ClientSize.Height / sf))
                Y5a = CInt((PictureBox2.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax) * 0.85 * (PictureBox2.Height / sf) / Convert.ToDouble(t))
            End If
            Dim points1(Numcoor - 1) As Point
            For vv = 0 To points1.Length - 1
                points1(vv).X = CInt(Xcoor11(vv))
                points1(vv).Y = CInt(Ycoor11(vv))
            Next
            'e.Graphics.FillPolygon(mbrush1, points1)
            e.Graphics.DrawPolygon(mpen3, points1)
            e.Graphics.DrawLine(mpen, X3a, Y5a, X4a, Y5a)
            e.Graphics.DrawLine(mpen, X5a, Y3a, X5a, Y4a)
            Dim DrawFont9 As New Font("Verdana", CInt(10 / sf), FontStyle.Regular)
            Dim DrawFont6 As New Font("Verdana", CInt(6 / sf), FontStyle.Regular)
            e.Graphics.DrawString(Str3, DrawFont9, Brush2, CInt(X4a + 5), CInt(Y5a - 8))
            e.Graphics.DrawString(Str4, DrawFont9, Brush2, CInt(X5a - 6), CInt(Y3a - 18))
            Dim Str As String = CInt(b) & " x " & CInt(t) & " mm"
            Dim Brush As New SolidBrush(Color.Black)
            Dim textSize72 As SizeF
            textSize72 = G.MeasureString(Str, DrawFont9)
            XQQ17 = CInt((PictureBox2.ClientSize.Width / sf - CInt(textSize72.Width)) / 2)
            e.Graphics.DrawString(Str, DrawFont9, Brush, XQQ17, CInt(0.87 * PictureBox2.ClientSize.Height / sf))

    And if I want to draw relative zoom rectangle size for the zoom what I can do by simple code.


    Hany Metry


    • Edited by Hany Metry Tuesday, November 19, 2019 2:33 PM remove Yoffset from the code
    Tuesday, November 19, 2019 2:22 PM
  • Hany,

    "so I don't need Yoffset and the drawing shall be from left top corner, what the changes in the below code."


    Add a negative to cornery in the translate transform statement. In other words use yoffset = 0.


    I see negative value of corner X, why?


    Because you want to move the origin to the left. That is what the TranslateTransform does. It simply moves the origin of the view window coordinate system. ScaleTransform enlarges or reduces the scale of the window.

    You should try things and see. Change it to negative or remove the negative. Experiment. Put in some values like +- 10000 and see what happens to the view.

    You should practice with small one form examples like I show so you can learn just those transforms and zooming and etc. If you learn simple examples you can visualize how it will work in your big project and you will understand it better. Then just add things one at a time to your big project, run it , debug it, repeat.


    Private ScaleWidth As Single = 100000
        Private ScaleRatio As Single = 1
        Private Yoffset As Single
        Private Corner As New Point(0, 0)
        Private GridStep As Single = ScaleWidth / 5
        Private MouseDownPt, MouseMovePt, ZoomMouseDownPt, ZoomMouseMovePt As PointF
        Private MouseStatus As Integer
        Private ToolCbCancel As Boolean = False

    Private Sub PictureBox2_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox2.Paint Dim G As Graphics = e.Graphics Dim q As Integer Dim ABco(2000) As ListViewItem NumBarcoor = ListView1.Items.Count If NumBarcoor >= 1 Then For q = 0 To NumBarcoor - 1 ABco(q) = ListView1.Items(q) Ser13(q) = CDbl(ListView1.Items(q).Text) Dia(q) = CDbl(ABco(q).SubItems(1).Text) coorx1(q) = CDbl(ABco(q).SubItems(2).Text) coory1(q) = CDbl(ABco(q).SubItems(3).Text) Next q End If Dim vv As Integer Dim YY As Integer b = Xmax - Xmin t = Ymax - Ymin Dim sf As Single = CSng(PictureBox2.ClientSize.Width / (ScaleRatio * ScaleWidth)) e.Graphics.ScaleTransform(sf, sf) e.Graphics.TranslateTransform(-Corner.X, -Corner.Y) If (Ymax - Ymin) / (Xmax - Xmin) <= Convert.ToDouble(PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width) Then For vv = 0 To Numcoor - 1 Xcoor11(vv) = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + ((Math.Abs(Xmin) + Xcoor(vv)) / Convert.ToDouble(b)) * 0.85 * PictureBox2.ClientSize.Width) Ycoor11(vv) = CInt((PictureBox2.ClientSize.Height * 0.44) / sf - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - Ycoor(vv)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b))) Next X3a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b) - 0.085 * (PictureBox2.ClientSize.Width / sf)) X4a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b) + 0.085 * (PictureBox2.ClientSize.Width / sf)) Y3a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b))) Y4a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax + 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b))) Y5a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b))) X5a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b)) Else For vv = 0 To Numcoor - 1 Xcoor11(vv) = CInt(0.5 * (PictureBox2.ClientSize.Width / sf) + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + Xcoor(vv)) / Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf)) Ycoor11(vv) = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - Ycoor(vv)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t)) Next X3a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) - 0.1 * Convert.ToDouble(b)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf) X4a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + 0.1 * Convert.ToDouble(b)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf) Y3a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t)) Y4a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax + 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t)) X5a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin)) / Convert.ToDouble(t)) * (0.85 * PictureBox2.ClientSize.Height / sf)) Y5a = CInt((PictureBox2.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax) * 0.85 * (PictureBox2.Height / sf) / Convert.ToDouble(t)) End If Dim points1(Numcoor - 1) As Point For vv = 0 To points1.Length - 1 points1(vv).X = CInt(Xcoor11(vv)) points1(vv).Y = CInt(Ycoor11(vv)) Next 'e.Graphics.FillPolygon(mbrush1, points1) e.Graphics.DrawPolygon(mpen3, points1) e.Graphics.DrawLine(mpen, X3a, Y5a, X4a, Y5a) e.Graphics.DrawLine(mpen, X5a, Y3a, X5a, Y4a) Dim DrawFont9 As New Font("Verdana", CInt(10 / sf), FontStyle.Regular) Dim DrawFont6 As New Font("Verdana", CInt(6 / sf), FontStyle.Regular) e.Graphics.DrawString(Str3, DrawFont9, Brush2, CInt(X4a + 5 / sf), CInt(Y5a - 8 / sf)) e.Graphics.DrawString(Str4, DrawFont9, Brush2, CInt(X5a - 6 / sf), CInt(Y3a - 18 / sf)) Dim Str As String = CInt(b) & " x " & CInt(t) & " mm" Dim Brush As New SolidBrush(Color.Black) Dim textSize72 As SizeF textSize72 = G.MeasureString(Str, DrawFont9) XQQ17 = CInt((PictureBox2.ClientSize.Width / sf - CInt(textSize72.Width)) / 2) e.Graphics.DrawString(Str, DrawFont9, Brush, XQQ17, CInt(0.87 * PictureBox2.ClientSize.Height / sf)) 40: If NumBarcoor >= 1 Then If (Ymax - Ymin) / (Xmax - Xmin) <= Convert.ToDouble(PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width) Then For YY = 0 To NumBarcoor - 1 Diaf1(YY) = Convert.ToInt32(Dia(YY) * (PictureBox2.ClientSize.Width / sf) * 0.85 / Convert.ToSingle(b)) coorXf1(YY) = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + ((Math.Abs(Xmin) + coorx1(YY)) / Convert.ToDouble(b)) * 0.85 * (PictureBox2.ClientSize.Width / sf)) - Diaf1(YY) / 2 coorYf1(YY) = CInt((PictureBox2.ClientSize.Height * 0.44) / sf - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - coory1(YY)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b))) - Diaf1(YY) / 2 e.Graphics.FillEllipse(mbrush, CInt(coorXf1(YY)), CInt(coorYf1(YY)), CInt(Diaf1(YY)), CInt(Diaf1(YY))) e.Graphics.DrawString(CStr(YY + 1), DrawFont6, Brush, CInt(coorXf1(YY) + Diaf1(YY) + 1), CInt(coorYf1(YY) - 8)) Next Else For YY = 0 To NumBarcoor - 1 Diaf1(YY) = Convert.ToInt32(Dia(YY) * (PictureBox2.ClientSize.Height / sf) * 0.85 / Convert.ToSingle(t)) coorXf1(YY) = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + coorx1(YY)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf) - Diaf1(YY) / 2 coorYf1(YY) = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - coory1(YY)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t)) - Diaf1(YY) / 2 e.Graphics.FillEllipse(mbrush, CInt(coorXf1(YY)), CInt(coorYf1(YY)), CInt(Diaf1(YY)), CInt(Diaf1(YY))) e.Graphics.DrawString(CStr(YY + 1), DrawFont6, Brush, CInt(coorXf1(YY) + Diaf1(YY) + 1), CInt(coorYf1(YY) - 8)) Next End If For q = 1 To NumBarcoor A(q) = pi * Dia(q - 1) ^ 2 / 4 Next q Ass1 = 0 For q = 1 To NumBarcoor Ass1 = Ass1 + A(q) Next Pr3 = Math.Round((Ass1 / Area1) * 100, 3) Pr = Pr3 Dim Str1 As String = Pr3 & "% reinf" Dim textSize19 As SizeF textSize19 = G.MeasureString(Str1, DrawFont9) XQQ11 = CInt((PictureBox2.ClientSize.Width / sf - textSize19.Width) / 2) e.Graphics.DrawString(Str1, DrawFont9, Brush, XQQ11, CInt(0.92 * PictureBox2.ClientSize.Height / sf)) If MouseStatus = 1 Then If ZoomCb.Checked Then 'convert for positive yaxis and rectf Dim rect As New Rectangle(CInt(ZoomMouseDownPt.X), CInt(ZoomMouseDownPt.Y), CInt(ZoomMouseMovePt.X) - CInt(ZoomMouseDownPt.X), CInt(ZoomMouseDownPt.Y) - CInt(ZoomMouseMovePt.Y)) e.Graphics.DrawRectangle(Pens.Red, rect) Else 'draw other tracers if active End If End If End If End Sub Private Sub Form32Irregular_byCoordinates_Load(sender As Object, e As EventArgs) Handles MyBase.Load Form32Irregular_byCoordinates_Resize(0, Nothing) PictureBox2.Focus() End Sub Private Sub Form32Irregular_byCoordinates_Resize(sender As Object, e As EventArgs) Handles Me.Resize Yoffset = Convert.ToSingle(ScaleWidth * (PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width)) PictureBox2.Invalidate() End Sub Private Sub Picturebox2_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseDown Dim pt As PointF = GetScalePtFromClientPt(e.Location) If ZoomCb.Checked Then 'keep zoom independant of drawing tools to allow nesting of zoom with tools. ZoomMouseDownPt = pt ZoomMouseMovePt = ZoomMouseDownPt End If MouseStatus = 1 MouseMovePt = pt PictureBox2.Invalidate() PictureBox2.Focus() End Sub Private Sub Picturebox2_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseMove Dim pt As PointF = GetScalePtFromClientPt(e.Location) If MouseStatus = 1 Then If ZoomCb.Checked Then ZoomMouseMovePt = pt PictureBox2.Invalidate() End If End If End Sub Private Sub Picturebox2_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseUp Dim pt As PointF = GetScalePtFromClientPt(e.Location) If ZoomCb.Checked Then ZoomMouseMovePt = pt ZoomCb.Checked = False 'set scale based on zoom rect Select Case MouseStatus Case 1 ZoomMouseMovePt = pt ZoomCb.Checked = False 'set scale based on zoom rect width Dim dx As Integer = CInt(ZoomMouseMovePt.X - ZoomMouseDownPt.X) Dim dy As Integer = CInt(ZoomMouseMovePt.Y - ZoomMouseDownPt.Y) ScaleWidth = dx 'set origin Corner.X = CInt(ZoomMouseDownPt.X) Corner.Y = CInt(ZoomMouseDownPt.Y) 'update yoffset etc Form32Irregular_byCoordinates_Resize(0, Nothing) End Select End If MouseStatus = 0 PictureBox2.Invalidate() End Sub Private Function GetScalePtFromClientPt(pt As PointF) As PointF 'convert screen pixels to scale drawing coords Dim sf As Double = PictureBox2.ClientSize.Width / (ScaleWidth * ScaleRatio) Return New PointF(CSng(Corner.X + (pt.X / sf)), CSng(Corner.Y + (pt.Y / sf))) End Function Private Sub Picturebox2_MouseWheel(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseWheel ScaleRatio += CSng(Math.Sign(e.Delta) * 0.1) If ScaleRatio < 0.000001 Then ScaleRatio = 0.000001 If ScaleRatio > 10000 Then ScaleRatio = 10000 PictureBox2.Invalidate() End Sub Private Sub Picturebox2_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox2.MouseEnter PictureBox2.Focus() End Sub

    Hi Mr Tommy,

    The above is my code which dont draw the red rectangle and dont zoom

    If you have a time just quick look.

    Kind Regards,


    Hany Metry


    • Edited by Hany Metry Tuesday, November 19, 2019 3:07 PM delete part
    Tuesday, November 19, 2019 2:42 PM
  • Hi Mr Tommy,

    The above is my code which dont draw the red rectangle and dont zoom

    If you have a time just quick look.

    Kind Regards,


    Hany Metry

    I dont understand what you are asking.

    You will need to debug your own code.

    Final Answer.

    Tuesday, November 19, 2019 2:56 PM
  • Hi Mr Tommy,

    The above is my code which dont draw the red rectangle and dont zoom

    If you have a time just quick look.

    Kind Regards,


    Hany Metry

    I dont understand what you are asking.

    You will need to debug your own code.

    Final Answer.

    Hi Mr tommy,

    When I just touch the picturebox2 by mouse, without input the second point by mouse the debug start and then the picture box is crossed by two large cross (hang) and I dont know what I will do?

    Please just quick look.

    It is same as your code but the draw of lines and polygon is relative to picture box 2 sizes.

    How I can change my code during zoom to draw relative to rectangle of zoom?

    Just give a last push.

    Kind Regards,


    Hany Metry

    Tuesday, November 19, 2019 3:24 PM
  • Hi Mr Tommy,

    The above is my code which dont draw the red rectangle and dont zoom

    If you have a time just quick look.

    Kind Regards,


    Hany Metry

    I dont understand what you are asking.

    You will need to debug your own code.

    Final Answer.

    Private ScaleWidth As Single = 100000
        Private ScaleRatio As Single = 1
        Private Yoffset As Single
        Private Corner As New Point(0, 0)
        Private GridStep As Single = ScaleWidth / 5
        Private MouseDownPt, MouseMovePt, ZoomMouseDownPt, ZoomMouseMovePt As PointF
        Private MouseStatus As Integer
        Private dx As Integer
        Private dy As Integer
        Private ToolCbCancel As Boolean = False
    Private Sub PictureBox2_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox2.Paint
            Dim G As Graphics = e.Graphics
            Dim q As Integer
            Dim ABco(2000) As ListViewItem
            NumBarcoor = ListView1.Items.Count
            If NumBarcoor >= 1 Then
                For q = 0 To NumBarcoor - 1
                    ABco(q) = ListView1.Items(q)
                    Ser13(q) = CDbl(ListView1.Items(q).Text)
                    Dia(q) = CDbl(ABco(q).SubItems(1).Text)
                    coorx1(q) = CDbl(ABco(q).SubItems(2).Text)
                    coory1(q) = CDbl(ABco(q).SubItems(3).Text)
                Next q
            End If
            Dim vv As Integer
            Dim YY As Integer
            b = Xmax - Xmin
            t = Ymax - Ymin
    
    Dim sf As Single = CSng(PictureBox2.ClientSize.Width / (ScaleRatio * ScaleWidth))
            e.Graphics.ScaleTransform(sf, sf)
            e.Graphics.TranslateTransform(-Corner.X, -Corner.Y)
            If MouseStatus = 3 Or MouseStatus = 1 Then
                If (Ymax - Ymin) / (Xmax - Xmin) <= Convert.ToDouble(PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width) Then
                    For vv = 0 To Numcoor - 1
                        Xcoor11(vv) = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + ((Math.Abs(Xmin) + Xcoor(vv)) / Convert.ToDouble(b)) * 0.85 * PictureBox2.ClientSize.Width)
                        Ycoor11(vv) = CInt((PictureBox2.ClientSize.Height * 0.44) / sf - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - Ycoor(vv)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    Next
                    X3a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b) - 0.085 * (PictureBox2.ClientSize.Width / sf))
                    X4a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b) + 0.085 * (PictureBox2.ClientSize.Width / sf))
                    Y3a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    Y4a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax + 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    Y5a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    X5a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b))
                Else
                    For vv = 0 To Numcoor - 1
                        Xcoor11(vv) = CInt(0.5 * (PictureBox2.ClientSize.Width / sf) + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + Xcoor(vv)) / Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf))
                        Ycoor11(vv) = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - Ycoor(vv)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                    Next
                    X3a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) - 0.1 * Convert.ToDouble(b)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf)
                    X4a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + 0.1 * Convert.ToDouble(b)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf)
                    Y3a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                    Y4a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax + 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                    X5a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin)) / Convert.ToDouble(t)) * (0.85 * PictureBox2.ClientSize.Height / sf))
                    Y5a = CInt((PictureBox2.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax) * 0.85 * (PictureBox2.Height / sf) / Convert.ToDouble(t))
                End If
                Dim points1(Numcoor - 1) As Point
                For vv = 0 To points1.Length - 1
                    points1(vv).X = CInt(Xcoor11(vv))
                    points1(vv).Y = CInt(Ycoor11(vv))
                Next
                'e.Graphics.FillPolygon(mbrush1, points1)
                e.Graphics.DrawPolygon(mpen3, points1)
                e.Graphics.DrawLine(mpen, X3a, Y5a, X4a, Y5a)
                e.Graphics.DrawLine(mpen, X5a, Y3a, X5a, Y4a)
                Dim DrawFont9 As New Font("Verdana", CInt(10 / sf), FontStyle.Regular)
                Dim DrawFont6 As New Font("Verdana", CInt(6 / sf), FontStyle.Regular)
                e.Graphics.DrawString(Str3, DrawFont9, Brush2, CInt(X4a + 5 / sf), CInt(Y5a - 8 / sf))
                e.Graphics.DrawString(Str4, DrawFont9, Brush2, CInt(X5a - 6 / sf), CInt(Y3a - 18 / sf))
                Dim Str As String = CInt(b) & " x " & CInt(t) & " mm"
                Dim Brush As New SolidBrush(Color.Black)
                Dim textSize72 As SizeF
                textSize72 = G.MeasureString(Str, DrawFont9)
                XQQ17 = CInt((PictureBox2.ClientSize.Width / sf - CInt(textSize72.Width)) / 2)
                e.Graphics.DrawString(Str, DrawFont9, Brush, XQQ17, CInt(0.87 * PictureBox2.ClientSize.Height / sf))
                If NumBarcoor >= 1 Then
                    If (Ymax - Ymin) / (Xmax - Xmin) <= Convert.ToDouble(PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width) Then
                        For YY = 0 To NumBarcoor - 1
                            Diaf1(YY) = Convert.ToInt32(Dia(YY) * (PictureBox2.ClientSize.Width / sf) * 0.85 / Convert.ToSingle(b))
                            coorXf1(YY) = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + ((Math.Abs(Xmin) + coorx1(YY)) / Convert.ToDouble(b)) * 0.85 * (PictureBox2.ClientSize.Width / sf)) - Diaf1(YY) / 2
                            coorYf1(YY) = CInt((PictureBox2.ClientSize.Height * 0.44) / sf - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - coory1(YY)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b))) - Diaf1(YY) / 2
                            e.Graphics.FillEllipse(mbrush, CInt(coorXf1(YY)), CInt(coorYf1(YY)), CInt(Diaf1(YY)), CInt(Diaf1(YY)))
                            e.Graphics.DrawString(CStr(YY + 1), DrawFont6, Brush, CInt(coorXf1(YY) + Diaf1(YY) + 1 / sf), CInt(coorYf1(YY) - 8 / sf))
                        Next
                    Else
                        For YY = 0 To NumBarcoor - 1
                            Diaf1(YY) = Convert.ToInt32(Dia(YY) * (PictureBox2.ClientSize.Height / sf) * 0.85 / Convert.ToSingle(t))
                            coorXf1(YY) = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + coorx1(YY)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf) - Diaf1(YY) / 2
                            coorYf1(YY) = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - coory1(YY)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t)) - Diaf1(YY) / 2
                            e.Graphics.FillEllipse(mbrush, CInt(coorXf1(YY)), CInt(coorYf1(YY)), CInt(Diaf1(YY)), CInt(Diaf1(YY)))
                            e.Graphics.DrawString(CStr(YY + 1), DrawFont6, Brush, CInt(coorXf1(YY) + Diaf1(YY) + 1 / sf), CInt(coorYf1(YY) - 8 / sf))
                        Next
                    End If
                    For q = 1 To NumBarcoor
                        A(q) = pi * Dia(q - 1) ^ 2 / 4
                    Next q
                    Ass1 = 0
                    For q = 1 To NumBarcoor
                        Ass1 = Ass1 + A(q)
                    Next
                    Pr3 = Math.Round((Ass1 / Area1) * 100, 3)
                    Pr = Pr3
                    Dim Str1 As String = Pr3 & "% reinf"
                    Dim textSize19 As SizeF
                    textSize19 = G.MeasureString(Str1, DrawFont9)
                    XQQ11 = CInt((PictureBox2.ClientSize.Width / sf - textSize19.Width) / 2)
                    e.Graphics.DrawString(Str1, DrawFont9, Brush, XQQ11, CInt(0.92 * PictureBox2.ClientSize.Height / sf))
                End If
            ElseIf MouseStatus = 0 Then
                If dy / dx <= Convert.ToDouble(PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width) Then
                    For vv = 0 To Numcoor - 1
                        Xcoor11(vv) = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + ((Math.Abs(Xmin) + Xcoor(vv)) / Convert.ToDouble(b)) * 0.85 * PictureBox2.ClientSize.Width)
                        Ycoor11(vv) = CInt((PictureBox2.ClientSize.Height * 0.44) / sf - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - Ycoor(vv)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    Next
                    X3a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b) - 0.085 * (PictureBox2.ClientSize.Width / sf))
                    X4a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b) + 0.085 * (PictureBox2.ClientSize.Width / sf))
                    Y3a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    Y4a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax + 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    Y5a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    X5a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b))
                Else
                    For vv = 0 To Numcoor - 1
                        Xcoor11(vv) = CInt(0.5 * (PictureBox2.ClientSize.Width / sf) + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + Xcoor(vv)) / Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf))
                        Ycoor11(vv) = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - Ycoor(vv)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                    Next
                    X3a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) - 0.1 * Convert.ToDouble(b)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf)
                    X4a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + 0.1 * Convert.ToDouble(b)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf)
                    Y3a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                    Y4a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax + 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                    X5a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin)) / Convert.ToDouble(t)) * (0.85 * PictureBox2.ClientSize.Height / sf))
                    Y5a = CInt((PictureBox2.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax) * 0.85 * (PictureBox2.Height / sf) / Convert.ToDouble(t))
                End If
                Dim points11(Numcoor - 1) As Point
                For vv = 0 To points11.Length - 1
                    points11(vv).X = CInt(Xcoor11(vv))
                    points11(vv).Y = CInt(Ycoor11(vv))
                Next
                'e.Graphics.FillPolygon(mbrush1, points1)
                e.Graphics.DrawPolygon(mpen3, points11)
                e.Graphics.DrawLine(mpen, X3a, Y5a, X4a, Y5a)
                e.Graphics.DrawLine(mpen, X5a, Y3a, X5a, Y4a)
                Dim DrawFont99 As New Font("Verdana", CInt(10 / sf), FontStyle.Regular)
                Dim DrawFont66 As New Font("Verdana", CInt(6 / sf), FontStyle.Regular)
                e.Graphics.DrawString(Str3, DrawFont99, Brush2, CInt(X4a + 5 / sf), CInt(Y5a - 8 / sf))
                e.Graphics.DrawString(Str4, DrawFont99, Brush2, CInt(X5a - 6 / sf), CInt(Y3a - 18 / sf))
                Dim Str66 As String = CInt(b) & " x " & CInt(t) & " mm"
                Dim Brush66 As New SolidBrush(Color.Black)
                Dim textSize726 As SizeF
                textSize726 = G.MeasureString(Str66, DrawFont99)
                XQQ17 = CInt((PictureBox2.ClientSize.Width / sf - CInt(textSize726.Width)) / 2)
                e.Graphics.DrawString(Str66, DrawFont99, Brush, XQQ17, CInt(0.87 * PictureBox2.ClientSize.Height / sf))
                If NumBarcoor >= 1 Then
                    If dy / dx <= Convert.ToDouble(PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width) Then
                        For YY = 0 To NumBarcoor - 1
                            Diaf1(YY) = Convert.ToInt32(Dia(YY) * (PictureBox2.ClientSize.Width / sf) * 0.85 / Convert.ToSingle(b))
                            coorXf1(YY) = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + ((Math.Abs(Xmin) + coorx1(YY)) / Convert.ToDouble(b)) * 0.85 * (PictureBox2.ClientSize.Width / sf)) - Diaf1(YY) / 2
                            coorYf1(YY) = CInt((PictureBox2.ClientSize.Height * 0.44) / sf - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - coory1(YY)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b))) - Diaf1(YY) / 2
                            e.Graphics.FillEllipse(mbrush, CInt(coorXf1(YY)), CInt(coorYf1(YY)), CInt(Diaf1(YY)), CInt(Diaf1(YY)))
                            e.Graphics.DrawString(CStr(YY + 1), DrawFont66, Brush, CInt(coorXf1(YY) + Diaf1(YY) + 1 / sf), CInt(coorYf1(YY) - 8 / sf))
                        Next
                    Else
                        For YY = 0 To NumBarcoor - 1
                            Diaf1(YY) = Convert.ToInt32(Dia(YY) * (PictureBox2.ClientSize.Height / sf) * 0.85 / Convert.ToSingle(t))
                            coorXf1(YY) = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + coorx1(YY)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf) - Diaf1(YY) / 2
                            coorYf1(YY) = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - coory1(YY)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t)) - Diaf1(YY) / 2
                            e.Graphics.FillEllipse(mbrush, CInt(coorXf1(YY)), CInt(coorYf1(YY)), CInt(Diaf1(YY)), CInt(Diaf1(YY)))
                            e.Graphics.DrawString(CStr(YY + 1), DrawFont66, Brush, CInt(coorXf1(YY) + Diaf1(YY) + 1 / sf), CInt(coorYf1(YY) - 8 / sf))
                        Next
                    End If
                    For q = 1 To NumBarcoor
                        A(q) = pi * Dia(q - 1) ^ 2 / 4
                    Next q
                    Ass1 = 0
                    For q = 1 To NumBarcoor
                        Ass1 = Ass1 + A(q)
                    Next
                    Pr3 = Math.Round((Ass1 / Area1) * 100, 3)
                    Pr = Pr3
                    Dim Str1 As String = Pr3 & "% reinf"
                    Dim textSize19 As SizeF
                    textSize19 = G.MeasureString(Str1, DrawFont99)
                    XQQ11 = CInt((PictureBox2.ClientSize.Width / sf - textSize19.Width) / 2)
                    e.Graphics.DrawString(Str1, DrawFont99, Brush, XQQ11, CInt(0.92 * PictureBox2.ClientSize.Height / sf))
                End If
            End If
            If MouseStatus = 1 Then
                If ZoomCb.Checked Then
                    'convert for positive yaxis and rectf
                    Dim rect As New Rectangle(CInt(ZoomMouseDownPt.X), CInt(ZoomMouseDownPt.Y), CInt(ZoomMouseMovePt.X) - CInt(ZoomMouseDownPt.X), CInt(ZoomMouseDownPt.Y) - CInt(ZoomMouseMovePt.Y))
                    e.Graphics.DrawRectangle(Pens.Red, rect)
                Else
                    'draw other tracers if active
                End If
            End If
        End Sub
        Private Sub Form32Irregular_byCoordinates_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Form32Irregular_byCoordinates_Resize(0, Nothing)
            PictureBox2.Focus()
            MouseStatus = 3
        End Sub
        Private Sub Form32Irregular_byCoordinates_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Yoffset = Convert.ToSingle(ScaleWidth * (PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width))
            PictureBox2.Invalidate()
        End Sub
        Private Sub Picturebox2_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseDown
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
            If ZoomCb.Checked Then
                'keep zoom independant of drawing tools to allow nesting of zoom with tools.
                ZoomMouseDownPt = pt
                ZoomMouseMovePt = ZoomMouseDownPt
            End If
            MouseStatus = 1
            MouseMovePt = pt
            PictureBox2.Invalidate()
            PictureBox2.Focus()
        End Sub
        Private Sub Picturebox2_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseMove
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
            If MouseStatus = 1 Then
                If ZoomCb.Checked Then
                    ZoomMouseMovePt = pt
                    PictureBox2.Invalidate()
                End If
            End If
        End Sub
        Private Sub Picturebox2_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseUp
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
            If ZoomCb.Checked Then
                ZoomMouseMovePt = pt
                ZoomCb.Checked = False
                'set scale based on zoom rect
                Select Case MouseStatus
                    Case 1
                        ZoomMouseMovePt = pt
                        ZoomCb.Checked = False
                        'set scale based on zoom rect width
                        dx = CInt(ZoomMouseMovePt.X - ZoomMouseDownPt.X)
                        dy = CInt(ZoomMouseMovePt.Y - ZoomMouseDownPt.Y)
                        ScaleWidth = dx
                        'set origin
                        Corner.X = CInt(ZoomMouseDownPt.X)
                        Corner.Y = CInt(ZoomMouseDownPt.Y)
                        'update yoffset etc
                        Form32Irregular_byCoordinates_Resize(0, Nothing)
                End Select
            End If
            MouseStatus = 0
            PictureBox2.Invalidate()
        End Sub
        Private Function GetScalePtFromClientPt(pt As PointF) As PointF
            'convert screen pixels to scale drawing coords
            Dim sf As Double = PictureBox2.ClientSize.Width / (ScaleWidth * ScaleRatio)
            Return New PointF(CSng(Corner.X + (pt.X / sf)), CSng(Corner.Y + (pt.Y / sf)))
        End Function
        Private Sub Picturebox2_MouseWheel(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseWheel
            ScaleRatio += CSng(Math.Sign(e.Delta) * 0.1)
            If ScaleRatio < 0.000001 Then ScaleRatio = 0.000001
            If ScaleRatio > 10000 Then ScaleRatio = 10000
            PictureBox2.Invalidate()
        End Sub
        Private Sub Picturebox2_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox2.MouseEnter
            PictureBox2.Focus()
        End Sub

    The above does not do zoom good.

    Please advice Mr Tommy

    Kind Regards,


    Hany Metry

    Tuesday, November 19, 2019 4:25 PM
  • Dear Mr Tommy,

    Please Just copy the below code to empty form contain picturebox2 and contain checkbox of name ZoomCb and provide me with your comment to path my problem

    Private Sub PictureBox2_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox2.Paint
    Private ScaleWidth As Single = 100000
        Private ScaleRatio As Single = 1
        Private Yoffset As Single
        Private Corner As New Point(0, 0)
        Private GridStep As Single = ScaleWidth / 5
        Private MouseDownPt, MouseMovePt, ZoomMouseDownPt, ZoomMouseMovePt As PointF
        Private MouseStatus As Integer
        Private dx As Integer
        Private dy As Integer
        Private ToolCbCancel As Boolean = False
    Xcoor(0)=200
    Xcoor(1)=200
    Xcoor(2)=-200
    Xcoor(3)=-200
    Ycoor(0)=-350
    Ycoor(1)=350
    Ycoor(2)=350
    Ycoor(3)=-350
    coorx1(0)=141
    coorx1(1)=141
    coorx1(2)=141
    coorx1(3)=141
    coorx1(4)=0
    coorx1(5)=-141
    coorx1(6)=-141
    coorx1(7)=-141
    coorx1(8)=-141
    coorx1(9)=-141
    coorx1(10)=0
    coorx1(11)=141
    coory1(0)=-145.5
    coory1(1)=0
    coory1(2)=145.5
    coory1(3)=291
    coory1(4)=291
    coory1(5)=291
    coory1(6)=145.5
    coory1(7)=0
    coory1(8)=-145.5
    coory1(9)=-291
    coory1(10)=-291
    coory1(11)=-291
    Xmax=200
    Xmin=-200
    Ymax=350
    Ymin=-350
    Numcoor=4
    Numbarcoor=12
    Dim vv As Integer
            Dim YY As Integer
            b = Xmax - Xmin
            t = Ymax - Ymin
            Dim sf As Single = CSng(PictureBox2.ClientSize.Width / (ScaleRatio * ScaleWidth))
            e.Graphics.ScaleTransform(sf, sf)
            e.Graphics.TranslateTransform(-Corner.X, -Corner.Y)
            If MouseStatus = 3 Or MouseStatus = 1 Then
                If (Ymax - Ymin) / (Xmax - Xmin) <= Convert.ToDouble(PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width) Then
                    For vv = 0 To Numcoor - 1
                        Xcoor11(vv) = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + ((Math.Abs(Xmin) + Xcoor(vv)) / Convert.ToDouble(b)) * 0.85 * PictureBox2.ClientSize.Width)
                        Ycoor11(vv) = CInt((PictureBox2.ClientSize.Height * 0.44) / sf - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - Ycoor(vv)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    Next
                    X3a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b) - 0.085 * (PictureBox2.ClientSize.Width / sf))
                    X4a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b) + 0.085 * (PictureBox2.ClientSize.Width / sf))
                    Y3a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    Y4a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax + 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    Y5a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    X5a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b))
                Else
                    For vv = 0 To Numcoor - 1
                        Xcoor11(vv) = CInt(0.5 * (PictureBox2.ClientSize.Width / sf) + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + Xcoor(vv)) / Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf))
                        Ycoor11(vv) = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - Ycoor(vv)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                    Next
                    X3a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) - 0.1 * Convert.ToDouble(b)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf)
                    X4a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + 0.1 * Convert.ToDouble(b)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf)
                    Y3a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                    Y4a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax + 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                    X5a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin)) / Convert.ToDouble(t)) * (0.85 * PictureBox2.ClientSize.Height / sf))
                    Y5a = CInt((PictureBox2.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax) * 0.85 * (PictureBox2.Height / sf) / Convert.ToDouble(t))
                End If
                Dim points1(Numcoor - 1) As Point
                For vv = 0 To points1.Length - 1
                    points1(vv).X = CInt(Xcoor11(vv))
                    points1(vv).Y = CInt(Ycoor11(vv))
                Next
                'e.Graphics.FillPolygon(mbrush1, points1)
                e.Graphics.DrawPolygon(mpen3, points1)
                e.Graphics.DrawLine(mpen, X3a, Y5a, X4a, Y5a)
                e.Graphics.DrawLine(mpen, X5a, Y3a, X5a, Y4a)
                Dim DrawFont9 As New Font("Verdana", CInt(10 / sf), FontStyle.Regular)
                Dim DrawFont6 As New Font("Verdana", CInt(6 / sf), FontStyle.Regular)
                e.Graphics.DrawString(Str3, DrawFont9, Brush2, CInt(X4a + 5 / sf), CInt(Y5a - 8 / sf))
                e.Graphics.DrawString(Str4, DrawFont9, Brush2, CInt(X5a - 6 / sf), CInt(Y3a - 18 / sf))
                Dim Str As String = CInt(b) & " x " & CInt(t) & " mm"
                Dim Brush As New SolidBrush(Color.Black)
                Dim textSize72 As SizeF
                textSize72 = G.MeasureString(Str, DrawFont9)
                XQQ17 = CInt((PictureBox2.ClientSize.Width / sf - CInt(textSize72.Width)) / 2)
                e.Graphics.DrawString(Str, DrawFont9, Brush, XQQ17, CInt(0.87 * PictureBox2.ClientSize.Height / sf))
                If NumBarcoor >= 1 Then
                    If (Ymax - Ymin) / (Xmax - Xmin) <= Convert.ToDouble(PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width) Then
                        For YY = 0 To NumBarcoor - 1
                            Diaf1(YY) = Convert.ToInt32(Dia(YY) * (PictureBox2.ClientSize.Width / sf) * 0.85 / Convert.ToSingle(b))
                            coorXf1(YY) = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + ((Math.Abs(Xmin) + coorx1(YY)) / Convert.ToDouble(b)) * 0.85 * (PictureBox2.ClientSize.Width / sf)) - Diaf1(YY) / 2
                            coorYf1(YY) = CInt((PictureBox2.ClientSize.Height * 0.44) / sf - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - coory1(YY)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b))) - Diaf1(YY) / 2
                            e.Graphics.FillEllipse(mbrush, CInt(coorXf1(YY)), CInt(coorYf1(YY)), CInt(Diaf1(YY)), CInt(Diaf1(YY)))
                            e.Graphics.DrawString(CStr(YY + 1), DrawFont6, Brush, CInt(coorXf1(YY) + Diaf1(YY) + 1 / sf), CInt(coorYf1(YY) - 8 / sf))
                        Next
                    Else
                        For YY = 0 To NumBarcoor - 1
                            Diaf1(YY) = Convert.ToInt32(Dia(YY) * (PictureBox2.ClientSize.Height / sf) * 0.85 / Convert.ToSingle(t))
                            coorXf1(YY) = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + coorx1(YY)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf) - Diaf1(YY) / 2
                            coorYf1(YY) = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - coory1(YY)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t)) - Diaf1(YY) / 2
                            e.Graphics.FillEllipse(mbrush, CInt(coorXf1(YY)), CInt(coorYf1(YY)), CInt(Diaf1(YY)), CInt(Diaf1(YY)))
                            e.Graphics.DrawString(CStr(YY + 1), DrawFont6, Brush, CInt(coorXf1(YY) + Diaf1(YY) + 1 / sf), CInt(coorYf1(YY) - 8 / sf))
                        Next
                    End If
                    For q = 1 To NumBarcoor
                        A(q) = pi * Dia(q - 1) ^ 2 / 4
                    Next q
                    Ass1 = 0
                    For q = 1 To NumBarcoor
                        Ass1 = Ass1 + A(q)
                    Next
                    Pr3 = Math.Round((Ass1 / Area1) * 100, 3)
                    Pr = Pr3
                    Dim Str1 As String = Pr3 & "% reinf"
                    Dim textSize19 As SizeF
                    textSize19 = G.MeasureString(Str1, DrawFont9)
                    XQQ11 = CInt((PictureBox2.ClientSize.Width / sf - textSize19.Width) / 2)
                    e.Graphics.DrawString(Str1, DrawFont9, Brush, XQQ11, CInt(0.92 * PictureBox2.ClientSize.Height / sf))
                End If
            ElseIf MouseStatus = 0 Then
                If dy / dx <= Convert.ToDouble(PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width) Then
                    For vv = 0 To Numcoor - 1
                        Xcoor11(vv) = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + ((Math.Abs(Xmin) + Xcoor(vv)) / Convert.ToDouble(b)) * 0.85 * PictureBox2.ClientSize.Width)
                        Ycoor11(vv) = CInt((PictureBox2.ClientSize.Height * 0.44) / sf - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - Ycoor(vv)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    Next
                    X3a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b) - 0.085 * (PictureBox2.ClientSize.Width / sf))
                    X4a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b) + 0.085 * (PictureBox2.ClientSize.Width / sf))
                    Y3a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    Y4a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax + 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    Y5a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b)))
                    X5a = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + Math.Abs(Xmin) * 0.85 * (PictureBox2.ClientSize.Width / sf) / Convert.ToDouble(b))
                Else
                    For vv = 0 To Numcoor - 1
                        Xcoor11(vv) = CInt(0.5 * (PictureBox2.ClientSize.Width / sf) + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + Xcoor(vv)) / Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf))
                        Ycoor11(vv) = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - Ycoor(vv)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                    Next
                    X3a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) - 0.1 * Convert.ToDouble(b)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf)
                    X4a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + 0.1 * Convert.ToDouble(b)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf)
                    Y3a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                    Y4a = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax + 0.1 * Convert.ToDouble(t)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t))
                    X5a = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin)) / Convert.ToDouble(t)) * (0.85 * PictureBox2.ClientSize.Height / sf))
                    Y5a = CInt((PictureBox2.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax) * 0.85 * (PictureBox2.Height / sf) / Convert.ToDouble(t))
                End If
                Dim points11(Numcoor - 1) As Point
                For vv = 0 To points11.Length - 1
                    points11(vv).X = CInt(Xcoor11(vv))
                    points11(vv).Y = CInt(Ycoor11(vv))
                Next
                'e.Graphics.FillPolygon(mbrush1, points1)
                e.Graphics.DrawPolygon(mpen3, points11)
                e.Graphics.DrawLine(mpen, X3a, Y5a, X4a, Y5a)
                e.Graphics.DrawLine(mpen, X5a, Y3a, X5a, Y4a)
                Dim DrawFont99 As New Font("Verdana", CInt(10 / sf), FontStyle.Regular)
                Dim DrawFont66 As New Font("Verdana", CInt(6 / sf), FontStyle.Regular)
                e.Graphics.DrawString(Str3, DrawFont99, Brush2, CInt(X4a + 5 / sf), CInt(Y5a - 8 / sf))
                e.Graphics.DrawString(Str4, DrawFont99, Brush2, CInt(X5a - 6 / sf), CInt(Y3a - 18 / sf))
                Dim Str66 As String = CInt(b) & " x " & CInt(t) & " mm"
                Dim Brush66 As New SolidBrush(Color.Black)
                Dim textSize726 As SizeF
                textSize726 = G.MeasureString(Str66, DrawFont99)
                XQQ17 = CInt((PictureBox2.ClientSize.Width / sf - CInt(textSize726.Width)) / 2)
                e.Graphics.DrawString(Str66, DrawFont99, Brush, XQQ17, CInt(0.87 * PictureBox2.ClientSize.Height / sf))
                If NumBarcoor >= 1 Then
                    If dy / dx <= Convert.ToDouble(PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width) Then
                        For YY = 0 To NumBarcoor - 1
                            Diaf1(YY) = Convert.ToInt32(Dia(YY) * (PictureBox2.ClientSize.Width / sf) * 0.85 / Convert.ToSingle(b))
                            coorXf1(YY) = CInt((PictureBox2.ClientSize.Width / sf) * 0.075 + ((Math.Abs(Xmin) + coorx1(YY)) / Convert.ToDouble(b)) * 0.85 * (PictureBox2.ClientSize.Width / sf)) - Diaf1(YY) / 2
                            coorYf1(YY) = CInt((PictureBox2.ClientSize.Height * 0.44) / sf - Convert.ToDouble(t) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (2 * Convert.ToDouble(b)) + (Ymax - coory1(YY)) * 0.85 * (PictureBox2.ClientSize.Width / sf) / (Convert.ToDouble(b))) - Diaf1(YY) / 2
                            e.Graphics.FillEllipse(mbrush, CInt(coorXf1(YY)), CInt(coorYf1(YY)), CInt(Diaf1(YY)), CInt(Diaf1(YY)))
                            e.Graphics.DrawString(CStr(YY + 1), DrawFont66, Brush, CInt(coorXf1(YY) + Diaf1(YY) + 1 / sf), CInt(coorYf1(YY) - 8 / sf))
                        Next
                    Else
                        For YY = 0 To NumBarcoor - 1
                            Diaf1(YY) = Convert.ToInt32(Dia(YY) * (PictureBox2.ClientSize.Height / sf) * 0.85 / Convert.ToSingle(t))
                            coorXf1(YY) = CInt(0.5 * PictureBox2.ClientSize.Width / sf + ((-Convert.ToDouble(b) / 2 + Math.Abs(Xmin) + coorx1(YY)) / Convert.ToDouble(t)) * 0.85 * PictureBox2.ClientSize.Height / sf) - Diaf1(YY) / 2
                            coorYf1(YY) = CInt((PictureBox2.ClientSize.Height / sf) * 0.44 + (-Convert.ToDouble(t) / 2 + Ymax - coory1(YY)) * 0.85 * (PictureBox2.ClientSize.Height / sf) / Convert.ToDouble(t)) - Diaf1(YY) / 2
                            e.Graphics.FillEllipse(mbrush, CInt(coorXf1(YY)), CInt(coorYf1(YY)), CInt(Diaf1(YY)), CInt(Diaf1(YY)))
                            e.Graphics.DrawString(CStr(YY + 1), DrawFont66, Brush, CInt(coorXf1(YY) + Diaf1(YY) + 1 / sf), CInt(coorYf1(YY) - 8 / sf))
                        Next
                    End If
                    For q = 1 To NumBarcoor
                        A(q) = pi * Dia(q - 1) ^ 2 / 4
                    Next q
                    Ass1 = 0
                    For q = 1 To NumBarcoor
                        Ass1 = Ass1 + A(q)
                    Next
                    Pr3 = Math.Round((Ass1 / Area1) * 100, 3)
                    Pr = Pr3
                    Dim Str1 As String = Pr3 & "% reinf"
                    Dim textSize19 As SizeF
                    textSize19 = G.MeasureString(Str1, DrawFont99)
                    XQQ11 = CInt((PictureBox2.ClientSize.Width / sf - textSize19.Width) / 2)
                    e.Graphics.DrawString(Str1, DrawFont99, Brush, XQQ11, CInt(0.92 * PictureBox2.ClientSize.Height / sf))
                End If
            End If
            If MouseStatus = 1 Then
                If ZoomCb.Checked Then
                    'convert for positive yaxis and rectf
                    Dim rect As New Rectangle(CInt(ZoomMouseDownPt.X), CInt(ZoomMouseDownPt.Y), CInt(ZoomMouseMovePt.X) - CInt(ZoomMouseDownPt.X), CInt(ZoomMouseDownPt.Y) - CInt(ZoomMouseMovePt.Y))
                    e.Graphics.DrawRectangle(Pens.Red, rect)
                Else
                    'draw other tracers if active
                End If
            End If
        End Sub
        Private Sub Form32Irregular_byCoordinates_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Form32Irregular_byCoordinates_Resize(0, Nothing)
            PictureBox2.Focus()
            MouseStatus = 3
        End Sub
        Private Sub Form32Irregular_byCoordinates_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Yoffset = Convert.ToSingle(ScaleWidth * (PictureBox2.ClientSize.Height / PictureBox2.ClientSize.Width))
            PictureBox2.Invalidate()
        End Sub
        Private Sub Picturebox2_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseDown
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
            If ZoomCb.Checked Then
                'keep zoom independant of drawing tools to allow nesting of zoom with tools.
                ZoomMouseDownPt = pt
                ZoomMouseMovePt = ZoomMouseDownPt
            End If
            MouseStatus = 1
            MouseMovePt = pt
            PictureBox2.Invalidate()
            PictureBox2.Focus()
        End Sub
        Private Sub Picturebox2_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseMove
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
            If MouseStatus = 1 Then
                If ZoomCb.Checked Then
                    ZoomMouseMovePt = pt
                    PictureBox2.Invalidate()
                End If
            End If
        End Sub
        Private Sub Picturebox2_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseUp
            Dim pt As PointF = GetScalePtFromClientPt(e.Location)
            If ZoomCb.Checked Then
                ZoomMouseMovePt = pt
                ZoomCb.Checked = False
                'set scale based on zoom rect
                Select Case MouseStatus
                    Case 1
                        ZoomMouseMovePt = pt
                        ZoomCb.Checked = False
                        'set scale based on zoom rect width
                        dx = CInt(ZoomMouseMovePt.X - ZoomMouseDownPt.X)
                        dy = CInt(ZoomMouseMovePt.Y - ZoomMouseDownPt.Y)
                        ScaleWidth = dx
                        'set origin
                        Corner.X = CInt(ZoomMouseDownPt.X)
                        Corner.Y = CInt(ZoomMouseDownPt.Y)
                        'update yoffset etc
                        Form32Irregular_byCoordinates_Resize(0, Nothing)
                End Select
            End If
            MouseStatus = 0
            PictureBox2.Invalidate()
        End Sub
        Private Function GetScalePtFromClientPt(pt As PointF) As PointF
            'convert screen pixels to scale drawing coords
            Dim sf As Double = PictureBox2.ClientSize.Width / (ScaleWidth * ScaleRatio)
            Return New PointF(CSng(Corner.X + (pt.X / sf)), CSng(Corner.Y + (pt.Y / sf)))
        End Function
        Private Sub Picturebox2_MouseWheel(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseWheel
            ScaleRatio += CSng(Math.Sign(e.Delta) * 0.1)
            If ScaleRatio < 0.000001 Then ScaleRatio = 0.000001
            If ScaleRatio > 10000 Then ScaleRatio = 10000
            PictureBox2.Invalidate()
        End Sub
        Private Sub Picturebox2_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox2.MouseEnter
            PictureBox2.Focus()
        End Sub

    Kind Regards,

    Hany Metry



    • Edited by Hany Metry Tuesday, November 19, 2019 6:00 PM
    Tuesday, November 19, 2019 5:58 PM
  • Hi Mr Tommy,

    I discovered my mistake to draw red rectangle for zoom.

    The I must replace the code below

    Dim rect As New Rectangle(CInt(ZoomMouseDownPt.X), CInt(ZoomMouseDownPt.Y), CInt(ZoomMouseMovePt.X) - CInt(ZoomMouseDownPt.X), CInt(ZoomMouseDownPt.Y) - CInt(ZoomMouseMovePt.Y))

    By the below one

    Dim rect As New Rectangle(CInt(ZoomMouseDownPt.X), CInt(ZoomMouseDownPt.Y), CInt(ZoomMouseMovePt.X) - CInt(ZoomMouseDownPt.X), CInt(ZoomMouseMovePt.Y) - CInt(ZoomMouseDownPt.Y))

    That I forget to modfy that code from your code after cancel Yoffset.

    Kind Regards,


    Hany Metry

    Thursday, November 21, 2019 6:23 AM