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

📄 form1.vb

📁 Source code for VB.NET book - Chapter 4
💻 VB
字号:
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    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

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.BackColor = System.Drawing.SystemColors.Window
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Top
        Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(504, 304)
        Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(160, 320)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(80, 32)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "六边形"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(296, 320)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(80, 32)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "圆"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(504, 366)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim pen1 As New System.Drawing.Pen(Color.Green, 0.4)
    Dim g As System.Drawing.Graphics
    Const Max = 20
    Const pi = 3.1415926
    Dim dx(Max), dy(Max) As Double
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        g = PictureBox1.CreateGraphics
        PictureBox1.Refresh()
        Dim i As Integer
        Dim s, r As Double
        i = 6
        s = 80
        r = 0.34
        setdate(6)
        snow(200.0#, 180.0#, 6, i, s, r)
    End Sub

    Private Sub Sixth(ByVal x, ByVal y, ByVal n, ByVal s)
        Dim i, j, x1, x2, y1, y2 As Integer
        For i = 0 To n
            If i = n Then
                j = 1
            Else
                j = i + 1
            End If
            x1 = Int(x + dx(i) * s) - 50
            y1 = Int(y + dy(i) * s) - 55
            x2 = Int(x + dx(j) * s) - 50
            y2 = Int(y + dy(j) * s) - 55
            g.DrawLine(pen1, x1, y1, x2, y2)
        Next
    End Sub

    Private Sub snow(ByVal x, ByVal y, ByVal n, ByVal i, ByVal s, ByVal r)
        Dim t, t1 As Integer
        Dim xx, yy, s1 As Double
        If i > 0 Then
            For t = 0 To n
                xx = x + dx(t) * s
                yy = y + dy(t) * s
                t1 = i - 1
                s1 = s * r
                snow(xx, yy, n, t1, s1, r)
            Next
        End If
        Sixth(x, y, n, s)
    End Sub

    Private Sub setdate(ByVal n)
        Dim i As Integer
        Dim theta As Double
        dx(i) = 0.0#
        dy(i) = 0.0#
        theta = 2.0# * pi / n
        For i = 1 To n
            dx(i) = Math.Sin(i * theta)
            dy(i) = Math.Cos(i * theta)
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        PictureBox1.Refresh()
        g = PictureBox1.CreateGraphics
        Dim n, i As Integer
        Dim r, r1, c As Single
        Dim p As Double = 0.8
        Dim f As Double = 0.2
        c = 40.0#
        n = 6
        r1 = 0.5 * 1000 * (1 - f) / (1 - p)
        r = r1 / c
        g.DrawEllipse(pen1, 200 - r, 150 - r, 2 * r, 2 * r)
        circle(200, 150, r, n + 1)
    End Sub
    Private Sub circle(ByVal x, ByVal y, ByVal r, ByVal n)
        Dim tcos(100), tsin(100) As Single
        Dim i, ns As Integer
        Dim theta As Double
        Dim f As Single = 0.3!
        Dim c As Double = 2
        ns = 10
        theta = 2 * pi / ns
        Dim n1, f1 As Single
        n1 = n - 1
        f1 = f * r
        If n1 > 1 Then
            For i = 1 To ns
                tcos(i) = c * Math.Cos(i * theta)
                tsin(i) = c * Math.Sin(i * theta)
                g.DrawEllipse(pen1, x + r * tcos(i) - f1, y + r * tsin(i) - f1, 2 * f1, 2 * f1)
                circle(x + r * tcos(i), y + r * tsin(i), f1, n1)
            Next
        End If
    End Sub
End Class

⌨️ 快捷键说明

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