ToolStripItem.Placement 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取项的当前布局。
public:
property System::Windows::Forms::ToolStripItemPlacement Placement { System::Windows::Forms::ToolStripItemPlacement get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.ToolStripItemPlacement Placement { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Placement : System.Windows.Forms.ToolStripItemPlacement
Public ReadOnly Property Placement As ToolStripItemPlacement
属性值
- 属性
示例
下面的代码示例演示如何使用 Placement 属性进行自定义呈现。 此代码示例是为 ToolStripItem 类提供的一个更大示例的一部分。
// This method defines the behavior for rendering the
// background of a ToolStripItem. If the item is a
// RolloverItem, it paints the item's BackgroundImage
// centered in the client area. If the mouse is in the
// item's client area, a border is drawn around it.
// If the item is on a drop-down or if it is on the
// overflow, a gradient is painted in the background.
protected override void OnRenderItemBackground(
ToolStripItemRenderEventArgs e)
{
base.OnRenderItemBackground(e);
RolloverItem item = e.Item as RolloverItem;
// If the ToolSTripItem is of type RolloverItem,
// perform custom rendering for the background.
if (item != null)
{
if (item.Placement == ToolStripItemPlacement.Overflow ||
item.IsOnDropDown)
{
using (LinearGradientBrush b = new LinearGradientBrush(
item.ContentRectangle,
Color.Salmon,
Color.DarkRed,
0f,
false))
{
e.Graphics.FillRectangle(b, item.ContentRectangle);
}
}
// The RolloverItem control only supports
// the ImageLayout.Center setting for the
// BackgroundImage property.
if (item.BackgroundImageLayout == ImageLayout.Center)
{
// Get references to the item's ContentRectangle
// and BackgroundImage, for convenience.
Rectangle cr = item.ContentRectangle;
Image bgi = item.BackgroundImage;
// Compute the center of the item's ContentRectangle.
int centerX = (cr.Width - bgi.Width) / 2;
int centerY = (cr.Height - bgi.Height) / 2;
// If the item is selected, draw the background
// image as usual. Otherwise, draw it as disabled.
if (item.Selected)
{
e.Graphics.DrawImage(bgi, centerX, centerY);
}
else
{
ControlPaint.DrawImageDisabled(
e.Graphics,
bgi,
centerX,
centerY,
item.BackColor);
}
}
// If the item is in the rollover state,
// draw a border around it.
if (item.Rollover)
{
ControlPaint.DrawFocusRectangle(
e.Graphics,
item.ContentRectangle);
}
}
}
' This method defines the behavior for rendering the
' background of a ToolStripItem. If the item is a
' RolloverItem, it paints the item's BackgroundImage
' centered in the client area. If the mouse is in the
' item's client area, a border is drawn around it.
' If the item is on a drop-down or if it is on the
' overflow, a gradient is painted in the background.
Protected Overrides Sub OnRenderItemBackground(ByVal e As ToolStripItemRenderEventArgs)
MyBase.OnRenderItemBackground(e)
Dim item As RolloverItem = CType(e.Item, RolloverItem)
' If the ToolSTripItem is of type RolloverItem,
' perform custom rendering for the background.
If (item IsNot Nothing) Then
If item.Placement = ToolStripItemPlacement.Overflow OrElse item.IsOnDropDown Then
Dim b As New LinearGradientBrush(item.ContentRectangle, Color.Salmon, Color.DarkRed, 0.0F, False)
Try
e.Graphics.FillRectangle(b, item.ContentRectangle)
Finally
b.Dispose()
End Try
End If
' The RolloverItem control only supports
' the ImageLayout.Center setting for the
' BackgroundImage property.
If item.BackgroundImageLayout = ImageLayout.Center Then
' Get references to the item's ContentRectangle
' and BackgroundImage, for convenience.
Dim cr As Rectangle = item.ContentRectangle
Dim bgi As Image = item.BackgroundImage
' Compute the center of the item's ContentRectangle.
Dim centerX As Integer = CInt((cr.Width - bgi.Width) / 2)
Dim centerY As Integer = CInt((cr.Height - bgi.Height) / 2)
' If the item is selected, draw the background
' image as usual. Otherwise, draw it as disabled.
If item.Selected Then
e.Graphics.DrawImage(bgi, centerX, centerY)
Else
ControlPaint.DrawImageDisabled(e.Graphics, bgi, centerX, centerY, item.BackColor)
End If
End If
' If the item is in the rollover state,
' draw a border around it.
If item.Rollover Then
ControlPaint.DrawFocusRectangle(e.Graphics, item.ContentRectangle)
End If
End If
End Sub
注解
Placement检查 事件中的 LayoutCompleted 属性,以确定项是放置在main ToolStrip、溢出 ToolStrip上,还是当前根本未显示。 项未显示的典型原因是主 ToolStrip 中不能容纳项,并且其 Overflow 属性设置为 Never。