【问题标题】:Create dyamically re-sizable line\shape winforms创建可动态调整大小的线条\形状 winforms
【发布时间】:2018-01-11 02:24:04
【问题描述】:

我是编程新手。任何人都可以帮助我在图片框中创建线条/形状,并使用线条/形状上的手柄。就像我们在 CAD 软件中所做的那样。

我想知道如何在鼠标单击时创建一条线,直到发生另一个鼠标单击事件。

Public Class Form1

Dim isDrag As Boolean = False
Dim theRectangle As New Rectangle(New Point(0, 0), New Size(0, 0))
Dim startPoint As Point
Dim IsDimension As Boolean = False
Dim LineLocationStPoint As Point = Nothing
Dim LineLocationEndPoint As Point = Nothing
Dim cnt As Integer = 0
Dim LineArray As New ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim fd As OpenFileDialog = New OpenFileDialog()
    Dim strFileName As String

    fd.Title = "Open File Dialog"
    fd.Filter = "(*.PDF;*.DWG;*.TIFF;*.TIF)|*.PDF;*.DWG;*.TIFF;*.TIF|All files (*.*)|*.*"
    fd.FilterIndex = 2
    fd.RestoreDirectory = True

    If fd.ShowDialog() = DialogResult.OK Then
        strFileName = fd.FileName
        'ShowFileInWebBrowser(WebBrowser1, strFileName)
        PictureBox1.Load(strFileName)
    End If
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As  _
    System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    If (e.Button = MouseButtons.Right) Then
        isDrag = True
    End If

    Dim control As Control = CType(sender, Control)


    startPoint = control.PointToScreen(New Point(e.X, e.Y))

    If (e.Button = MouseButtons.Left) Then
        IsDimension = True
        LineLocationStPoint = e.Location
        LineArray.Add(e.Location)
    End If
End Sub

Private  Sub Form1_MouseMove(ByVal sender As Object, ByVal e As  _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove


    If (isDrag) Then

        ControlPaint.DrawReversibleFrame(theRectangle, Me.BackColor, _
            FrameStyle.Dashed)


         Dim endPoint As Point = CType(sender, Control).PointToScreen(New Point(e.X, e.Y))
         Dim width As Integer = endPoint.X - startPoint.X
         Dim height As Integer = endPoint.Y - startPoint.Y
         theRectangle = New Rectangle(startPoint.X, startPoint.Y, _
            width, height)

         ControlPaint.DrawReversibleFrame(theRectangle, Me.BackColor, _
             FrameStyle.Dashed)
    End If
    If IsDimension Then
        LineLocationEndPoint = e.Location
        Dim g As Graphics = PictureBox1.CreateGraphics()
        g.DrawLine(Pens.Red, LineLocationStPoint, e.Location)
        g.Dispose()
    End If
End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As  _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp

    If IsDimension Then
        PictureBox1.Refresh()
    ElseIf isDrag Then
        ' If the MouseUp event occurs, the user is not dragging.
        isDrag = False

        ' Draw the rectangle to be evaluated. Set a dashed frame style 
        ' using the FrameStyle enumeration.
        ControlPaint.DrawReversibleFrame(theRectangle, Me.BackColor, _
            FrameStyle.Dashed)

        ' Find out which controls intersect the rectangle and change their color.
        ' The method uses the RectangleToScreen method to convert the 
        ' Control's client coordinates to screen coordinates.
        Dim i As Integer
        Dim controlRectangle As Rectangle
        For i = 0 To Controls.Count - 1
            controlRectangle = Controls(i).RectangleToScreen _
                (Controls(i).ClientRectangle)
            If controlRectangle.IntersectsWith(theRectangle) Then
                Controls(i).BackColor = Color.BurlyWood
            End If
        Next

        ' Reset the rectangle.
        theRectangle = New Rectangle(0, 0, 0, 0)
    End If       

End Sub

但它会从选定点连续创建线。而我想创建一条线仅用于向用户显示线的路径。我已经实现了选择矩形一个右键单击按钮

工作流程: 工具栏将包含线条和区域

  • 打开一个文件(图像文件)
  • 点击工具栏的行按钮
  • *来到图片框
  • *点击屏幕的某一点
  • *动态线开始在屏幕上绘制(线将从第一个点击的点到鼠标的任何位置)
  • *当用户点击下一次创建行时。

  • 工具栏的点击区域

  • *回到图片框

  • *操作和线条一样,但是当用户点击图片框上的第三个点时,应该会出现一个阴影矩形。

【问题讨论】:

  • 多好的作业啊!到目前为止,您尝试了什么?
  • 我更新了我的问题。请看看它并帮助我..感谢您这么快回复
  • Acutally 我不确定,如果 MouseUp 事件是你正在寻找的。那是因为我不是 100% 确定练习有哪个目标 ;-) 我认为,第一次右键单击开始绘制,第二次停止它?如果是这样,请将isDrag = True 更改为isDrag = Not isDrag
  • isdrag 用于创建选择矩形框
  • 我已经用程序更新了问题。

标签: vb.net winforms cad


【解决方案1】:

http://www.vb-helper.com/howto_2005_line_control.html

对于那些想要在其中实现调整大小的线的人。 要添加可调整大小的行,您需要添加自己的自定义控件。使用此自定义控件并在表单中添加/使用它以供进一步使用。 感谢大家的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2014-05-24
    • 2012-08-02
    • 1970-01-01
    相关资源
    最近更新 更多