プログラムをタスクトレイ内で実行する方法
VB.NET
Const SW_HIDE = 0 ' ウィンドウを非表示
Const SW_SHOW = 5 ' ウィンドウを表示
Const SW_RESTORE = 9 ' ウィンドウを元の状態に戻す
' ウィンドウ表示/非表示切替
Private Declare Function ShowWindow Lib "user32.dll" _
(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
' ウィンドウ表示/非表示チェック
Private Declare Function IsWindowVisible Lib "user32.dll" _
(ByVal hwnd As Integer) As Integer
' タスクトレイ実行したプログラム
Private exe As System.Diagnostics.Process
Private Sub Form1_Load( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
' アプリケーションのタイトル
NotifyIcon1.Text = "Outlook Express"
' アプリケーションのアイコン
NotifyIcon1.Icon = Me.Icon
' 実行
exe = System.Diagnostics.Process.Start("C:\Program Files\Outlook Express\msimn.exe")
End Sub
Private Sub NotifyIcon1_MouseDown( _
ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles NotifyIcon1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
' マウス左ボタンをクリック
If exe Is Nothing = False AndAlso exe.MainWindowHandle <> 0 Then
If IsWindowVisible(exe.MainWindowHandle) = 0 Then
' ウィンドウ表示
ShowWindow(exe.MainWindowHandle, SW_SHOW)
Else
' ウィンドウ非表示
ShowWindow(exe.MainWindowHandle, SW_HIDE)
End If
End If
End If
End Sub
Private Sub Form1_FormClosed( _
ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) _
Handles MyBase.FormClosed
' タスクトレイアイコン消去
NotifyIcon1.Icon = Nothing
End Sub
ひと目でわかる Visual C# 2013/2012 アプリケーション開発入門 (MSDNプログラミングシリーズ)![]()