ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 75|ตอบกลับ: 0

[VB.NET] แจกโค้ดต้นฉบับในการทำ Metronome (อุปกรณ์ที่ใช้กำหนดจังหวะ)

[คัดลอกลิงก์]

337

กระทู้

533

โพสต์

7684

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
7684
โพสต์ 7 วันที่แล้ว | ดูโพสต์ทั้งหมด |โหมดอ่าน








Add References ... KryptonToolkit ...



การเปลี่ยน Skin หรือ Theme ของ KryptonToolkit 5.55 สำหรับ .Net Framework 4.0

Metronome (เมโทรนอม) คืออุปกรณ์ที่ใช้กำหนดจังหวะ (Tempo) ในดนตรี โดยจะส่งเสียง "ติ๊ก-ต็อก" หรือเสียงเคาะออกมาเป็นช่วงเวลาที่สม่ำเสมอ เพื่อให้นักดนตรีเล่นตามจังหวะได้ตรงกัน

หน้าที่หลักของ Metronome
- ช่วยควบคุมความเร็วของเพลง (เช่น 60, 80, 120 BPM)
- ฝึกการรักษาจังหวะให้สม่ำเสมอ
- ใช้ซ้อมดนตรีเดี่ยวหรือทั้งวง

BPM (Beats Per Minute) คือจำนวนครั้งของจังหวะใน 1 นาที
60 BPM = 1 จังหวะต่อวินาที
120 BPM = 2 จังหวะต่อวินาที

4/4 คือ เครื่องหมายกำหนดจังหวะ (Time Signature) ที่ใช้บอกโครงสร้างของจังหวะในเพลง
ความหมายของ 4/4
ตัวเลขบน (4) = มี 4 จังหวะ ใน 1 ห้องเพลง (1 Bar)
ตัวเลขล่าง (4) = หนึ่งจังหวะมีค่าเป็น โน้ตตัวดำ (Quarter Note)
ดังนั้น 4/4 = 1 ห้องมี 4 จังหวะ และแต่ละจังหวะเท่ากับโน้ตตัวดำ 1 ตัว


มาดูโค้ดต้นฉบับเต็มกันเถอะ ...
  1. Imports System.Media
  2. Imports Krypton.Toolkit

  3. Public Class frmMetronome

  4.     '// ==============================
  5.     ' API สำหรับควบคุมเสียง Windows
  6.     '// ==============================
  7.     Private Declare Function waveOutGetVolume Lib "winmm.dll" _
  8.         (ByVal uDeviceID As Integer, ByRef lpdwVolume As Integer) As Integer

  9.     Private Declare Function waveOutSetVolume Lib "winmm.dll" _
  10.         (ByVal uDeviceID As Integer, ByVal dwVolume As Integer) As Integer


  11.     '// ==============================
  12.     ' ตัวแปรหลักของโปรแกรม
  13.     '// ==============================
  14.     Private player As SoundPlayer
  15.     Private isLeft As Boolean = True
  16.     Private isRunning As Boolean = False

  17.     Private imgLeft As Image
  18.     Private imgRight As Image

  19.     Private WithEvents tmrBeat As New Timer

  20.     Private Sub frmMetronome_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  21.         Me.Dispose()
  22.         GC.SuppressFinalize(Me)
  23.         Application.Exit()
  24.     End Sub

  25.     '// ==============================
  26.     ' FORM LOAD
  27.     '// ==============================
  28.     Private Sub frmMetronome_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  29.         ' ค่า BPM เริ่มต้น
  30.         With trkBPM
  31.             .Minimum = 40
  32.             .Maximum = 240
  33.             .Value = 80
  34.             .BackStyle = Krypton.Toolkit.PaletteBackStyle.ButtonForm
  35.             .TickFrequency = 10
  36.         End With
  37.         trkBPM.Value = 80
  38.         lblBPM.Text = "BPM : 80"
  39.         Call SetTimerInterval()

  40.         ' โหลดเสียง
  41.         Dim soundPath As String = MyPath(Application.StartupPath) & "Sound1.wav"
  42.         player = New SoundPlayer(soundPath)

  43.         ' โหลดรูปครั้งเดียว
  44.         imgLeft = Image.FromFile(MyPath(Application.StartupPath) & "PendulumLeft.png")
  45.         imgRight = Image.FromFile(MyPath(Application.StartupPath) & "PendulumRight.png")
  46.         picPendulum.Image = imgLeft

  47.         ' ตั้งค่า Volume จากระบบ
  48.         With trkVolume
  49.             .Minimum = 0
  50.             .Maximum = 100
  51.             .Value = 50
  52.             .BackStyle = Krypton.Toolkit.PaletteBackStyle.ButtonForm
  53.             .TickFrequency = 10
  54.         End With
  55.         trkVolume.Value = GetSystemVolume()
  56.         lblVolume.Text = "Vol : " & trkVolume.Value & "%"

  57.         btnStart.Text = "เริ่ม"

  58.         '// KryptonToolkit Palette.
  59.         With cmbPalette
  60.             .Items.Add("Office2003")
  61.             .Items.Add("Office2007Blue")
  62.             .Items.Add("Office2007Silver")
  63.             .Items.Add("Office2007Black")
  64.             .Items.Add("Office2010Blue")
  65.             .Items.Add("Office2010Silver")
  66.             .Items.Add("Office2010Black")
  67.         End With
  68.         cmbPalette.SelectedIndex = 4
  69.         '// Set to KryptonDataGridView By changing the Palette according to the main form.
  70.         Me.PaletteMode = Krypton.Toolkit.PaletteMode.Global
  71.     End Sub

  72.     '// ==============================
  73.     ' BPM CONTROL
  74.     '// ==============================
  75.     Private Sub trkBPM_Scroll(sender As Object, e As EventArgs) Handles trkBPM.Scroll
  76.         lblBPM.Text = "BPM : " & trkBPM.Value
  77.         SetTimerInterval()
  78.     End Sub

  79.     Private Sub SetTimerInterval()
  80.         tmrBeat.Interval = CInt(60000 / trkBPM.Value)
  81.     End Sub

  82.     '// ==============================
  83.     ' START / STOP (Toggle)
  84.     '// ==============================
  85.     Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
  86.         If Not isRunning Then
  87.             tmrBeat.Start()
  88.             btnStart.Text = "หยุด"
  89.             isRunning = True
  90.         Else
  91.             tmrBeat.Stop()
  92.             btnStart.Text = "เริ่ม"
  93.             picPendulum.Image = imgLeft
  94.             isRunning = False
  95.         End If
  96.     End Sub

  97.     Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
  98.         Me.Close()
  99.     End Sub

  100.     '// ==============================
  101.     ' METRONOME TICK
  102.     '// ==============================
  103.     Private Sub tmrBeat_Tick(sender As Object, e As EventArgs) Handles tmrBeat.Tick
  104.         ' เล่นเสียง
  105.         player.Stop()
  106.         player.Play()
  107.         ' สลับภาพ
  108.         If isLeft Then
  109.             picPendulum.Image = imgRight
  110.         Else
  111.             picPendulum.Image = imgLeft
  112.         End If
  113.         isLeft = Not isLeft
  114.     End Sub

  115.     '// ==============================
  116.     ' VOLUME CONTROL
  117.     '// ==============================
  118.     Private Sub trkVolume_Scroll(sender As Object, e As EventArgs) Handles trkVolume.Scroll
  119.         SetSystemVolume(trkVolume.Value)
  120.         lblVolume.Text = "Vol : " & trkVolume.Value & "%"
  121.     End Sub

  122.     Private Function GetSystemVolume() As Integer
  123.         Dim vol As Integer
  124.         waveOutGetVolume(0, vol)
  125.         Dim leftVol As Integer = vol And &HFFFF
  126.         Return CInt((leftVol / &HFFFF) * 100)
  127.     End Function

  128.     Private Sub SetSystemVolume(ByVal level As Integer)
  129.         If level < 0 Then level = 0
  130.         If level > 100 Then level = 100

  131.         Dim newVol As Integer = CInt((level / 100.0) * &HFFFF)
  132.         Dim volAll As Integer = (newVol << 16) Or newVol
  133.         waveOutSetVolume(0, volAll)
  134.     End Sub

  135.     ' / --------------------------------------------------------------------------------------------
  136.     ' / Change Palette Mode.
  137.     ' / --------------------------------------------------------------------------------------------
  138.     Private Sub cmbPalette_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbPalette.SelectedIndexChanged
  139.         Dim manager As New KryptonManager()
  140.         Select Case cmbPalette.SelectedIndex
  141.             Case 0
  142.                 manager.GlobalPaletteMode = PaletteModeManager.ProfessionalOffice2003
  143.             Case 1
  144.                 manager.GlobalPaletteMode = PaletteModeManager.Office2007Blue
  145.             Case 2
  146.                 manager.GlobalPaletteMode = PaletteModeManager.Office2007Silver
  147.             Case 3
  148.                 manager.GlobalPaletteMode = PaletteModeManager.Office2007Black
  149.             Case 4
  150.                 manager.GlobalPaletteMode = PaletteModeManager.Office2010Blue
  151.             Case 5
  152.                 manager.GlobalPaletteMode = PaletteModeManager.Office2010Silver
  153.             Case 6
  154.                 manager.GlobalPaletteMode = PaletteModeManager.Office2010Black
  155.         End Select
  156.     End Sub


  157. #Region "UTILITY"
  158.     Private Function MyPath(ByVal AppPath As String) As String
  159.         Dim p = AppPath.ToLower().
  160.             Replace("\bin\debug", "").
  161.             Replace("\bin\release", "").
  162.             Replace("\bin\x86\debug", "").
  163.             Replace("\bin\x86\release", "")

  164.         If Not p.EndsWith("") Then p &= ""
  165.         Return p
  166.     End Function
  167. #End Region

  168. End Class
คัดลอกไปที่คลิปบอร์ด


ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...

ดาวน์โหลด krypton Toolkit 5.55 สำหรับ .Net Framework 4.0 ได้ที่นี่ ...


ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2026-2-18 19:30 , Processed in 0.080481 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้