|
既存プログラムをタスクトレイで実行するサンプル(VB.NET)

|
<このサンプルの概要>
既存プログラム(アプリケーションEXE)をタスクトレイで実行するサンプル(VB.NET)です。
VB.NETでタスクトレイにアイコンを表示する方法は非常に簡単です。
NotifyIconをFromに貼り付けて、NotifyIcon.Iconにアイコン画像を設定するだけです。
このサンプルではOutlook Expressをタスクトレイで実行しています。
また、タスクトレイのアイコンをクリックした時にウィンドウの表示/非表示の切替をしています。
このサンプルを元に作成したツールがあります。タスクトレイ実行ツールは、いろんなプログラム
(Outlook Express、Internet Explorer...)をタスクトレイで実行するツールです。
プログラムだけでは無くいろんなファイル(ショートカットでも)を実行します。
残念ながら、一部タスクトレイに入らないファイルもありますが、実用上問題ないと思います。
★フォームモジュール(Form1.vb)
Public Class Form1
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
' タスクトレイで実行したプログラム(Outlook Express)
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
' アプリケーションを実行(Outlook Express)
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
End Class