방법: MenuStrip 선택 여백 및 이미지 여백 구성
ShowImageMargin 및 ShowCheckMargin 속성을 다양한 조합으로 설정하여 MenuStrip을 사용자 지정할 수 있습니다.
예제
다음 코드 예제에서는 ContextMenuStrip 검사 여백 및 이미지 여백을 설정하고 사용자 지정하는 방법을 보여 줍니다. 절차는 ContextMenuStrip 또는 MenuStrip에서 동일합니다.
// This code example demonstrates how to set the check
// and image margins for a ToolStripMenuItem.
class Form5 : Form
{
public Form5()
{
// Size the form to show three wide menu items.
this.Width = 500;
this.Text = "ToolStripContextMenuStrip: Image and Check Margins";
// Create a new MenuStrip control.
MenuStrip ms = new MenuStrip();
// Create the ToolStripMenuItems for the MenuStrip control.
ToolStripMenuItem bothMargins = new ToolStripMenuItem("BothMargins");
ToolStripMenuItem imageMarginOnly = new ToolStripMenuItem("ImageMargin");
ToolStripMenuItem checkMarginOnly = new ToolStripMenuItem("CheckMargin");
ToolStripMenuItem noMargins = new ToolStripMenuItem("NoMargins");
// Customize the DropDowns menus.
// This ToolStripMenuItem has an image margin
// and a check margin.
bothMargins.DropDown = CreateCheckImageContextMenuStrip();
((ContextMenuStrip)bothMargins.DropDown).ShowImageMargin = true;
((ContextMenuStrip)bothMargins.DropDown).ShowCheckMargin = true;
// This ToolStripMenuItem has only an image margin.
imageMarginOnly.DropDown = CreateCheckImageContextMenuStrip();
((ContextMenuStrip)imageMarginOnly.DropDown).ShowImageMargin = true;
((ContextMenuStrip)imageMarginOnly.DropDown).ShowCheckMargin = false;
// This ToolStripMenuItem has only a check margin.
checkMarginOnly.DropDown = CreateCheckImageContextMenuStrip();
((ContextMenuStrip)checkMarginOnly.DropDown).ShowImageMargin = false;
((ContextMenuStrip)checkMarginOnly.DropDown).ShowCheckMargin = true;
// This ToolStripMenuItem has no image and no check margin.
noMargins.DropDown = CreateCheckImageContextMenuStrip();
((ContextMenuStrip)noMargins.DropDown).ShowImageMargin = false;
((ContextMenuStrip)noMargins.DropDown).ShowCheckMargin = false;
// Populate the MenuStrip control with the ToolStripMenuItems.
ms.Items.Add(bothMargins);
ms.Items.Add(imageMarginOnly);
ms.Items.Add(checkMarginOnly);
ms.Items.Add(noMargins);
// Dock the MenuStrip control to the top of the form.
ms.Dock = DockStyle.Top;
// Add the MenuStrip control to the controls collection last.
// This is important for correct placement in the z-order.
this.Controls.Add(ms);
}
// This utility method creates a Bitmap for use in
// a ToolStripMenuItem's image margin.
internal Bitmap CreateSampleBitmap()
{
// The Bitmap is a smiley face.
Bitmap sampleBitmap = new Bitmap(32, 32);
Graphics g = Graphics.FromImage(sampleBitmap);
using (Pen p = new Pen(ProfessionalColors.ButtonPressedBorder))
{
// Set the Pen width.
p.Width = 4;
// Set up the mouth geometry.
Point[] curvePoints = new Point[]{
new Point(4,14),
new Point(16,24),
new Point(28,14)};
// Draw the mouth.
g.DrawCurve(p, curvePoints);
// Draw the eyes.
g.DrawEllipse(p, new Rectangle(new Point(7, 4), new Size(3, 3)));
g.DrawEllipse(p, new Rectangle(new Point(22, 4), new Size(3, 3)));
}
return sampleBitmap;
}
// This utility method creates a ContextMenuStrip control
// that has four ToolStripMenuItems showing the four
// possible combinations of image and check margins.
internal ContextMenuStrip CreateCheckImageContextMenuStrip()
{
// Create a new ContextMenuStrip control.
ContextMenuStrip checkImageContextMenuStrip = new ContextMenuStrip();
// Create a ToolStripMenuItem with a
// check margin and an image margin.
ToolStripMenuItem yesCheckYesImage =
new ToolStripMenuItem("Check, Image");
yesCheckYesImage.Checked = true;
yesCheckYesImage.Image = CreateSampleBitmap();
// Create a ToolStripMenuItem with no
// check margin and with an image margin.
ToolStripMenuItem noCheckYesImage =
new ToolStripMenuItem("No Check, Image");
noCheckYesImage.Checked = false;
noCheckYesImage.Image = CreateSampleBitmap();
// Create a ToolStripMenuItem with a
// check margin and without an image margin.
ToolStripMenuItem yesCheckNoImage =
new ToolStripMenuItem("Check, No Image");
yesCheckNoImage.Checked = true;
// Create a ToolStripMenuItem with no
// check margin and no image margin.
ToolStripMenuItem noCheckNoImage =
new ToolStripMenuItem("No Check, No Image");
noCheckNoImage.Checked = false;
// Add the ToolStripMenuItems to the ContextMenuStrip control.
checkImageContextMenuStrip.Items.Add(yesCheckYesImage);
checkImageContextMenuStrip.Items.Add(noCheckYesImage);
checkImageContextMenuStrip.Items.Add(yesCheckNoImage);
checkImageContextMenuStrip.Items.Add(noCheckNoImage);
return checkImageContextMenuStrip;
}
}
' This code example demonstrates how to set the check
' and image margins for a ToolStripMenuItem.
Class Form5
Inherits Form
Public Sub New()
' Size the form to show three wide menu items.
Me.Width = 500
Me.Text = "ToolStripContextMenuStrip: Image and Check Margins"
' Create a new MenuStrip control.
Dim ms As New MenuStrip()
' Create the ToolStripMenuItems for the MenuStrip control.
Dim bothMargins As New ToolStripMenuItem("BothMargins")
Dim imageMarginOnly As New ToolStripMenuItem("ImageMargin")
Dim checkMarginOnly As New ToolStripMenuItem("CheckMargin")
Dim noMargins As New ToolStripMenuItem("NoMargins")
' Customize the DropDowns menus.
' This ToolStripMenuItem has an image margin
' and a check margin.
bothMargins.DropDown = CreateCheckImageContextMenuStrip()
CType(bothMargins.DropDown, ContextMenuStrip).ShowImageMargin = True
CType(bothMargins.DropDown, ContextMenuStrip).ShowCheckMargin = True
' This ToolStripMenuItem has only an image margin.
imageMarginOnly.DropDown = CreateCheckImageContextMenuStrip()
CType(imageMarginOnly.DropDown, ContextMenuStrip).ShowImageMargin = True
CType(imageMarginOnly.DropDown, ContextMenuStrip).ShowCheckMargin = False
' This ToolStripMenuItem has only a check margin.
checkMarginOnly.DropDown = CreateCheckImageContextMenuStrip()
CType(checkMarginOnly.DropDown, ContextMenuStrip).ShowImageMargin = False
CType(checkMarginOnly.DropDown, ContextMenuStrip).ShowCheckMargin = True
' This ToolStripMenuItem has no image and no check margin.
noMargins.DropDown = CreateCheckImageContextMenuStrip()
CType(noMargins.DropDown, ContextMenuStrip).ShowImageMargin = False
CType(noMargins.DropDown, ContextMenuStrip).ShowCheckMargin = False
' Populate the MenuStrip control with the ToolStripMenuItems.
ms.Items.Add(bothMargins)
ms.Items.Add(imageMarginOnly)
ms.Items.Add(checkMarginOnly)
ms.Items.Add(noMargins)
' Dock the MenuStrip control to the top of the form.
ms.Dock = DockStyle.Top
' Add the MenuStrip control to the controls collection last.
' This is important for correct placement in the z-order.
Me.Controls.Add(ms)
End Sub
' This utility method creates a Bitmap for use in
' a ToolStripMenuItem's image margin.
Friend Function CreateSampleBitmap() As Bitmap
' The Bitmap is a smiley face.
Dim sampleBitmap As New Bitmap(32, 32)
Dim g As Graphics = Graphics.FromImage(sampleBitmap)
Dim p As New Pen(ProfessionalColors.ButtonPressedBorder)
Try
' Set the Pen width.
p.Width = 4
' Set up the mouth geometry.
Dim curvePoints() As Point = _
{New Point(4, 14), New Point(16, 24), New Point(28, 14)}
' Draw the mouth.
g.DrawCurve(p, curvePoints)
' Draw the eyes.
g.DrawEllipse(p, New Rectangle(New Point(7, 4), New Size(3, 3)))
g.DrawEllipse(p, New Rectangle(New Point(22, 4), New Size(3, 3)))
Finally
p.Dispose()
End Try
Return sampleBitmap
End Function
' This utility method creates a ContextMenuStrip control
' that has four ToolStripMenuItems showing the four
' possible combinations of image and check margins.
Friend Function CreateCheckImageContextMenuStrip() As ContextMenuStrip
' Create a new ContextMenuStrip control.
Dim checkImageContextMenuStrip As New ContextMenuStrip()
' Create a ToolStripMenuItem with a
' check margin and an image margin.
Dim yesCheckYesImage As New ToolStripMenuItem("Check, Image")
yesCheckYesImage.Checked = True
yesCheckYesImage.Image = CreateSampleBitmap()
' Create a ToolStripMenuItem with no
' check margin and with an image margin.
Dim noCheckYesImage As New ToolStripMenuItem("No Check, Image")
noCheckYesImage.Checked = False
noCheckYesImage.Image = CreateSampleBitmap()
' Create a ToolStripMenuItem with a
' check margin and without an image margin.
Dim yesCheckNoImage As New ToolStripMenuItem("Check, No Image")
yesCheckNoImage.Checked = True
' Create a ToolStripMenuItem with no
' check margin and no image margin.
Dim noCheckNoImage As New ToolStripMenuItem("No Check, No Image")
noCheckNoImage.Checked = False
' Add the ToolStripMenuItems to the ContextMenuStrip control.
checkImageContextMenuStrip.Items.Add(yesCheckYesImage)
checkImageContextMenuStrip.Items.Add(noCheckYesImage)
checkImageContextMenuStrip.Items.Add(yesCheckNoImage)
checkImageContextMenuStrip.Items.Add(noCheckNoImage)
Return checkImageContextMenuStrip
End Function
End Class
코드 컴파일
이 예제에는 다음 사항이 필요합니다.
- System, System.Drawing 및 System.Windows.Forms 어셈블리에 대한 참조
참고 항목
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET Desktop feedback