更新 : 2007 年 11 月
アドイン コマンドの既定のアイコン (スマイル マーク) は任意のアイコンと置き換えることができます。「方法 : アドインの既定のアイコンを変更する」で、あらかじめ定義されている標準のアイコンについて取り上げていますが、それ以外にもさまざまなアイコンを使用できます。
その方法を次に示します。
アイコンのビットマップをサテライト DLL ファイルのリソースとして格納する。
AddNamedCommand2 メソッドの MSOButton パラメータを false に設定する。これは、サテライト DLL からアイコンのビットマップを検索するという意味になります。
アドイン プロジェクトのコマンドバー部分に使用するリソースの ID 番号を参照する。
カスタム アイコンをアドイン ボタンに追加する方法を次の手順に示します。
![]() |
---|
使用している設定またはエディションによっては、表示されるダイアログ ボックスやメニュー コマンドがヘルプに記載されている内容と異なる場合があります。ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio の設定」を参照してください。 |
カスタム ビットマップをアドイン ボタン アイコンとしてアドイン プロジェクトに追加するには
Visual Studio で既存のアドイン ソリューションを開くか、新しいアドイン ソリューションを作成します。
新しいアドインを作成する場合は、必ず [アドイン用のコマンド バー UI を作成しますか?] オプションをオンにしてください。
新しいリソース ファイルをアドイン プロジェクトに追加します。具体的な手順を次に示します。
ソリューション エクスプローラでアドイン プロジェクトを右クリックします。
[追加] メニューの [新しい項目] を選択します。
テンプレート一覧から [リソース ファイル] を選択し、[追加] ボタンをクリックします。既定の名前 (Resources1.resx) をそのまま使用します。
これにより、Visual Studio のリソース エディタが起動します。
ソリューション エクスプローラのツール バーで [すべてのファイルを表示] をクリックします。
Resource1.resx のプロパティで、[ビルド アクション] プロパティを [なし] に設定します。
アドインのビットマップ引数には、整数値を指定する必要があります。このプロパティ設定することで、リソース ファイルを編集したり、数値識別子を使ってビットマップ リソースを指定したりできます。アドイン プロジェクトに .resx ファイルを組み込むと、これらのことができなくなります。
リソース エディタで [リソースの追加] をクリックし、[新しいイメージ] メニューの [BMP イメージ] を選択します。ここでは、既定の名前 (Image1.bmp) をそのまま使用します。
既存のビットマップ イメージ (16 x 16 ピクセル、16 色または True Color) を選択することもできます。アドインのカスタム アイコンには、16 色または True Color の 16 x 16 ピクセル画像を使用する必要があります。
ビットマップのプロパティ ウィンドウで、Height プロパティおよび Width プロパティを 16 に設定し、Colors プロパティを 16 色または True Color に設定します。
新しいビットマップを作成した場合は、その画像をリソース エディタで編集します。
ソリューション エクスプローラで Resource1.resx ファイルを右クリックし、[プロジェクトから削除] を選択します。
これは、不要なコンパイラ エラーを防ぐための指定です。このファイルがプロジェクトに含まれている必要ありません。これにより、Visual Studio のリソース エディタを使用できます。
アドインの Connect クラスを開きます。AddNamedCommand2 行の OnConnection メソッドで、MSOButton パラメータの値を true から false に変更し、Bitmap パラメータの値を 59 から 1 に変更します。この例を次に示します。
command = commands.AddNamedCommand2(_addInInstance, "MyAddin1", " MyAddin1", "Executes the command for MyAddin1", False, 1, Nothing, CType(vsCommandStatus.vsCommandStatusSupported, Integer) + CType(vsCommandStatus.vsCommandStatusEnabled, Integer), vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton)
Command command = commands.AddNamedCommand2(_addInInstance, "MyAddin1", "MyAddin1", "Executes the command for MyAddin1", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus. vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
MSOButton 引数を false に設定することで、リソース ファイルからボタンのビットマップを検索するよう、アドインに対して強制的に指定しています。1 という番号がビットマップの識別子になります。この値については、後の手順で設定します。
[ファイル] メニューの [すべてを保存] を選択し、[ビルド] メニューの [ソリューションのビルド] を選択してソリューションを閉じます。
Windows エクスプローラで、メモ帳を使って Resource1.resx ファイルを編集します。
"Image1" となっている箇所をすべて検索し、それらを "1" に変更します。作業を終えたら、このファイルを保存します。
アドインの \Resources フォルダで、ビットマップ ファイル名を Image1.bmp から 1.bmp に変更します。
ResGen およびアセンブリ リンカ (AL) を使用して、サテライト DLL をビルドします。具体的な手順を次に示します。
[スタート] ボタンをクリックし、[すべてのプログラム] をポイントします。次に [Microsoft Visual Studio 2005] をポイントし、[Visual Studio Tools] をポイントして、[Visual Studio 2005 コマンド プロンプト] をクリックします。
これにより、Visual Studio ツールを簡単に参照できるよう、必要な環境変数が設定されます。
コマンド プロンプトで、.resx ファイルが格納されているフォルダに移動し、「Resgen Resource1.resx」と入力します。
Resgen は、指定された .resx ファイルを .resources ファイルにコンパイルするためのユーティリティです。詳細については、「リソース ファイル ジェネレータ (Resgen.exe)」を参照してください。
コマンド プロンプトに「Al.exe /embed:Resource1.resources /culture:en-US /out:<Add-In Name>.resources.dll」と入力します。
<Add-In Name> の部分は、実際のアドイン名と置き換えてください。たとえば、実際のアドイン プロジェクトの名前が MyAddin1 の場合は、/out: スイッチを /out:MyAddin1.resources.dll のように置き換えます。/out: に指定した名前がプロジェクトの名前と異なると、リソースの DLL を見つけることができません。
アセンブリ リンカ (Al.exe) により、指定された .resources ファイルが、アドインで参照可能なサテライト リソース DLL へと変換されます。/culture スイッチは、英語以外の言語に変更できます。詳細については、「アセンブリ リンカ (Al.exe)」を参照してください。
Windows エクスプローラで、アドインの DLL ディレクトリ (通常は \bin フォルダ) に移動し、en-US という名前のフォルダを作成します (English US の場合。アセンブリ リンカでカルチャ値に「en-US」を指定したため)。
<Add-In Name>.resources.dll ファイルを新しい en-US フォルダにコピーします。
Visual Studio で、もう一度アドイン プロジェクトを開いて実行します。
[ツール] メニューをクリックします。
アドインが、指定したカスタム アイコンで [ツール] メニューに表示されます。
カスタム ビットマップをアドイン ボタン アイコンとしてネイティブの Visual C++ アドインに追加するには
上記と同じ手順に従います。ただし、次の点を変更する必要があります。
新しい Visual C++ Win32 DLL プロジェクトを作成します。
リソース ファイル (.rc) を追加します。
リソース ビューで、ビットマップ (16 x 16) を追加し、ID (数値) を割り当てます。
ビットマップには、16 色または True Color の 16 x 16 ピクセル画像を使用する必要があります。
Connect.cpp の AddNamedCommand2 メソッドに修正を加えます。MSOButton を VARIANT_FALSE に設定し、Bitmap を、先ほど割り当てたビットマップ ID に設定します。
DLL をビルドします。
ネイティブのアドイン DLL ディレクトリに、サブフォルダ "1033" (English ロケールの場合) を作成します。
サテライト DLL を "1033" ディレクトリにコピーします。
AddIn.rgs を開き、2 つの reg キー値 "SatelliteDllName" および "SatelliteDllPath" を追加します。この例を次に示します。
HKCU { NoRemove 'SOFTWARE' { NoRemove 'Microsoft' { NoRemove 'VisualStudio' { NoRemove '8.0' { NoRemove 'AddIns' { ForceRemove 'NativeAddinCustBitmap.Connect' { val LoadBehavior = d 0 val CommandLineSafe = d 0 val CommandPreload = d 1 val FriendlyName = s 'NativeAddinCustBitmap' val Description = s 'NativeAddinCustBitmap Description' val SatelliteDllName = s 'NativeAddinCustBitmapUI.dll' val SatelliteDllPath = s 'C:\Test\CustomBitmap\NativeAddinCustBitmap \NativeAddinCustBitmap\Debug' } } } } } } }
"SatelliteDllPath" で、パスにロケール ID を追加しないでください。実行時に自動的に追加されます。
アドインを再度ビルドして、更新した情報を登録します。
使用例
サテライト リソース DLL に格納されたカスタム アイコンを参照するアドイン (Visual Basic、Visual C#、Visual J#) の完全なコード例を次に示します。
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Public Class Connect
Implements IDTExtensibility2
Implements IDTCommandTarget
Dim _applicationObject As DTE2
Dim _addInInstance As AddIn
Public Sub New()
End Sub
Public Sub OnConnection(ByVal application As Object, _
ByVal connectMode As ext_ConnectMode, ByVal addInInst _
As Object, ByRef custom As Array) Implements _
IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
If connectMode = ext_ConnectMode.ext_cm_UISetup Then
Dim commands As Commands2 = CType(_applicationObject. _
Commands, Commands2)
Dim toolsMenuName As String
Try
' To move the command to a different
' menu, change the word, Tools, to the English version
' of the menu. This code will take the culture, append
' the name of the menu, and then add the command to
' that menu. A list of all the top-level menus is
' in the file, CommandBar.resx.
Dim resourceManager As System.Resources. _
ResourceManager = New System.Resources. _
ResourceManager("MyAddin3.CommandBar", _
System.Reflection.Assembly.GetExecutingAssembly())
Dim cultureInfo As System.Globalization.CultureInfo _
= New System.Globalization. _
CultureInfo(_applicationObject.LocaleID)
toolsMenuName = resourceManager.GetString(String. _
Concat (cultureInfo.TwoLetterISOLanguageName, _
"Tools"))
Catch e As Exception
' We tried to find a localized version of the word,
' Tools, but one was not found. Default to the en-US
' word, which may work for the current culture.
toolsMenuName = "Tools"
End Try
' Place the command on the Tools menu.
' Find the MenuBar command bar, which is the top-level
' command bar holding all the main menu items:
Dim commandBars As CommandBars = _
CType(_applicationObject.CommandBars, _
CommandBars)
Dim menuBarCommandBar As CommandBar = _
commandBars.Item("MenuBar")
' Find the Tools command bar on the MenuBar command bar.
Dim toolsControl As CommandBarControl = _
menuBarCommandBar.Controls.Item(toolsMenuName)
Dim toolsPopup As CommandBarPopup = CType(toolsControl, _
CommandBarPopup)
Try
' Add a command to the Commands collection.
Dim command As Command = _
commands.AddNamedCommand2(_addInInstance, _
"MyAddin3", "MyAddin3", "Executes the command for _
MyAddin3", False, 1, Nothing, _
CType(vsCommandStatus.vsCommandStatusSupported, _
Integer) + CType(vsCommandStatus. _
vsCommandStatusEnabled, Integer), _
vsCommandStyle.vsCommandStylePictAndText, _
vsCommandControlType.vsCommandControlTypeButton)
' Find the appropriate command bar on the MenuBar
' command bar.
command.AddControl(toolsPopup.CommandBar, 1)
Catch argumentException As System.ArgumentException
' If we are here, then the exception is probably
' because a command with that name already exists. If
' so there is no need to recreate
' the command and we can safely ignore the exception.
End Try
End If
End Sub
Public Sub OnDisconnection(ByVal disconnectMode As _
ext_DisconnectMode, ByRef custom As Array) Implements _
IDTExtensibility2.OnDisconnection
End Sub
Public Sub OnAddInsUpdate(ByRef custom As Array) Implements _
IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnStartupComplete(ByRef custom As Array) Implements _
IDTExtensibility2.OnStartupComplete
End Sub
Public Sub OnBeginShutdown(ByRef custom As Array) Implements _
IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub QueryStatus(ByVal commandName As String, ByVal _
neededText As vsCommandStatusTextWanted, ByRef status As _
vsCommandStatus, ByRef commandText As Object) Implements _
IDTCommandTarget.QueryStatus
If neededText = vsCommandStatusTextWanted. _
vsCommandStatusTextWantedNone Then
If commandName = "MyAddin3.Connect.MyAddin3" Then
status = CType(vsCommandStatus.vsCommandStatusEnabled _
+ vsCommandStatus.vsCommandStatusSupported, _
vsCommandStatus)
Else
status = vsCommandStatus.vsCommandStatusUnsupported
End If
End If
End Sub
Public Sub Exec(ByVal commandName As String, ByVal executeOption _
As vsCommandExecOption, ByRef varIn As Object, ByRef varOut As _
Object, ByRef handled As Boolean) Implements _
IDTCommandTarget.Exec
handled = False
If executeOption = vsCommandExecOption. _
vsCommandExecOptionDoDefault Then
If commandName = "MyAddin3.Connect.MyAddin3" Then
handled = True
MsgBox("Add-in is running.")
Exit Sub
End If
End If
End Sub
End Class
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;
using System.Globalization;
namespace MyAddin1
{
public class Connect : Object, IDTExtensibility2, IDTCommandTarget
{
public Connect()
{
}
public void OnConnection(object application, ext_ConnectMode
connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object []contextGUIDS = new object[] { };
Commands2 commands =
(Commands2)_applicationObject.Commands;
string toolsMenuName;
try
{
ResourceManager resourceManager = new
ResourceManager("MyAddin1.CommandBar",
Assembly.GetExecutingAssembly());
CultureInfo cultureInfo = new
System.Globalization.CultureInfo
(_applicationObject.LocaleID);
string resourceName = String.Concat(cultureInfo.
TwoLetterISOLanguageName, "Tools");
toolsMenuName =
resourceManager.GetString(resourceName);
}
catch
{
toolsMenuName = "Tools";
}
CommandBar menuBarCommandBar =
((CommandBars)_applicationObject.CommandBars)["MenuBar"];
CommandBarControl toolsControl =
menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
try
{
Command command =
commands.AddNamedCommand2(_addInInstance, "MyAddin1",
"MyAddin1", "Executes the command for MyAddin1",
false, 1, ref contextGUIDS,
(int)vsCommandStatus.vsCommandStatusSupported+(int)
vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStylePictAndText,
vsCommandControlType.vsCommandControlTypeButton);
if((command != null) && (toolsPopup != null))
{
command.AddControl(toolsPopup.CommandBar, 1);
}
}
catch
{
}
}
}
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref
Array custom)
{
}
public void OnAddInsUpdate(ref Array custom)
{
}
public void OnStartupComplete(ref Array custom)
{
}
public void OnBeginShutdown(ref Array custom)
{
}
public void QueryStatus(string commandName,
vsCommandStatusTextWanted neededText, ref vsCommandStatus status,
ref object commandText)
{
if(neededText ==
vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if(commandName == "MyAddin1.Connect.MyAddin1")
{
status =
(vsCommandStatus)vsCommandStatus.
vsCommandStatusSupported|vsCommandStatus.
vsCommandStatusEnabled;
return;
}
}
}
public void Exec(string commandName, vsCommandExecOption
executeOption, ref object varIn, ref object varOut, ref bool
handled)
{
handled = false;
if(executeOption ==
vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "MyAddin1.Connect.MyAddin1")
{
handled = true;
System.Windows.Forms.MessageBox.Show("Add-in ran.");
return;
}
}
}
private DTE2 _applicationObject;
private AddIn _addInInstance;
}
}