www.masahiko.info
IT+
www.masahiko.info
デベロッパー、ITプロ向けの技術情報Webマガジン。アイティプラス(IT Plus)。
IT+
▼個別記事ページ

2004/11/01 02:19 【カテゴリ:.NET Tips & Tricks】 (更新:2006/04/08 23:26)

.NETでタスクトレイのプロパティ表示などのシェル操作を行うには? このエントリーを含むはてなブックマーク

[タスク バーと[スタート]メニューのプロパティ]ダイアログや、[名前を指定して実行]ダイアログを表示するには、Windowsシェルのコマンドを実行する必要があります。.NETのWindowsアプリケーションで、このシェル操作を行う方法はいくつか考えられますが、このエントリーでは、次の2つの方法を紹介します。

1. Windowsシェル機能のCOMオブジェクト「Shell.Application」を利用した方法
2. shell32.dllを使った方法

1の方法では、Windowsシェル機能を操作できるCOMオブジェクト「Shell.Application」を利用します。ただし、Windows NT 4.0(や、.NETでは使えないがWindows 95)でこのオブジェクトを使うには、IEやサービスパックのインストールなどが必須だと思われます(.NETがインストールされている環境ならIEも入っているので大丈夫かも?)。よって、この方法は環境によっては正しく動作しないかもしれません。

2の方法では、「shell32.dllへの参照」が必要になります。Visual Studioでこの参照を追加するには、次の2つの手順を実行してください。
(1) ソリューション エクスプローラで「C:\WINDOWS\system32\shell32.dll」の参照を追加する
(2) ソース ファイルの先頭に「using Shell32」(C#の場合。VB.NETは「Imports Shell32」)を追加する

実際のコードを以下に示します。

【1のコード】

// C#
private void button1_Click(object sender, System.EventArgs e)
{
  // 1. Windowsシェル機能のCOMオブジェクト「Shell.Application」を利用した方法

  // COMのプログラムIDから型を取得する
  Type typeShell = Type.GetTypeFromProgID("Shell.Application");
  // Activatorを使って型からオブジェクトを作成する
  object objShell = Activator.CreateInstance(typeShell);

  switch (this.comboBox1.SelectedItem.ToString())
  {
    case" タスクトレイのプロパティを呼び出す(TrayProperties)":
      typeShell.InvokeMember("TrayProperties", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
      break;
    case" 日付と時刻のプロパティ(SetTime)":
      typeShell.InvokeMember("SetTime", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
      break;
    case" 全ウィンドウを最小化(MinimizeAll)":
      typeShell.InvokeMember("MinimizeAll", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
      break;
    case" 元に戻す - すべて最小化(UndoMinimizeALL)":
      typeShell.InvokeMember("UndoMinimizeALL", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
      break;
    case" 重ねて表示(CascadeWindows)":
      typeShell.InvokeMember("CascadeWindows", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
      break;
    case" 上下に並べて表示(TileHorizontally)":
      typeShell.InvokeMember("TileHorizontally", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
      break;
    case" 左右に並べて表示(TileVertically)":
      typeShell.InvokeMember("TileVertically", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
      break;
    case" コントロールパネルの項目<例:desk.cpl>(ControlPanelItem)":
    {
      Object [] ooo = {"desk.cpl"};
      typeShell.InvokeMember("ControlPanelItem", System.Reflection.BindingFlags.InvokeMethod, null, objShell, ooo);
      break;
    }
    case" エクスプローラで開く(Explore)":
    {
      Object [] ooo = {"C:\\"};
      typeShell.InvokeMember("Explore", System.Reflection.BindingFlags.InvokeMethod, null, objShell, ooo);
      break;
    }
    case" 名前を指定して実行(FileRun)":
      typeShell.InvokeMember("FileRun", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
      break;
    case" コンピュータの検索(FindComputer)":
      typeShell.InvokeMember("FindComputer", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
      break;
    case" 検索(FindFiles)":
      typeShell.InvokeMember("FindFiles", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
      break;
    case" Windowsヘルプ(Help)":
      typeShell.InvokeMember("Help", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
      break;
    case" コンピュータの電源を切る画面(ShutdownWindows)":
      typeShell.InvokeMember("ShutdownWindows", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
      break;
  }
}


' VB.NET
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
  ' 1. Windowsシェル機能のCOMオブジェクト「Shell.Application」を利用した方法

  ' COMのプログラムIDから型を取得する
  Dim typeShell As Type = Type.GetTypeFromProgID("Shell.Application")
  ' Activatorを使って型からオブジェクトを作成する
  Dim objShell As Object = Activator.CreateInstance(typeShell)

  Select Case Me.comboBox1.SelectedItem.ToString()
    Case" タスクトレイのプロパティを呼び出す(TrayProperties)"
      typeShell.InvokeMember("TrayProperties", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, Nothing)

    Case" 日付と時刻のプロパティ(SetTime)"
      typeShell.InvokeMember("SetTime", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, Nothing)

    Case" 全ウィンドウを最小化(MinimizeAll)"
      typeShell.InvokeMember("MinimizeAll", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, Nothing)

    Case" 元に戻す - すべて最小化(UndoMinimizeALL)"
      typeShell.InvokeMember("UndoMinimizeALL", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, Nothing)

    Case" 重ねて表示(CascadeWindows)"
      typeShell.InvokeMember("CascadeWindows", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, Nothing)

    Case" 上下に並べて表示(TileHorizontally)"
      typeShell.InvokeMember("TileHorizontally", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, Nothing)

    Case" 左右に並べて表示(TileVertically)"
      typeShell.InvokeMember("TileVertically", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, Nothing)

    Case" コントロールパネルの項目<例:desk.cpl>(ControlPanelItem)"
      Dim ooo() As Object = {"desk.cpl"}
      typeShell.InvokeMember("ControlPanelItem", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, ooo)

    Case" エクスプローラで開く(Explore)"
      Dim ooo() As Object = {"C:\"}
      typeShell.InvokeMember("Explore", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, ooo)

    Case" 名前を指定して実行(FileRun)"
      typeShell.InvokeMember("FileRun", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, Nothing)

    Case" コンピュータの検索(FindComputer)"
      typeShell.InvokeMember("FindComputer", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, Nothing)

    Case" 検索(FindFiles)"
      typeShell.InvokeMember("FindFiles", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, Nothing)

    Case" Windowsヘルプ(Help)"
      typeShell.InvokeMember("Help", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, Nothing)

    Case" コンピュータの電源を切る画面(ShutdownWindows)"
      typeShell.InvokeMember("ShutdownWindows", System.Reflection.BindingFlags.InvokeMethod, Nothing, objShell, Nothing)

  End Select
End Sub

【2のコード】

// C#
private void button2_Click(object sender, System.EventArgs e)
{
  // 2. shell32.dllを使った方法
  ShellClass shellobject = new ShellClass();

  switch (this.comboBox1.SelectedItem.ToString())
  {
    case" タスクトレイのプロパティを呼び出す(TrayProperties)":
      shellobject.TrayProperties();
      break;
    case" 日付と時刻のプロパティ(SetTime)":
      shellobject.SetTime();
      break;
    case" 全ウィンドウを最小化(MinimizeAll)":
      shellobject.MinimizeAll();
      break;
    case" 元に戻す - すべて最小化(UndoMinimizeALL)":
      shellobject.UndoMinimizeALL();
      break;
    case" 重ねて表示(CascadeWindows)":
      shellobject.CascadeWindows();
      break;
    case" 上下に並べて表示(TileHorizontally)":
      shellobject.TileHorizontally();
      break;
    case" 左右に並べて表示(TileVertically)":
      shellobject.TileVertically();
      break;
    case" コントロールパネルの項目<例:desk.cpl>(ControlPanelItem)":
      shellobject.ControlPanelItem("desk.cpl");
      break;
    case" エクスプローラで開く(Explore)":
      shellobject.Explore("C:\\");
      break;
    case" 名前を指定して実行(FileRun)":
      shellobject.FileRun();
      break;
    case" コンピュータの検索(FindComputer)":
      shellobject.FindComputer();
      break;
    case" 検索(FindFiles)":
      shellobject.FindFiles();
      break;
    case" Windowsヘルプ(Help)":
      shellobject.Help();
      break;
    case" コンピュータの電源を切る画面(ShutdownWindows)":
      shellobject.ShutdownWindows();
      break;
  }
}


' VB.NET
Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
  ' 2. shell32.dllを使った方法

  Dim shellobject As ShellClass = New ShellClass()

  Select Case Me.comboBox1.SelectedItem.ToString()
    Case" タスクトレイのプロパティを呼び出す(TrayProperties)"
      shellObject.TrayProperties()

    Case" 日付と時刻のプロパティ(SetTime)"
      shellObject.SetTime()

    Case" 全ウィンドウを最小化(MinimizeAll)"
      shellObject.MinimizeAll()

    Case" 元に戻す - すべて最小化(UndoMinimizeALL)"
      shellObject.UndoMinimizeALL()

    Case" 重ねて表示(CascadeWindows)"
      shellObject.CascadeWindows()

    Case" 上下に並べて表示(TileHorizontally)"
      shellObject.TileHorizontally()

    Case" 左右に並べて表示(TileVertically)"
      shellObject.TileVertically()

    Case" コントロールパネルの項目<例:desk.cpl>(ControlPanelItem)"
      shellObject.ControlPanelItem("desk.cpl")

    Case" エクスプローラで開く(Explore)"
      shellObject.Explore("C:\")

    Case" 名前を指定して実行(FileRun)"
      shellObject.FileRun()

    Case" コンピュータの検索(FindComputer)"
      shellObject.FindComputer()

    Case" 検索(FindFiles)"
      shellObject.FindFiles()

    Case" Windowsヘルプ(Help)"
      shellObject.Help()

    Case" コンピュータの電源を切る画面(ShutdownWindows)"
      shellObject.ShutdownWindows()

  End Select
End Sub

このプログラムで実際に「タスクトレイのプロパティ」を表示したのが次の画面です。
shellcmd.gif

これらのサンプル・プログラムは、次のリンクからダウンロードできます。
shellcmd_cs.zip(C#版/Visual Studio .NET 2002プロジェクト)
shellcmd_vb.zip(VB.NET版/Visual Studio .NET 2002プロジェクト)

| 個別記事ページ表示中 | コメント書込↓ | トラックバック作成↓ |
コメント[0 items] & トラックバック[0 items]
コメントを書き込む














トラックバックを作成する
以下のURLにトラックバックpingを送信してください。
トラックバックURL:
http://www.masahiko.info/blog/mt-tb.cgi/768

|| Top | Profile | Works | Diary || IT+ | #BLOG | MyView ||
【リンクについて】→詳細を見る
リンク・フリーです。事前、事後のご連絡は必要ありません。
【著作権について】→詳細を見る
Copyright © 2003-2008 Masahiko Isshiki. All rights reserved.
(引用と私的使用以外の記事・画像及び情報の無断転載を禁じます)