MenuItem.MeasureItem 事件

定义

当菜单在绘制菜单项之前需要知道菜单项大小时发生。Occurs when the menu needs to know the size of a menu item before drawing it.

public:
 event System::Windows::Forms::MeasureItemEventHandler ^ MeasureItem;
public event System.Windows.Forms.MeasureItemEventHandler MeasureItem;
member this.MeasureItem : System.Windows.Forms.MeasureItemEventHandler 
Public Custom Event MeasureItem As MeasureItemEventHandler 

示例

下面的代码示例演示如何使用此成员。The following code example demonstrates the use of this member. 在此示例中,事件处理程序报告 MeasureItem 事件的发生。In the example, an event handler reports on the occurrence of the MeasureItem event. 此报表可帮助你了解何时发生事件,并可帮助你进行调试。This report helps you to learn when the event occurs and can assist you in debugging.

若要运行示例代码,请将其粘贴到包含名为 MenuItem1MenuItem 类型实例的项目中。To run the example code, paste it into a project that contains an instance of type MenuItem named MenuItem1. 然后,确保事件处理程序与 MeasureItem 事件关联。Then ensure that the event handler is associated with the MeasureItem event.

private void MenuItem1_MeasureItem(Object sender, MeasureItemEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "Graphics", e.Graphics );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Index", e.Index );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "ItemHeight", e.ItemHeight );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "ItemWidth", e.ItemWidth );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "MeasureItem Event" );
}
Private Sub MenuItem1_MeasureItem(sender as Object, e as MeasureItemEventArgs) _ 
     Handles MenuItem1.MeasureItem

    Dim messageBoxVB as New System.Text.StringBuilder()
    messageBoxVB.AppendFormat("{0} = {1}", "Graphics", e.Graphics)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Index", e.Index)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "ItemHeight", e.ItemHeight)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "ItemWidth", e.ItemWidth)
    messageBoxVB.AppendLine()
    MessageBox.Show(messageBoxVB.ToString(),"MeasureItem Event")

End Sub

注解

若要引发此事件,必须将菜单项的 OwnerDraw 属性设置为 trueIn order for this event to be raised, you must have the OwnerDraw property of the menu item set to true. 在绘制所有者描述的菜单以允许指定要绘制的菜单项大小时,将引发此事件。This event is raised before owner drawn menus are drawn to allow for the size of the menu item to be drawn to be specified. 有关处理事件的详细信息,请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

适用于

另请参阅