⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmgdilines.vb

📁 This program demonstrates how to draw lines in a picture box using the GDI, controlling background c
💻 VB
📖 第 1 页 / 共 2 页
字号:
'GDILines - Demonstrating how to draw lines in a picture box using the GDI, controlling background color 
' and line color. It also includes a simple animation using a timer.. 

Option Strict On

Public Class frmGDILines
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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 btnPaint As System.Windows.Forms.Button
    Friend WithEvents picDisplay As System.Windows.Forms.PictureBox
    Friend WithEvents cboPenColors As System.Windows.Forms.ComboBox
    Friend WithEvents cboBackgroundColors As System.Windows.Forms.ComboBox
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents cboPictureToDraw As System.Windows.Forms.ComboBox
    Friend WithEvents tmrAnimator As System.Windows.Forms.Timer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.btnPaint = New System.Windows.Forms.Button()
        Me.picDisplay = New System.Windows.Forms.PictureBox()
        Me.cboPenColors = New System.Windows.Forms.ComboBox()
        Me.cboBackgroundColors = New System.Windows.Forms.ComboBox()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.cboPictureToDraw = New System.Windows.Forms.ComboBox()
        Me.tmrAnimator = New System.Windows.Forms.Timer(Me.components)
        Me.SuspendLayout()
        '
        'btnPaint
        '
        Me.btnPaint.Location = New System.Drawing.Point(312, 488)
        Me.btnPaint.Name = "btnPaint"
        Me.btnPaint.Size = New System.Drawing.Size(152, 23)
        Me.btnPaint.TabIndex = 0
        Me.btnPaint.Text = "Paint!"
        '
        'picDisplay
        '
        Me.picDisplay.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.picDisplay.Location = New System.Drawing.Point(24, 16)
        Me.picDisplay.Name = "picDisplay"
        Me.picDisplay.Size = New System.Drawing.Size(442, 392)
        Me.picDisplay.TabIndex = 1
        Me.picDisplay.TabStop = False
        '
        'cboPenColors
        '
        Me.cboPenColors.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboPenColors.Location = New System.Drawing.Point(24, 488)
        Me.cboPenColors.Name = "cboPenColors"
        Me.cboPenColors.Size = New System.Drawing.Size(128, 21)
        Me.cboPenColors.TabIndex = 2
        '
        'cboBackgroundColors
        '
        Me.cboBackgroundColors.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboBackgroundColors.Location = New System.Drawing.Point(168, 488)
        Me.cboBackgroundColors.Name = "cboBackgroundColors"
        Me.cboBackgroundColors.Size = New System.Drawing.Size(128, 21)
        Me.cboBackgroundColors.TabIndex = 3
        '
        'Label6
        '
        Me.Label6.AccessibleDescription = "Label with text ""Brush Size"""
        Me.Label6.AccessibleName = "Brush Size Label"
        Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label6.ImeMode = System.Windows.Forms.ImeMode.NoControl
        Me.Label6.Location = New System.Drawing.Point(24, 456)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(112, 23)
        Me.Label6.TabIndex = 19
        Me.Label6.Text = "Pen Color:"
        '
        'Label1
        '
        Me.Label1.AccessibleDescription = "Label with text ""Brush Size"""
        Me.Label1.AccessibleName = "Brush Size Label"
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.ImeMode = System.Windows.Forms.ImeMode.NoControl
        Me.Label1.Location = New System.Drawing.Point(168, 456)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(120, 23)
        Me.Label1.TabIndex = 20
        Me.Label1.Text = "Background Color:"
        '
        'Label2
        '
        Me.Label2.AccessibleDescription = "Label with text ""Brush Size"""
        Me.Label2.AccessibleName = "Brush Size Label"
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.ImeMode = System.Windows.Forms.ImeMode.NoControl
        Me.Label2.Location = New System.Drawing.Point(24, 424)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(128, 23)
        Me.Label2.TabIndex = 21
        Me.Label2.Text = "Picture to Draw:"
        '
        'cboPictureToDraw
        '
        Me.cboPictureToDraw.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboPictureToDraw.Location = New System.Drawing.Point(160, 424)
        Me.cboPictureToDraw.Name = "cboPictureToDraw"
        Me.cboPictureToDraw.Size = New System.Drawing.Size(304, 21)
        Me.cboPictureToDraw.TabIndex = 22
        '
        'tmrAnimator
        '
        Me.tmrAnimator.Interval = 1
        '
        'frmGDILines
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(492, 533)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cboPictureToDraw, Me.Label2, Me.Label1, Me.Label6, Me.cboBackgroundColors, Me.cboPenColors, Me.picDisplay, Me.btnPaint})
        Me.MaximizeBox = False
        Me.Name = "frmGDILines"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Drawing Lines with the GDI"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim gMyGraphics As Graphics ' the drawing surface
    Dim cMyColor As Color ' used to hold colors for the pen and brush
    Dim pMyPen As Pen ' used to draw the lines
    Dim bMyBrush As SolidBrush = New SolidBrush(Color.White) ' used to draw the background
    Dim intPosition As Integer ' position to draw the animated lines
    Dim intDirection As Integer ' controls direction of the animated lines

    Private Sub frmGDILines_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '--------------------------------------------------------------------------
        ' this procedure runs as the form loads...does some initial set-up

        Call SetUpCombos() ' fills the combo boxes
    End Sub

    Private Sub DrawPicture(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaint.Click
        '--------------------------------------------------------------------------
        ' this procedure is called when you click the paint button.  it does painting for everything except
        ' the animated lines.  

        ' check to see if the user has chosen to see animated lines
        If cboPictureToDraw.Text <> "Animated lines" Then
            tmrAnimator.Enabled = False ' turn the timer off
            intPosition = 0 ' set the animating position to 0 for starting off
        Else
            tmrAnimator.Enabled = True ' turn on the timer, begin the animation
            Exit Sub ' leave this sub procedure
        End If

        ' set up the drawing surface to draw in the picturebox
        gMyGraphics = picDisplay.CreateGraphics

        ' set up a pen with the color named in the pen color combo box
        ' - this pen will be drawing the lines
        cMyColor = Color.FromName(CStr(cboPenColors.SelectedItem))
        pMyPen = New Pen(cMyColor, 1)

        ' set up a brush with the color named in the background color combo box
        ' - this brush will be drawing the background color
        cMyColor = Color.FromName(CStr(cboBackgroundColors.SelectedItem))
        bMyBrush.Color = cMyColor

        ' fill the picturebox with a background color
        gMyGraphics.FillRectangle(bMyBrush, 0, 0, picDisplay.Width, picDisplay.Height)

        Dim i As Integer ' used for looping

        ' examine the chosen picture in the cboPictureToDraw combo box
        ' - execute a drawing routine based on what the user has selected
        Select Case cboPictureToDraw.Text

            Case "Lines from top left corner"

                For i = picDisplay.Width To 0 Step -15
                    gMyGraphics.DrawLine(pMyPen, 0, 0, i, picDisplay.Width)
                Next i

                For i = 0 To picDisplay.Width Step 15
                    gMyGraphics.DrawLine(pMyPen, 0, 0, picDisplay.Width, i)
                Next i

            Case "Lines from bottom left corner"

                For i = picDisplay.Width To 0 Step -15
                    gMyGraphics.DrawLine(pMyPen, 0, picDisplay.Height, i, 0)
                Next i

                For i = 0 To picDisplay.Width Step 15
                    gMyGraphics.DrawLine(pMyPen, 0, picDisplay.Height, picDisplay.Width, i)
                Next i

            Case "Lines from top right corner"

                For i = picDisplay.Width To 0 Step -15
                    gMyGraphics.DrawLine(pMyPen, picDisplay.Width, 0, i, picDisplay.Width)
                Next i

                For i = 0 To picDisplay.Width Step 15
                    gMyGraphics.DrawLine(pMyPen, picDisplay.Width, 0, 0, i)
                Next i

            Case "Lines from bottom right corner"

                For i = picDisplay.Width To 0 Step -15
                    gMyGraphics.DrawLine(pMyPen, picDisplay.Width, picDisplay.Height, i, 0)
                Next i

                For i = 0 To picDisplay.Width Step 15
                    gMyGraphics.DrawLine(pMyPen, picDisplay.Width, picDisplay.Height, 0, i)
                Next i

            Case "Lines from top corners"

                For i = picDisplay.Width To 0 Step -15
                    gMyGraphics.DrawLine(pMyPen, picDisplay.Width, 0, i, picDisplay.Width)
                Next i

                For i = 0 To picDisplay.Width Step 15
                    gMyGraphics.DrawLine(pMyPen, picDisplay.Width, 0, 0, i)
                Next i

                For i = picDisplay.Width To 0 Step -15
                    gMyGraphics.DrawLine(pMyPen, 0, 0, i, picDisplay.Width)
                Next i

                For i = 0 To picDisplay.Width Step 15
                    gMyGraphics.DrawLine(pMyPen, 0, 0, picDisplay.Width, i)
                Next i

            Case "Lines from bottom corners"

                For i = picDisplay.Width To 0 Step -15
                    gMyGraphics.DrawLine(pMyPen, picDisplay.Width, picDisplay.Height, i, 0)
                Next i

                For i = 0 To picDisplay.Width Step 15
                    gMyGraphics.DrawLine(pMyPen, picDisplay.Width, picDisplay.Height, 0, i)
                Next i

                For i = picDisplay.Width To 0 Step -15
                    gMyGraphics.DrawLine(pMyPen, 0, picDisplay.Height, i, 0)
                Next i

                For i = 0 To picDisplay.Width Step 15
                    gMyGraphics.DrawLine(pMyPen, 0, picDisplay.Height, picDisplay.Width, i)
                Next i

            Case "Lines from crossed corners 1"

                For i = picDisplay.Width To 0 Step -15
                    gMyGraphics.DrawLine(pMyPen, picDisplay.Width, picDisplay.Height, i, 0)
                Next i

                For i = 0 To picDisplay.Width Step 15
                    gMyGraphics.DrawLine(pMyPen, picDisplay.Width, picDisplay.Height, 0, i)
                Next i

                For i = picDisplay.Width To 0 Step -15
                    gMyGraphics.DrawLine(pMyPen, 0, 0, i, picDisplay.Width)
                Next i

                For i = 0 To picDisplay.Width Step 15
                    gMyGraphics.DrawLine(pMyPen, 0, 0, picDisplay.Width, i)
                Next i

            Case "Lines from crossed corners 2"

                For i = picDisplay.Width To 0 Step -15
                    gMyGraphics.DrawLine(pMyPen, 0, picDisplay.Height, i, 0)
                Next i

                For i = 0 To picDisplay.Width Step 15
                    gMyGraphics.DrawLine(pMyPen, 0, picDisplay.Height, picDisplay.Width, i)
                Next i

                For i = picDisplay.Width To 0 Step -15
                    gMyGraphics.DrawLine(pMyPen, picDisplay.Width, 0, i, picDisplay.Width)
                Next i

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -