วันอาทิตย์ที่ 4 มกราคม พ.ศ. 2558

VB.net sample Analog Clock

ตัวอย่างโค้ดโปรแกรม นาฬิกาเข็ม
โค้ดไม่ยากขอไม่อธิบายให้ยืดยาว โหลดไปดูกันเองละกันครับ
โปรเจ็กนี้เขียนด้วย Visual Studio 2013 และ .Net 4.0





โค้ดโปรแกรม

Public Class Form1
    Dim Minute1 As Bitmap
    Dim Second1 As Bitmap
    Dim HourP As Bitmap
    Dim Height1, Width1 As Integer

    Friend WithEvents Timer1 As New System.Windows.Forms.Timer
    Friend WithEvents PictureBox1 As New System.Windows.Forms.PictureBox

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Size = New Size(316, 338)
        Me.Timer1.Interval = 1000
        Me.Timer1.Enabled = True
        Me.PictureBox1.BackColor = System.Drawing.SystemColors.ActiveCaptionText
        Me.PictureBox1.Location = New System.Drawing.Point(-1, 0)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(300, 300)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        Me.PictureBox1.BackColor = Color.White
        Me.Controls.Add(PictureBox1)
        Me.Label1.BackColor = Color.Black
        Me.Label1.ForeColor = Color.Yellow
        Me.Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) _
              Handles Timer1.Tick

        Height1 = PictureBox1.Size.Height
        Width1 = PictureBox1.Size.Width
        Dim TM As Bitmap = New Bitmap(Height1, Width1)
        Dim G As Graphics = Graphics.FromImage(TM)
        PictureBox1.Controls.Clear()
        Dim x1 As Integer = Height1 / 2
        Dim y1 As Integer = Width1 / 2
        G.TranslateTransform(x1, y1)

        'หน้าปัด
        Dim PenBlack As New Pen(Brushes.Black, 5)
        Dim PenGreen As New Pen(Brushes.Green, 4)

        For i As Integer = 1 To 60
            G.ResetTransform()
            G.TranslateTransform(x1, y1)
            G.RotateTransform(i * 6)
            G.DrawLine(PenGreen, 0, -131, 0, -135)
        Next
        For i As Integer = 1 To 12
            G.ResetTransform()
            G.TranslateTransform(x1, y1)
            G.RotateTransform(i * 30)
            G.DrawLine(PenBlack, 0, -115, 0, -145)
        Next


        Dim HPen As New Pen(Color.Blue, 20) 'เข็มสั้น
        G.ResetTransform()
        G.TranslateTransform(x1, y1)
        G.RotateTransform(((Now.Hour * 30) + (Now.Minute * 0.5)))
        G.DrawLine(HPen, 0, 20, 0, -100)

        Dim MinPen As New Pen(Color.Orange, 10) 'เข็มยาว
        G.ResetTransform()
        G.TranslateTransform(x1, y1)
        G.RotateTransform((((Now.Minute * 60) + Now.Second) * 0.1))
        G.DrawLine(MinPen, 0, 20, 0, -140)

        Dim SecPen As New Pen(Color.Red, 4) 'เข็มวินาที
        G.ResetTransform()
        G.TranslateTransform(x1, y1)
        G.RotateTransform((Now.Second * 6))
        G.DrawLine(SecPen, 0, 20, 0, -138)

        Dim PointPen As New Pen(Color.Black, 2) 'หมุด
        Dim PointPenRed As New Pen(Color.Red, 12)
        G.ResetTransform()
        G.TranslateTransform(x1, y1)
        G.DrawEllipse(PointPenRed, -2, -2, 4, 4)
        G.DrawEllipse(PointPen, -2, -2, 4, 4)

        PictureBox1.Image = TM

        Label1.Text = Now.Hour & "." & Now.Minute & "." & Now.Second
    End Sub
End Class


ดาว์นโหลดโปรเจ็ก

ในบางครั้งเข็มมันก็ไม่จำเป็นต้องตรงเสมอไป เปลี่ยนจาก DrawLine เป็น DrawCurve แทนซะก็สวยไปอีกแบบ

VB.net สร้าง Form วิ่งตาม Mouse

บทความนี้ใช้ Visual Studio 2013 และ .Net 4.0

โดยเราจะใช้ Properties MousePosition ของคลาส System.Windows.Forms.Control ในการเก็บค่าตำแหน่งของเมาส์

สร้างโปรเจ็กขึ้นมาเป็น VisualBasic Windows Forms .Net4.0 ตั้งชื่อโปรแกรมว่าอะไรก็ได้

สร้าง Control ขั้นมาสอง Control คือ PictureBox และ Timer


กำหนด Properties ต่างๆ ดังนี้

Form1:
BackColor = Green
FormBorderStyle = None
Size = 80,80
TransparencyKey = Green

PictureBox1:
BackColor = Green
Dock = Fill
Image = เลือกรูปที่ท่านต้องการ
ในตัวอย่างผมใช้รูปกาตูนละกัน
SizeMode = Zoom

Timer1:
inteval = 10

กด F7 เพื่อเปิดหน้า View Code
ประกาศตัวแปล LocalMousePosition เป็นชนิท Point  ดังนี้
Dim LocalMousePosition As Point
สร้าง Event Timer1.Tick และชุดคำสั่งดังนี้
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) _
        Handles Timer1.Tick
        LocalMousePosition = Control.MousePosition
        Me.Location = New Point(LocalMousePosition.X - (Me.Width / 2), _
                                LocalMousePosition.Y - (Me.Height / 2))
    End Sub
 สร้าง Event PictureBox1.MouseClick ขั้นมาโดยมีคำสั่งดังนี้
    Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) _
        Handles PictureBox1.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Me.Close()
        Else
            If Timer1.Enabled Then
                Timer1.Enabled = False
            Else
                Timer1.Enabled = True
            End If
        End If
    End Sub
โค้ดทั้งหมดจะเป็นดังนี้


ดาว์นโหลดโปรเจ็ก Source Code

เซฟและคลิกปุ่ม Start Debuging เพื่อรันโปรแกรมทดสอบ
คลิกที่รูปภาพเพื่อให้ Timer1 เริ่มทำงาน ทดสอบลากเมาส์
คลิกที่รูปอีกครั้งเพื่อให้ Timer1 หยุดทำงาน
คลิกขวาที่รูปเพื่อปิดโปรแกรม