'//----------------------------------------------------------------------
'//【プロセス アナライザ】- ProcessAnalyzer
'//
'//
'//                                                       by Masato Hirai         
'//----------------------------------------------------------------------
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Diagnostics
Imports GeniusHirai.ColorfulControls

Public Class Form1 : Inherits Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    'Windows フォーム デザイナを使って変更してください。  
    ' コード エディタを使って変更しないでください。
  Friend WithEvents RichLabel1 As GeniusHirai.ColorfulControls.RichLabel
  Friend WithEvents cboFileName As System.Windows.Forms.ComboBox
  Friend WithEvents lblFileName As GeniusHirai.ColorfulControls.RichLabel
  Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents Label3 As System.Windows.Forms.Label
  Friend WithEvents Label4 As System.Windows.Forms.Label
  Friend WithEvents Label5 As System.Windows.Forms.Label
  Friend WithEvents cboPriority As System.Windows.Forms.ComboBox
  Friend WithEvents lblStartupTime As GeniusHirai.ColorfulControls.NumericLabel
  Friend WithEvents lblThreads As GeniusHirai.ColorfulControls.NumericLabel
  Friend WithEvents lblWorkingSet As GeniusHirai.ColorfulControls.NumericLabel
  Friend WithEvents lblProcessorTime As GeniusHirai.ColorfulControls.NumericLabel
  Friend WithEvents Timer1 As System.Windows.Forms.Timer
  Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
  Friend WithEvents btnFile As System.Windows.Forms.Button
  Friend WithEvents Button1 As GeniusHirai.ColorfulControls.OvalButton
  Friend WithEvents CheckBox1 As GeniusHirai.ColorfulControls.BigCheckBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.RichLabel1 = New GeniusHirai.ColorfulControls.RichLabel
Me.cboFileName = New System.Windows.Forms.ComboBox
Me.lblFileName = New GeniusHirai.ColorfulControls.RichLabel
Me.btnFile = New System.Windows.Forms.Button
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.Button1 = New GeniusHirai.ColorfulControls.OvalButton
Me.CheckBox1 = New GeniusHirai.ColorfulControls.BigCheckBox
Me.cboPriority = New System.Windows.Forms.ComboBox
Me.Label1 = New System.Windows.Forms.Label
Me.GroupBox2 = New System.Windows.Forms.GroupBox
Me.lblProcessorTime = New GeniusHirai.ColorfulControls.NumericLabel
Me.Label5 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.lblWorkingSet = New GeniusHirai.ColorfulControls.NumericLabel
Me.lblThreads = New GeniusHirai.ColorfulControls.NumericLabel
Me.lblStartupTime = New GeniusHirai.ColorfulControls.NumericLabel
Me.Label2 = New System.Windows.Forms.Label
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.GroupBox1.SuspendLayout()
Me.GroupBox2.SuspendLayout()
Me.SuspendLayout()
'
'RichLabel1
'
Me.RichLabel1.AutoSize = True
Me.RichLabel1.Font = New System.Drawing.Font("MS UI Gothic", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.RichLabel1.Location = New System.Drawing.Point(8, 24)
Me.RichLabel1.Name = "RichLabel1"
Me.RichLabel1.Size = New System.Drawing.Size(110, 27)
Me.RichLabel1.TabIndex = 0
Me.RichLabel1.Text = "ターゲット："
'
'cboFileName
'
Me.cboFileName.AllowDrop = True
Me.cboFileName.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
            Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.cboFileName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cboFileName.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.cboFileName.Location = New System.Drawing.Point(8, 72)
Me.cboFileName.Name = "cboFileName"
Me.cboFileName.Size = New System.Drawing.Size(568, 24)
Me.cboFileName.TabIndex = 1
'
'lblFileName
'
Me.lblFileName.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
            Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.lblFileName.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(192, Byte), CType(255, Byte))
Me.lblFileName.BorderColor = System.Drawing.Color.Aqua
Me.lblFileName.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.lblFileName.Effect = GeniusHirai.ColorfulControls.RichLabel.EffectConstant.Fill
Me.lblFileName.Font = New System.Drawing.Font("Arial Black", 26.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblFileName.ForeColor = System.Drawing.Color.Blue
Me.lblFileName.Location = New System.Drawing.Point(120, 8)
Me.lblFileName.Name = "lblFileName"
Me.lblFileName.Size = New System.Drawing.Size(504, 56)
Me.lblFileName.TabIndex = 3
Me.lblFileName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'btnFile
'
Me.btnFile.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnFile.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.btnFile.Location = New System.Drawing.Point(584, 72)
Me.btnFile.Name = "btnFile"
Me.btnFile.Size = New System.Drawing.Size(40, 24)
Me.btnFile.TabIndex = 4
Me.btnFile.Text = "..."
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.Button1)
Me.GroupBox1.Controls.Add(Me.CheckBox1)
Me.GroupBox1.Controls.Add(Me.cboPriority)
Me.GroupBox1.Controls.Add(Me.Label1)
Me.GroupBox1.Font = New System.Drawing.Font("MS UI Gothic", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.GroupBox1.Location = New System.Drawing.Point(16, 112)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(200, 264)
Me.GroupBox1.TabIndex = 5
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "スタートアップ"
'
'Button1
'
Me.Button1.FaceColor = GeniusHirai.ColorfulControls.OvalButton.FaceColorConstant.Gray
Me.Button1.Location = New System.Drawing.Point(8, 176)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(176, 72)
Me.Button1.TabIndex = 3
Me.Button1.Text = "起動"
'
'CheckBox1
'
Me.CheckBox1.Checked = True
Me.CheckBox1.CheckState = System.Windows.Forms.CheckState.Checked
Me.CheckBox1.Location = New System.Drawing.Point(8, 120)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.Size = New System.Drawing.Size(176, 32)
Me.CheckBox1.TabIndex = 2
Me.CheckBox1.Text = "PriorityBoost"
'
'cboPriority
'
Me.cboPriority.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cboPriority.Location = New System.Drawing.Point(8, 72)
Me.cboPriority.Name = "cboPriority"
Me.cboPriority.Size = New System.Drawing.Size(184, 32)
Me.cboPriority.TabIndex = 1
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(8, 40)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(146, 27)
Me.Label1.TabIndex = 0
Me.Label1.Text = "PriorityClass:"
'
'GroupBox2
'
Me.GroupBox2.Controls.Add(Me.lblProcessorTime)
Me.GroupBox2.Controls.Add(Me.Label5)
Me.GroupBox2.Controls.Add(Me.Label4)
Me.GroupBox2.Controls.Add(Me.Label3)
Me.GroupBox2.Controls.Add(Me.lblWorkingSet)
Me.GroupBox2.Controls.Add(Me.lblThreads)
Me.GroupBox2.Controls.Add(Me.lblStartupTime)
Me.GroupBox2.Controls.Add(Me.Label2)
Me.GroupBox2.Font = New System.Drawing.Font("MS UI Gothic", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.GroupBox2.Location = New System.Drawing.Point(224, 112)
Me.GroupBox2.Name = "GroupBox2"
Me.GroupBox2.Size = New System.Drawing.Size(400, 264)
Me.GroupBox2.TabIndex = 7
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "情報"
'
'lblProcessorTime
'
Me.lblProcessorTime.BackColor = System.Drawing.Color.Black
Me.lblProcessorTime.ForeColor = System.Drawing.Color.Yellow
Me.lblProcessorTime.Location = New System.Drawing.Point(232, 104)
Me.lblProcessorTime.Name = "lblProcessorTime"
Me.lblProcessorTime.Size = New System.Drawing.Size(157, 43)
Me.lblProcessorTime.TabIndex = 7
Me.lblProcessorTime.Text = "NumericLabel4"
Me.lblProcessorTime.UpperPoint = 7
Me.lblProcessorTime.ZoomMode = GeniusHirai.ColorfulControls.NumericLabel.ZoomModes.SuperMini
'
'Label5
'
Me.Label5.AutoSize = True
Me.Label5.Location = New System.Drawing.Point(16, 112)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(199, 27)
Me.Label5.TabIndex = 6
Me.Label5.Text = "プロセッサタイム(ms)"
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(16, 208)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(202, 27)
Me.Label4.TabIndex = 5
Me.Label4.Text = "ワーキングセット(KB)"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(16, 160)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(155, 27)
Me.Label3.TabIndex = 4
Me.Label3.Text = "スレッドカウント:"
'
'lblWorkingSet
'
Me.lblWorkingSet.BackColor = System.Drawing.Color.Black
Me.lblWorkingSet.ForeColor = System.Drawing.Color.Yellow
Me.lblWorkingSet.Location = New System.Drawing.Point(232, 200)
Me.lblWorkingSet.Name = "lblWorkingSet"
Me.lblWorkingSet.Size = New System.Drawing.Size(157, 43)
Me.lblWorkingSet.TabIndex = 3
Me.lblWorkingSet.Text = "NumericLabel3"
Me.lblWorkingSet.UpperPoint = 7
Me.lblWorkingSet.ZoomMode = GeniusHirai.ColorfulControls.NumericLabel.ZoomModes.SuperMini
'
'lblThreads
'
Me.lblThreads.BackColor = System.Drawing.Color.Black
Me.lblThreads.ForeColor = System.Drawing.Color.Yellow
Me.lblThreads.Location = New System.Drawing.Point(317, 152)
Me.lblThreads.Name = "lblThreads"
Me.lblThreads.Size = New System.Drawing.Size(72, 43)
Me.lblThreads.TabIndex = 3
Me.lblThreads.Text = "NumericLabel2"
Me.lblThreads.UpperPoint = 3
Me.lblThreads.ZoomMode = GeniusHirai.ColorfulControls.NumericLabel.ZoomModes.SuperMini
'
'lblStartupTime
'
Me.lblStartupTime.BackColor = System.Drawing.Color.Black
Me.lblStartupTime.ForeColor = System.Drawing.Color.Yellow
Me.lblStartupTime.Location = New System.Drawing.Point(217, 32)
Me.lblStartupTime.Name = "lblStartupTime"
Me.lblStartupTime.Size = New System.Drawing.Size(172, 63)
Me.lblStartupTime.TabIndex = 1
Me.lblStartupTime.Text = "NumericLabel1"
Me.lblStartupTime.UpperPoint = 5
Me.lblStartupTime.ZoomMode = GeniusHirai.ColorfulControls.NumericLabel.ZoomModes.Mini
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(8, 40)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(164, 27)
Me.Label2.TabIndex = 0
Me.Label2.Text = "起動時間(ms)："
'
'Timer1
'
Me.Timer1.Interval = 250
'
'OpenFileDialog1
'
Me.OpenFileDialog1.Filter = "実行ファイル(*.exe)|*.EXE"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(632, 382)
Me.Controls.Add(Me.GroupBox2)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.btnFile)
Me.Controls.Add(Me.lblFileName)
Me.Controls.Add(Me.RichLabel1)
Me.Controls.Add(Me.cboFileName)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "プロセス アナライザ"
Me.TopMost = True
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox2.ResumeLayout(False)
Me.ResumeLayout(False)

    End Sub

#End Region

  Dim p As Process

  '//
  '// スタートアップ
  '//
  Public Shared Sub Main()
    Application.EnableVisualStyles()
    Application.Run(New Form1)
  End Sub
  '//
  '// Form1_Loadイベント
  '//
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    cboPriority.Items.AddRange([Enum].GetNames(GetType(ProcessPriorityClass)))
    cboPriority.SelectedIndex = 0
  End Sub
  '//
  '// ターゲット アプリケーションの選択
  '//
  Private Sub btnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
      cboFileName.Items.Add(OpenFileDialog1.FileName)
      cboFileName.SelectedIndex = cboFileName.Items.Count - 1
    End If
  End Sub
  '//
  '// ターゲット アプリケーションの起動
  '//
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Button1.FaceColor = OvalButton.FaceColorConstant.Green
    Button1.Text = "起動中.."
    Me.Refresh()
    p = New Process
    p.StartInfo.FileName = cboFileName.SelectedItem
    Dim t As Integer = Environment.TickCount
    p.Start()
    p.PriorityClass = [Enum].Parse(GetType(ProcessPriorityClass), cboPriority.SelectedItem)
    p.PriorityBoostEnabled = CheckBox1.Checked
    p.WaitForInputIdle(60000)
    Me.Timer1.Enabled = True
    lblStartupTime.Value = Environment.TickCount - t
    lblThreads.Value = p.Threads.Count
    lblWorkingSet.Value = p.WorkingSet \ 1024
    lblProcessorTime.Value = p.TotalProcessorTime.Milliseconds
  End Sub
  '//
  '// ターゲットプロセスの状態監視
  '//
  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If p.HasExited Then
      Timer1.Enabled = False
      lblStartupTime.Value = 0
      lblThreads.Value = 0
      lblWorkingSet.Value = 0
      lblProcessorTime.Value = 0
      Button1.FaceColor = OvalButton.FaceColorConstant.Gray
      Button1.Text = "起動"
    Else
      p.Refresh()
      lblThreads.Value = p.Threads.Count
      lblWorkingSet.Value = p.WorkingSet \ 1024
      lblProcessorTime.Value = p.TotalProcessorTime.Milliseconds
    End If
  End Sub
  '//
  '// ComboBoxへのドラッグ＆ドロップ操作
  '//
  Private Sub cboFileName_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles cboFileName.DragEnter
    If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
      e.Effect = DragDropEffects.Copy
    Else
      e.Effect = DragDropEffects.None
    End If
  End Sub
  Private Sub cboFileName_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles cboFileName.DragDrop
    Dim fileName() As String = _
        e.Data.GetData(DataFormats.FileDrop, False)
    cboFileName.Items.AddRange(fileName)
    cboFileName.SelectedIndex = cboFileName.Items.Count - 1
  End Sub

  Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Application.Exit()
  End Sub
  Private Sub cboFileName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFileName.SelectedIndexChanged
    Dim s() As String = cboFileName.SelectedItem.ToString().Split("\"c)
    lblFileName.Text = s(s.Length - 1)
  End Sub

End Class
