vb.net - 单击时动态更改笔的颜色和绘画

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

该程序

我正在使用visual basic(来自C++)玩耍和学习图形。我制作了一个程序,我想做两件事:按下鼠标左键时绘画,松开时停止,我还希望能够使用颜色对话框更改笔的颜色。经过数小时的沮丧之后,我还没有解决这两个问题。

代码(片段)

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 的文档,但它对我正在尝试做的事情没有用。我可以很好地创建一支笔,但我希望用户能够在应用程序当前运行时更改颜色。

图形用户界面布局

enter image description here

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

最佳答案

在窗体上放置一个按钮 (Button1) 和图片框 (PictureBox1),同时添加一个颜色对话框 (ColorDialog1)。

此代码将允许您在图片框上绘制并使用您从颜色对话框中选择的颜色来选择颜色。 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

关于vb.net - 单击时动态更改笔的颜色和绘画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25121137/

相关文章:

c# - 在单晶报告查看器中加载多个报告

visual-studio-2010 - Visual Studio Debug模式:how to go to the location of the next statement to be executed?

c# - 区分两大段文字

mysql - fatal error : attempting to read the result set

c# - 在 Windows 窗体中托管 IDeskBand

c# - 如何在没有消息的情况下保存/覆盖现有的 Excel 文件

c++ - 是否可以在所有平台上使用 vs2010 c++ 并使用 Qt?

c# - 签署 WP7 项目的强名?

vb.net - 没有应用程序与此操作的指定文件关联 (VB.NET)

vb.net:是否可以一次注释掉多于1行的代码?