Cpu usage

Description

Contrôle permettant de visualiser un graphe représentant l'utilisation du ou des processeurs (pour les applications multi-thread). Par défaut, ce contrôle est la réplique du CPU Usage History du Task Manager de windows mais il est entièrement paramétrable : Couleur de fond, couleur du quadrillage, couleur du graphe, taille du quadrillage....

Pour l?utiliser, mettez n'importe quel numéro dans le champ « serial ». Ca ne sert à rien?mais c?est comme ça. :-)

N?hésitez pas à visiter mon site qui est en cours de construction : [URL="http://www.need-soft.com"]http://www.need-soft.com/URL

Stephane DEFAYE

Source / Exemple :


Option Strict On

Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization

<ToolboxBitmap(GetType(GrahProc), "GrahProc.bmp")> _
Public Class GrahProc
    Inherits System.Windows.Forms.UserControl

#Region "Declaration"

    Dim _Graph_BackColor As Color = Color.Black
    Dim _Graph_GridColor As Color = Color.SeaGreen
    Dim _Graph_GridColorPen As Pen = Pens.SeaGreen
    Dim _Graph_GraphColor As Color = Color.Lime
    Dim _Graph_GraphColorPen As Pen = Pens.Lime
    Dim _Graph_BorderStyle As BorderStyle = Windows.Forms.BorderStyle.FixedSingle
    Dim _Graph_Start As Boolean
    Dim _Graph_Width As Integer = 100
    Dim _Graph_Height As Integer = 24
    Dim _Graph_Interval As Integer = 1000
    Dim _Graph_QuaterSize As Short = 12
    Dim _Graph_Licence As Boolean = True
    Dim _Graph_Serial As String = CStr(My.Computer.Registry.GetValue _
                                                                ("HKEY_LOCAL_MACHINE\SOFTWARE\Need-Soft\GraphProc", _
                                                                "Serial", Nothing))
    Dim Var_Points() As Point 'tableau qui contient les coordonées du graph
    Dim Yy As Integer = 0 'variable pour gérer le décalage du quadrillage
    Dim frmAbout_Form As New frmAbout

    Dim Var_Process As System.Diagnostics.Process
    Dim Var_TmpProc01 As String = "0"
    Dim Var_TmpProc02 As String
    Dim Var_TmpMach1 As String = "0"
    Dim Var_TmpMach2 As String
    Dim Var_PercentProc As Integer 'valeur en % d'utilisation du processeur

    Dim Var_CountProc As Integer = Environment.ProcessorCount 'nombre de processeur(s)
    Dim Var_Tag1 As Short = 0

#End Region

#Region "Propiété"

    Public Property Graph_BorderStyle() As BorderStyle

        Get
            Return _Graph_BorderStyle
        End Get
        Set(ByVal value As BorderStyle)
            Select Case value
                Case Windows.Forms.BorderStyle.Fixed3D
                    Me.PnlProc.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
                    _Graph_BorderStyle = Windows.Forms.BorderStyle.Fixed3D
                Case Windows.Forms.BorderStyle.FixedSingle
                    Me.PnlProc.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
                    _Graph_BorderStyle = Windows.Forms.BorderStyle.FixedSingle
                Case Windows.Forms.BorderStyle.None
                    Me.PnlProc.BorderStyle = Windows.Forms.BorderStyle.None
                    _Graph_BorderStyle = Windows.Forms.BorderStyle.None
            End Select
        End Set
    End Property

    Public Property Graph_BackColor() As Color
        Get
            Return _Graph_BackColor
        End Get
        Set(ByVal value As Color)
            _Graph_BackColor = value
            PnlProc.BackColor = value
        End Set
    End Property

    Public Property Graph_GridColor() As Color
        Get
            Return _Graph_GridColor
        End Get
        Set(ByVal value As Color)
            _Graph_GridColor = value
            _Graph_GridColorPen = New Pen(_Graph_GridColor)
        End Set
    End Property

    Public Property Graph_GraphColor() As Color
        Get
            Return _Graph_GraphColor
        End Get
        Set(ByVal value As Color)
            _Graph_GraphColor = value
            _Graph_GraphColorPen = New Pen(_Graph_GraphColor)
        End Set
    End Property

    Public Property Graph_Interval() As Integer
        Get
            Return _Graph_Interval
        End Get
        Set(ByVal Value As Integer)
            _Graph_Interval = Value

            If _Graph_Interval > 60000 Then
                _Graph_Interval = 60000
                MsgBox("The value must be less 60000.", MsgBoxStyle.Information, "Need-Soft")
            ElseIf _Graph_Interval < 100 Then
                _Graph_Interval = 100
                MsgBox("The value must be more 99.", MsgBoxStyle.Information, "Need-Soft")
            End If
            Timer1.Interval = Value
        End Set
    End Property

    Public Property Graph_QuaterSize() As Short
        Get
            If _Graph_QuaterSize <= 3 Then
                _Graph_QuaterSize = 3
            End If
            Return _Graph_QuaterSize
        End Get
        Set(ByVal Value As Short)
            _Graph_QuaterSize = Value
        End Set
    End Property

    Public Property Graph_Start() As Boolean
        Get
            Return _Graph_Start
        End Get
        Set(ByVal value As Boolean)
            Me.Timer1.Enabled = value
            If value = False Then S_PnlClear()
        End Set
    End Property

#End Region

#Region "Function"

    Private Sub GraphProc_Resize(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles Me.Resize
        Init_GraphProc()

    End Sub ' Contrôle au redimensionnement du contrôle

    Protected Sub Init_GraphProc()

        Me.PnlProc.Height = Me.Height 'on ajuste la taille 
        Me.PnlProc.Width = Me.Width 'du panel à la taille du controle
        _Graph_Width = Me.Width
        _Graph_Height = Me.Height

        Try
            Var_Process = System.Diagnostics.Process.GetCurrentProcess

        Catch excep As Exception
            Exit Try
        End Try

        If _Graph_BorderStyle = Windows.Forms.BorderStyle.Fixed3D Then 'la gargeur du graph varie 
            _Graph_Width = _Graph_Width - 3 'en fonction du style de la bordure
            _Graph_Height = _Graph_Height - 3
        ElseIf Graph_BorderStyle = Windows.Forms.BorderStyle.FixedSingle Then
            _Graph_Width = _Graph_Width - 1
            _Graph_Height = _Graph_Height - 1
        End If

        PnlProc.BackColor = _Graph_BackColor
        ReDim Var_Points(_Graph_Width)
        For u As Integer = 0 To _Graph_Width 'initialisation du tableau des coordonnées du graph
            Var_Points(u).X = (u - 1)
            Var_Points(u).Y = _Graph_Height
        Next
        _Graph_Serial = CStr(My.Computer.Registry.GetValue _
                                                                     ("HKEY_LOCAL_MACHINE\SOFTWARE\Need-Soft\GraphProc", _
                                                                     "Serial", Nothing))

        If Me._Graph_Serial = Nothing Then
            Me.frmAbout_Form.ShowDialog()
        End If

    End Sub 'initialise la taille du graph et la couleur de fond

    Protected Sub S_Pnl()

        Try
            Dim Graphic As System.Drawing.Graphics = PnlProc.CreateGraphics
            Dim I, Y, X As Integer

            Yy = Yy - 1
            If Yy < -_Graph_QuaterSize Then Yy = -1
            Y = Yy
            X = -1
            Graphic.Clear(_Graph_BackColor) ' on efface le panel
            For I = 0 To CInt(_Graph_Height / _Graph_QuaterSize) ' boucle pour tracer les traits verticaux
                X = X + _Graph_QuaterSize
                If X > _Graph_Height Then Exit For
                Graphic.DrawLine(_Graph_GridColorPen, 1, X, _Graph_Width, X)
            Next

            For I = 0 To CInt(_Graph_Width / (_Graph_QuaterSize)) ' boucle pour tracer les traits horizontaux
                Y = Y + _Graph_QuaterSize
                If Y > _Graph_Width Then Exit For
                Graphic.DrawLine(_Graph_GridColorPen, Y, 0, Y, _Graph_Height)
            Next
            For u As Integer = 0 To (_Graph_Width - 1) 'boucle pour décaler le graph de 1px sur la gauche
                Var_Points(u) = Var_Points(u + 1)
                Var_Points(u).X = (u - 1)
            Next u
            Var_Points((_Graph_Width - 1)).Y = CInt((100 - Func_Proc()) / 100 * _Graph_Height)
            Var_Points((_Graph_Width - 1)).X = _Graph_Width
            Graphic.DrawLines(_Graph_GraphColorPen, Var_Points)

            Graphic.Dispose()
        Catch excep As Exception
            Exit Sub
        End Try

    End Sub 'dessine le graph complet, fond, quadrillage et graph

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

        S_Pnl()

    End Sub 'timer, marche arret, intervalle

    Protected Sub S_PnlClear()

        Try
            Dim Graphic As System.Drawing.Graphics = PnlProc.CreateGraphics
            Dim I, Y, X As Integer

            Yy = Yy - 1
            If Yy < -_Graph_QuaterSize Then Yy = -1
            Y = Yy
            X = -1
            Graphic.Clear(_Graph_BackColor) ' on efface le panel
            For I = 0 To CInt(_Graph_Height / _Graph_QuaterSize) ' boucle pour tracer les traits verticaux
                X = X + _Graph_QuaterSize
                If X > _Graph_Height Then Exit For
                Graphic.DrawLine(_Graph_GridColorPen, 1, X, _Graph_Width, X)
            Next

            For I = 0 To CInt(_Graph_Width / (_Graph_QuaterSize)) ' boucle pour tracer les traits horizontaux
                Y = Y + _Graph_QuaterSize
                If Y > _Graph_Width Then Exit For
                Graphic.DrawLine(_Graph_GridColorPen, Y, 0, Y, _Graph_Height)
            Next
            Graphic.Dispose()

        Catch excep As Exception
            Exit Sub
        End Try
    End Sub 'a l'arret du timer on redessine le graph avec le fond et le quadrillage

    Function Func_Proc() As Integer
        Try
            Var_TmpProc02 = Split(Var_Process.TotalProcessorTime.ToString, ":")(2)
            Var_TmpProc02 = CStr(CDbl((Split(Var_TmpProc02, ".")(0) & Microsoft.VisualBasic.Left(Split(Var_TmpProc02, ".")(1), 3))) / Var_CountProc)
            Var_TmpMach2 = Environment.TickCount.ToString
            If Var_TmpMach1 = Var_TmpMach2 Then
                Return Var_PercentProc
                Exit Function
            End If
            Var_PercentProc = CInt(Math.Round(((CDbl(Var_TmpProc02) - CDbl(Var_TmpProc01)) / (CDbl(Var_TmpMach2) - CDbl(Var_TmpMach1))) * 100))

            Var_TmpProc01 = Var_TmpProc02
            Var_TmpMach1 = Var_TmpMach2
            If Var_PercentProc < 0 Then
                Var_PercentProc = 0
            ElseIf Var_PercentProc > 100 Then
                Var_PercentProc = 100

            End If
            Return Var_PercentProc
        Catch excep As Exception
            Return Var_PercentProc
        End Try
    End Function

#End Region

    Private Sub PnlProc_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PnlProc.DoubleClick

        Me.frmAbout_Form.ShowDialog()

    End Sub

End Class

Conclusion :


Vous trouverez dans le zip un formulaire de test.
Je pense que le code est pas mal commenté, mais n'hésitez pas à me solliciter pour toutes questions ou problèmes.

L'idée m'est venue d'une source de ce site :
http://www.vbfrance.com/codes/DIRECTORYCLONE-V2-SYNCHRONISEZ-VOS-DOSSIER-AUTOMATIQUEMENT_36786.aspx.

Ah! C'est ici que je devais mentionner mon site, alors, je répète :

N?hésitez pas à visiter mon site qui est en cours de construction : [URL="http://www.need-soft.com"]http://www.need-soft.com/URL

Stephane DEFAYE

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (s.defaye)