Мой Kbyte.Ru
Рассылка Kbyte.Ru
Группы на Kbyte.Ru
Партнеры Kbyte.Ru
Сделано руками
Сделано руками
> Исходные коды - Джон Бюттнер -

Visual Basic .NET - Графика

Все примеры / Мультимедиа / Графика

Рисование мышкой на PictureBox

Автор: Джон Бюттнер | добавлено: 08.01.2011, 12:37 | просмотров: 6577 (1+) | комментариев: 0 | рейтинг: *x5
Простой пример рисования в PictureBox мышкой.


'I got frustrated trying to find a Simple VB.NET freehand drawing code
'So I made my own :)

'*** Acknowlegements  ***
'Ideas for this code came from the MicroSoft "Scribble" sample code, 
'Christian Graus's excellent arcticle on a C++ code called "Doodle" 
'and the MicroSoft website.

'Note that this is a VERY pedantic freehand drawing code, so be kind in you comments. :)
'It uses a graphics path to follow the users mouse movements
'The path is then painted in the window.

'By John Buettner
'26 July 2003
'This code is for informational purposes
'It is property of the code writer, but may be modified and
'used for any purpose private or commercial, However,
'Users of this code must agree not to copyright or infringe
'upon the original coders rights to this code.

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
  'Namespace myPaint

  Inherits System.Windows.Forms.Form  ' Of course ;)

  Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath() 'declare a new Graphic path to follow the mouse movement

  '*** below I declare some values for an Alpha  and other user selected variables
  'these will be used as I expand this program for a higher level use.

  Dim myAlpha As Integer = 100 ' declare a Alpha variable
  Dim myUserColor As New Color() 'this is a color the user selects
  Dim myPenWidth As Single = 5 'set pen width variable


#Region " Windows Form Designer generated code "

  Public Sub New()

    'This call is required by the Windows Form Designer.

    'Add any initialization after the InitializeComponent() call

  End Sub

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

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

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.PictureBox1 = New System.Windows.Forms.PictureBox()
    CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.Label1.Location = New System.Drawing.Point(87, 35)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(393, 27)
    Me.Label1.TabIndex = 1
    Me.Label1.Text = "Below is a Graphics path freehand drawing space"
    Me.PictureBox1.BackColor = System.Drawing.Color.Ivory
    Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.PictureBox1.Location = New System.Drawing.Point(93, 83)
    Me.PictureBox1.Name = "PictureBox1"
    Me.PictureBox1.Size = New System.Drawing.Size(374, 236)
    Me.PictureBox1.TabIndex = 2
    Me.PictureBox1.TabStop = False
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.BackColor = System.Drawing.Color.BlanchedAlmond
    Me.ClientSize = New System.Drawing.Size(696, 456)
    Me.Name = "Form1"
    Me.Text = "Johns' Free Hand Doodle"
    CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()

  End Sub

#End Region

  Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown

    If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down  

      mousePath.StartFigure()    ' The L mouse is down so we need to start a new line in mousePath

    End If

  End Sub

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

    If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down  

        mousePath.AddLine(e.X, e.Y, e.X, e.Y)    'Add mouse coordiantes to mousePath

        MsgBox("No way, Hose!")
      End Try

    End If

    PictureBox1.Invalidate() 'Repaint the PictureBox using the PictureBox1 Paint event

  End Sub

  Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    ' Here is where we do the actual painting

    Try ' error trapping

      myUserColor = (System.Drawing.Color.Black) 'You can remove this line and add a user selected color to
      'change the value of myUserColor

      myAlpha = 100  ' This will give the color a Alpha effect, you can set this to 255 if you want a full color

      '*********************** NOTE  ***********************************************
      'The line below set the pen up with the ability to add user selected Alpha, Color and Penwidth
      ' A simpler, but less flexible solution would be to replace the line with the following code:
      'Dim CurrentPen = New Pen(System.Drawing.Color.Black, myPenWidth)
      '************  End Note  ***************************

      Dim CurrentPen = New Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 'Set up the pen

      e.Graphics.DrawPath(CurrentPen, mousePath)  'draw the path! :)

      ' MsgBox("Not happening!")
    End Try

  End Sub

  'End Namespace

End Class 'Form1
Файлы Freehand_Drawing_Source_Code.zip (10,25 Кб)
Об авторе

Джон Бюттнер

Нет информации об авторе...
Джон Бюттнер
Последние комментарии (всего: 0)

Добавлять комментарии могут только зарегистрированные пользователи сайта.
Если у Вас уже есть учетная запись на Kbyte.Ru, пройдите процедуру авторизации OpenID.
Если Вы еще не зарегистрированы на Kbyte.Ru - зарегистрируйтесь.

Нет комментариев...

Зарегистрируйся и получи 10% скидку на добавление своего сайта в каталоги! Подробнее »
Поиск по сайту
Люди на Kbyte.Ru