Kali ini ane lagi coba iseng-iseng buat bikin program di visual basic. Yang gw share kali ini adalah membuat aplikasi reminder (alarm). Awalnya kalian harus install dulu tools yang namanya visual studio 2010. Terserah deh mau express, ultimate, ataupun professional. Mula-mula anda masuk dulu ke tools tersebut, kayak gambar ini nih.
Trus klik aja tuh File -> New -> Project. Lalu kalian pilih Visual basic windows lalu klik Windows Form Application. Kemudian namain projectnya terserah kalian.
Nah karena kita buat aplikasi alarm maka harus ada suara kannnnn. Berarti kita harus buat sebuah kelas yang menjalankan suara tersebut agar bisa diakses. Caranya klik kanan di project trus add -> class. Kalo gw sih gw namain kelasnya mulaisuara.vb. Tampilannya nanti bakal kayak gini.
Nah abis itu masukin deh file suara yang bakal buat bunyi alarm kalian. Filenya .wav. Kalo gw sih pake suara ayam hehe namanya Rooster.wav.
Setelah itu buka kelas mulaisuara.vb trus ketik code ini :
Imports System.IO
Imports System.Reflection
Imports System.Runtime.InteropServices
Public Class mulaisuara
Private Declare Auto Function PlaySound Lib "winmm.dll" ( _
ByVal pszSound As IntPtr, _
ByVal hModule As IntPtr, _
ByVal dwFlags As Int32 _
) As Boolean
Private Const SND_ASYNC As Int32 = &H1
Private Const SND_MEMORY As Int32 = &H4
Private Const SND_LOOP As Int32 = &H8
Private Const SND_PURGE As Int32 = &H40
Private Shared m_hgData As IntPtr
Public Shared Sub Play(ByVal Name As String, ByVal [Loop] As Boolean)
If Not m_hgData.Equals(IntPtr.Zero) Then
StopPlaying()
End If
' get the namespace
Dim strNameSpace As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()
' get the resource into a stream
Dim st As Stream = _
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(strNameSpace + "." + Name)
If st Is Nothing Then Exit Sub
Dim intLength As Integer = CInt(st.Length)
Dim abyt(intLength - 1) As Byte
st.Read(abyt, 0, intLength)
st.Close()
m_hgData = Marshal.AllocHGlobal(intLength)
Marshal.Copy(abyt, 0, m_hgData, intLength)
Dim Flags As Int32 = SND_MEMORY Or SND_ASYNC
If [Loop] Then
Flags = Flags Or SND_LOOP
End If
PlaySound( _
m_hgData, _
IntPtr.Zero, _
Flags _
)
End Sub
Public Shared Sub [Stop]()
If Not m_hgData.Equals(IntPtr.Zero) Then
StopPlaying()
End If
End Sub
Private Shared Sub StopPlaying()
PlaySound(IntPtr.Zero, IntPtr.Zero, SND_PURGE)
Marshal.FreeHGlobal(m_hgData)
m_hgData = IntPtr.Zero
End Sub
End Class
Lalu kalian buka form1.vb yang merupakan tampilan aplikasi anda. Terus kalian buat tampilannya kayak gini.
Spesifikasi toolboxnya kayak gini nih :
Label1 : Text= Set Alarm
Label2 Text= Pesan
Button1 : Name=stopp
Text=STOP!
Button2 : Name=OKbut
Text=OK
DateTimePicker :Name=dtpAlarm
Format=Time
TextBox :Name=pesantb
Timer :Name=Timeralarm
Nah misalkan udah , kalian klik kanan tuh Form1.vb trus klik deh View Code. Disinilah kita bersenang-senang (ngoding) :D.
Ketik aja nih codenya :
Public Class Form1
'property yang digunakan untuk mengambil nilai dari datetimepicker
Public Property waktu_alarm As DateTime
Get
Return dtpAlarm.Value
End Get
Set(ByVal value As DateTime)
dtpAlarm.Value = value
End Set
End Property
''' <summary>
''' '''''''''''''''''''''''cekkkk
''' </summary>
''' <remarks></remarks>
Private Sub RollTime()
If DateTime.Now.TimeOfDay.CompareTo(waktu_alarm.TimeOfDay) > 0 Then
waktu_alarm = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 1, _
waktu_alarm.Hour, waktu_alarm.Minute, waktu_alarm.Second)
Else
waktu_alarm = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, _
waktu_alarm.Hour, waktu_alarm.Minute, waktu_alarm.Second)
End If
End Sub
''' <summary>
''' ''''''''''''''''''''''''''''''''''''''''''''''
''' </summary>
Dim pesannya As String
Private Sub Timeralarm_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesTimeralarm.Tick
If DateTime.Now.CompareTo(waktu_alarm) >= 0 Then
'memastikan apakah alarm berlaku untuk hari ini atau besok
RollTime()
'pesan yang ditampilkan pada balloon tip
'AlarmNotifyIcon.BalloonTipText = IIf(txtMessage.Text.Length > 0, txtMessage.Text, "Attention!")
mulaisuara.Play("Rooster.wav", 2)
stopp.Enabled = True
wait.Text = "Alarm Selesai"
MessageBox.Show(pesannya, "Pesan")
End If
End Sub
''' <summary>
''' '''''''''''''
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub OKbut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKbut.Click
If (pesantb.Text = "") Then
MessageBox.Show("Pesan wajib dimasukan !!!!")
Else
Timeralarm.Enabled = True
MessageBox.Show("Alarm Berhasil Dibuat")
pesannya = pesantb.Text
pesantb.Text = ""
wait.Visible = True
'wait.Text = waktua
wait.Text = "Alarm Telah Dibuat"
End If
End Sub
Dim msg As MsgBoxResult
Private Sub stopp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopp.Click
mulaisuara.Stop()
stopp.Enabled = False
MessageBox.Show("Terima Kasih", "Nice Program :D")
'msg = MessageBox("a", vbYesNo)
Application.Exit()
' Timeralarm.Stop()
' .Timeralarm.Enabled = False
End Sub
Kalo udah silahkan di run deh aplikasinya. Oh iya catatan, aplikasi ini ga run di background jadi aplikasi harus aktif biar bisa berjalan sesuai keinginan.Semoga bermanfaat :D, selamat coding !!! .
Kalo yang mau langsung download source codenya langsung aja klik DOWNLOAD.
0 comments:
Post a Comment
KOMENTAR