Tips kali ini, saya membuat Aplikasi Grafik Performance CPU Usage History seperti pada Windows Task Manager yang berbeda hanya tampilan visualnya. Gambar di atas merupakan Tampilan Visualnya.
Ikuti langkah-langkah di bawah ini. ToolBox yang di gunakan sebagai berikut:
Design Form dari ToolBox di atas seperti gambar di bawah ini:
Ketikkan source program di bawah ini dengan mengarahkan kursor pada design Form, Klik kanan lalu pilih View Code.
Source Program:
Listing program pada class dengan nama CharCPUMemory:
Public Class CharCPUMemory
Public Shared Sub Main()
Application.Run(New frmMain)
End Sub
End Class
Listing program pada design Form:
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class frmMain
Inherits System.Windows.Forms.Form
Dim m_sngX As Single
Dim m_sngY As Single
Dim m_sngCPUY As Single
Dim m_sngProcsY As Single
Dim m_sngMemY As Single
Dim penCPU As New System.Drawing.Pen(Color.Red)
Dim penProcs As New System.Drawing.Pen(Color.Green)
Dim penMem As New System.Drawing.Pen(Color.Blue)
Dim penFore As New System.Drawing.Pen(SystemColors.WindowText)
Dim penBack As New System.Drawing.Pen(SystemColors.Control)
Dim MachineName As String = System.Windows.Forms.SystemInformation.ComputerName
Const INCREMENT As Single = 1
Private Sub tmrClock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrClock.Tick
Dim sngCPU As Single = PrfCPU.NextValue() / 100
Dim sngProcs As Single = PrfProcs.NextValue()
Dim sngMem As Single = PrfMem.NextValue / 1024
REM Gambar pada Panel PictureBox
DrawSweep(sngCPU, sngProcs, sngMem)
REM Updates Nilai pada Label
lblCPU.Text = "CPU: " & sngCPU.ToString("p")
lblProc.Text = "Processes: " & sngProcs
lblMem.Text = "Memory: " & sngMem.ToString("f0") & " KB"
End Sub
Private Sub DrawSweep(ByVal CPU As Single, _
ByVal Processes As Single, _
ByVal Memory As Single)
Dim oGrafix As Graphics = pnlSweep.CreateGraphics()
Dim sngHeight As Single = pnlSweep.Height
REM Nilai untuk CPU, Processor, Memory
Dim sngCPUY As Single
Dim sngProcsY As Single
Dim sngMemY As Single
REM menghapus garis sebelumnya
oGrafix.DrawLine(penBack, m_sngX, 0, m_sngX, sngHeight)
REM plot data nilai
sngCPUY = sngHeight - (CPU * sngHeight) - 1
oGrafix.DrawLine(penCPU, _
m_sngX - INCREMENT, m_sngCPUY, m_sngX, sngCPUY)
m_sngCPUY = sngCPUY
sngProcsY = sngHeight - Processes
oGrafix.DrawLine(penProcs, _
m_sngX - INCREMENT, m_sngProcsY, m_sngX, sngProcsY)
m_sngProcsY = sngProcsY
REM Nilai 10000 merupakan convert nilai memory
REM Nilai ini dapat diganti sesuai dengan besar memory yang dipakai
REM agar pola garis nilai yang di dapat benar
sngMemY = sngHeight - (Memory / 10000)
oGrafix.DrawLine(penMem, _
m_sngX - INCREMENT, m_sngMemY, m_sngX, sngMemY)
m_sngMemY = sngMemY
REM penjumlahan terhadap sumbu horizontal/ bergerak ke arah horizontal
m_sngX += INCREMENT
If m_sngX > pnlSweep.Width Then
REM kembali ke awal
m_sngX = 0
REM menghapus gambar
oGrafix.Clear(SystemColors.Control)
End If
REM menggambar pola baru
oGrafix.DrawLine(penFore, m_sngX, 0, m_sngX, sngHeight)
End Sub
Private Sub cmdSweep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSweep.Click
REM Pengaturan timer (on or off)
If cmdSweep.Text = "&Start" Then
cmdSweep.Text = "&Stop"
tmrClock.Enabled = True
Else
cmdSweep.Text = "&Start"
tmrClock.Enabled = False
End If
End Sub
Private Sub trkSpeed_Scroll1(ByVal sender As Object, ByVal e As System.EventArgs) Handles trkSpeed.Scroll
Dim iValue As Integer
iValue = CInt(trkSpeed.Value)
REM setting pengaturan interval timer pada pola char CPU Memory
tmrClock.Interval = iValue * 100 'ms
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sngCPU As Single = PrfCPU.NextValue() / 100
Dim sngProcs As Single = PrfProcs.NextValue()
Dim sngMem As Single = PrfMem.NextValue / 1024
cmdSweep.Text = "&Start"
lblCPU.Text = "CPU: " & sngCPU.ToString("p")
lblProc.Text = "Processes: " & sngProcs
lblMem.Text = "Memory: " & sngMem.ToString("f0") & " KB"
End Sub
End Class
Source Program:
Listing program pada class dengan nama CharCPUMemory:
Public Class CharCPUMemory
Public Shared Sub Main()
Application.Run(New frmMain)
End Sub
End Class
Listing program pada design Form:
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class frmMain
Inherits System.Windows.Forms.Form
Dim m_sngX As Single
Dim m_sngY As Single
Dim m_sngCPUY As Single
Dim m_sngProcsY As Single
Dim m_sngMemY As Single
Dim penCPU As New System.Drawing.Pen(Color.Red)
Dim penProcs As New System.Drawing.Pen(Color.Green)
Dim penMem As New System.Drawing.Pen(Color.Blue)
Dim penFore As New System.Drawing.Pen(SystemColors.WindowText)
Dim penBack As New System.Drawing.Pen(SystemColors.Control)
Dim MachineName As String = System.Windows.Forms.SystemInformation.ComputerName
Const INCREMENT As Single = 1
Private Sub tmrClock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrClock.Tick
Dim sngCPU As Single = PrfCPU.NextValue() / 100
Dim sngProcs As Single = PrfProcs.NextValue()
Dim sngMem As Single = PrfMem.NextValue / 1024
REM Gambar pada Panel PictureBox
DrawSweep(sngCPU, sngProcs, sngMem)
REM Updates Nilai pada Label
lblCPU.Text = "CPU: " & sngCPU.ToString("p")
lblProc.Text = "Processes: " & sngProcs
lblMem.Text = "Memory: " & sngMem.ToString("f0") & " KB"
End Sub
Private Sub DrawSweep(ByVal CPU As Single, _
ByVal Processes As Single, _
ByVal Memory As Single)
Dim oGrafix As Graphics = pnlSweep.CreateGraphics()
Dim sngHeight As Single = pnlSweep.Height
REM Nilai untuk CPU, Processor, Memory
Dim sngCPUY As Single
Dim sngProcsY As Single
Dim sngMemY As Single
REM menghapus garis sebelumnya
oGrafix.DrawLine(penBack, m_sngX, 0, m_sngX, sngHeight)
REM plot data nilai
sngCPUY = sngHeight - (CPU * sngHeight) - 1
oGrafix.DrawLine(penCPU, _
m_sngX - INCREMENT, m_sngCPUY, m_sngX, sngCPUY)
m_sngCPUY = sngCPUY
sngProcsY = sngHeight - Processes
oGrafix.DrawLine(penProcs, _
m_sngX - INCREMENT, m_sngProcsY, m_sngX, sngProcsY)
m_sngProcsY = sngProcsY
REM Nilai 10000 merupakan convert nilai memory
REM Nilai ini dapat diganti sesuai dengan besar memory yang dipakai
REM agar pola garis nilai yang di dapat benar
sngMemY = sngHeight - (Memory / 10000)
oGrafix.DrawLine(penMem, _
m_sngX - INCREMENT, m_sngMemY, m_sngX, sngMemY)
m_sngMemY = sngMemY
REM penjumlahan terhadap sumbu horizontal/ bergerak ke arah horizontal
m_sngX += INCREMENT
If m_sngX > pnlSweep.Width Then
REM kembali ke awal
m_sngX = 0
REM menghapus gambar
oGrafix.Clear(SystemColors.Control)
End If
REM menggambar pola baru
oGrafix.DrawLine(penFore, m_sngX, 0, m_sngX, sngHeight)
End Sub
Private Sub cmdSweep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSweep.Click
REM Pengaturan timer (on or off)
If cmdSweep.Text = "&Start" Then
cmdSweep.Text = "&Stop"
tmrClock.Enabled = True
Else
cmdSweep.Text = "&Start"
tmrClock.Enabled = False
End If
End Sub
Private Sub trkSpeed_Scroll1(ByVal sender As Object, ByVal e As System.EventArgs) Handles trkSpeed.Scroll
Dim iValue As Integer
iValue = CInt(trkSpeed.Value)
REM setting pengaturan interval timer pada pola char CPU Memory
tmrClock.Interval = iValue * 100 'ms
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sngCPU As Single = PrfCPU.NextValue() / 100
Dim sngProcs As Single = PrfProcs.NextValue()
Dim sngMem As Single = PrfMem.NextValue / 1024
cmdSweep.Text = "&Start"
lblCPU.Text = "CPU: " & sngCPU.ToString("p")
lblProc.Text = "Processes: " & sngProcs
lblMem.Text = "Memory: " & sngMem.ToString("f0") & " KB"
End Sub
End Class
Setelah mengetikkan listing dari source program diatas tekan F5, maka hasil tampilan visualnya seperti gambar di bawah ini. Tekan tombol Start maka tampilan visualnya seperti gambar di atas.
Artikel yang berkaitan yakni Grafik Dynamic Line, Klik Tutorial ini ==> di sini dan di sini
Selamat mencoba Guys! Nantikan Tips Aplikasi Cantik Lainnya by Verynandus Hutabalian
0 komentar:
Posting Komentar