방법: 도구 모음 단추의 아이콘 정의
참고
ToolStrip 컨트롤은 ToolBar 컨트롤을 대체하고 여기에 다른 기능을 추가하여 새로 도입된 컨트롤이지만 이전 버전과의 호환성 및 이후 사용 가능성을 고려하여 ToolBar 컨트롤을 계속 유지하도록 선택할 수 있습니다.
사용자가 쉽게 식별할 수 있도록 ToolBar 단추에 아이콘을 표시할 수 있습니다. 이렇게 하려면 ImageList 구성 요소(Windows Forms) 구성 요소에 이미지를 추가한 다음 ImageList 구성 요소를 ToolBar 컨트롤에 연결합니다.
프로그래밍 방식으로 도구 모음 단추의 아이콘을 설정하려면
동일한 프로시저에서 ImageList 구성 요소에 이미지를 할당합니다.
동일한 프로시저에서 ImageList 컨트롤을 ToolBar 컨트롤에 할당하고 개별 도구 모음 단추의 ImageIndex 속성을 할당합니다.
다음 코드 예제에서 이미지의 위치로 설정된 경로는 내 문서 폴더입니다. Windows 운영 체제가 실행되는 대부분의 컴퓨터에는 내 문서 폴더가 포함되어 있으므로 이 위치를 사용합니다. 또한 이 위치를 선택하면 사용자는 최소한의 시스템 액세스 수준을 갖고 응용 프로그램을 안전하게 실행할 수 있습니다. 아래 예제에서는 PictureBox 컨트롤이 포함된 폼을 이미 추가한 것으로 가정합니다.
위 단계를 수행하고 나면 아래와 유사한 코드가 작성됩니다.
Public Sub InitializeMyToolBar() ' Instantiate an ImageList component and a ToolBar control. Dim ToolBar1 as New ToolBar Dim ImageList1 as New ImageList ' Assign an image to the ImageList component. ' You should replace the bold image ' in the sample below with an icon of your own choosing. Dim myImage As System.Drawing.Image = _ Image.FromFile Image.FromFile _ (System.Environment.GetFolderPath _ (System.Environment.SpecialFolder.Personal) _ & "\Image.gif") ImageList1.Images.Add(myImage) ' Create a ToolBarButton. Dim ToolBarButton1 As New ToolBarButton() ' Add the ToolBarButton to the ToolBar. ToolBar1.Buttons.Add(toolBarButton1) ' Assign an ImageList to the ToolBar. ToolBar1.ImageList = ImageList1 ' Assign the ImageIndex property of the ToolBarButton. ToolBarButton1.ImageIndex = 0 End Sub
public void InitializeMyToolBar() { // Instantiate an ImageList component and a ToolBar control. ToolBar toolBar1 = new ToolBar(); ImageList imageList1 = new ImageList(); // Assign an image to the ImageList component. // You should replace the bold image // in the sample below with an icon of your own choosing. // Note the escape character used (@) when specifying the path. Image myImage = Image.FromFile (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal) + @"\Image.gif"); imageList1.Images.Add(myImage); // Create a ToolBarButton. ToolBarButton toolBarButton1 = new ToolBarButton(); // Add the ToolBarButton to the ToolBar. toolBar1.Buttons.Add(toolBarButton1); // Assign an ImageList to the ToolBar. toolBar1.ImageList = imageList1; // Assign ImageIndex property of the ToolBarButton. toolBarButton1.ImageIndex = 0; }
public: void InitializeMyToolBar() { // Instantiate an ImageList component and a ToolBar control. ToolBar ^ toolBar1 = gcnew ToolBar(); ImageList ^ imageList1 = gcnew ImageList(); // Assign an image to the ImageList component. // You should replace the bold image // in the sample below with an icon of your own choosing. Image ^ myImage = Image::FromFile(String::Concat (System::Environment::GetFolderPath (System::Environment::SpecialFolder::Personal), "\\Image.gif")); imageList1->Images->Add(myImage); // Create a ToolBarButton. ToolBarButton ^ toolBarButton1 = gcnew ToolBarButton(); // Add the ToolBarButton to the ToolBar. toolBar1->Buttons->Add(toolBarButton1); // Assign an ImageList to the ToolBar. toolBar1->ImageList = imageList1; // Assign ImageIndex property of the ToolBarButton. toolBarButton1->ImageIndex = 0; }