动态改变笔的颜色和点击时画画

时间:2014-08-04 14:33:10

标签: vb.net winforms visual-studio-2010 graphics

该计划

我正在玩游戏并使用visual basic(来自C ++)学习图形。我做了一个程序,我想做两件事:按下鼠标左键时画画,释放后停止,我希望能用colordialog改变笔颜色。经过几个小时的挫折,我还没有解决这两个问题。

代码(代码段)

Private obj As Graphics
Dim rect As Rectangle

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    obj = RichTextBox1.CreateGraphics
End Sub

Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
    obj.Dispose()
End Sub

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

    With rect
        .X = e.X
        .Y = e.Y
        .Width = TrackBar1.Value
        .Height = TrackBar1.Value
    End With

    If ToolStripButton1.Checked = True Then
        obj.DrawEllipse(Pens.Black, rect)
    ElseIf ToolStripButton2.Checked = True Then
        obj.DrawRectangle(Pens.Black, rect)
    End If

    ToolStripStatusLabel2.Text = (e.X & ", " & e.Y)

End Sub

过去的尝试(和挫折)

我的想法最初是为了做到这一点:

Dim myPen = New Pen(ButtonWithDC1.BackColor)

但这样做给了我一个错误信息。我查看了Microsoft的文档,但它对我正在尝试的内容没有用。我可以创建一支笔,但我希望用户能够在应用程序当前运行时更改颜色。

GUI布局

enter image description here

我没有试图解决我的另一个问题(在按下鼠标的同时进行绘图,而不仅仅是通过移动鼠标 - 就像普通的绘图程序一样),我甚至没有解决方案的起点。提前感谢大家。

2 个答案:

答案 0 :(得分:2)

你的问题比你想象的要多一些,而且每个帖子都不热衷于多个问题(也不是你的巢穴兴趣 - 有人可能知道A而不是B因此不会打扰回答)。

要在鼠标停止时绘制Mousemove,您需要跟踪鼠标何时关闭(问题A):

Private _mouseDown As Boolean
Private _mouseLoc As Point 
Private _mouseNewLoc As Point

sub Ctl_MouseDown(sender...
  ' ToDo: add logic to check which button....
   _mouseDown = True 
   _mouseLoc = New Point(e.X, e.Y)
End Sub

sub Ctl_MouseUp(sender...
   _mouseDown = False
End Sub

然后mousemove可用于捕获当前位置

Sub Ctl_MouseMove(sender....
     If _mouseDn Then
          _mouseNewLoc = New Point(e.X, e.Y)
          Ctl.invalidate    ' call to paint
     End If
End Sub

' selected color from dialog or whereever
Private myColor As Color
Sub Ctl_Paint(sender....

    If _mouseDn Then
        ' Pen ctor is overloaded...(Question B)
        Using p As New Pen(myColor)
            e.Graphics.DrawLine(p, _mouseLoc, _mouseNewLoc)
           ' plus more....
        End Using
    End If

这只解决了提出的问题;你所面临的更大问题是跟踪已经绘制的内容。这将仅在鼠标向下时绘制一条线,但对于多边形或形状,您必须添加代码以重绘这些部分。可以是组成多边形的点列表,也可以将您拥有的内容保存到位图并添加到其中。这有点超出了问题的范围,取决于应用因素。你还需要一个绘图开始/停止或方式来指示何时停止添加线条或椭圆形或其他任何东西(椭圆形很简单:一次一个,线条作为形状的一部分需要一些工作)。

无论哪种方式,如果要查看形状/绘图/图像的开发,所有绘画都必须在Paint事件(或OnPaint)中进行。

答案 1 :(得分:2)

在表单上放置一个按钮(Button1)和图片框(PictureBox1),同时添加一个colordialog(ColorDialog1)。

此代码允许您在图片框上绘图,并使用您从colordialog中选择的颜色选择颜色。 MouseDown事件写入鼠标已关闭的标志,并存储最后一个位置。 MouseUp做的类似。 MouseMove实际绘制。使用一行和最后一个位置。

Public Class Form1

    Private myColor As Color = Color.Black
    Private mouseIsDown As Boolean = False
    Private previousLocation As System.Nullable(Of System.Drawing.Point) = Nothing

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        myColor = If(ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK, ColorDialog1.Color, myColor)
    End Sub

    Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
        mouseIsDown = True
        previousLocation = e.Location
    End Sub

    Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
        If mouseIsDown Then
            If previousLocation IsNot Nothing Then
                Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
                    g.DrawLine(New Pen(myColor), previousLocation.Value, e.Location)
                End Using
                PictureBox1.Invalidate()
            End If
            previousLocation = e.Location
        End If
    End Sub

    Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
        mouseIsDown = False
        previousLocation = Nothing
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    End Sub
End Class
相关问题