StatusBar.OnDrawItem 方法

引发 OnDrawItem 事件。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Protected Overridable Sub OnDrawItem ( _
    sbdievent As StatusBarDrawItemEventArgs _
)
用法
Dim sbdievent As StatusBarDrawItemEventArgs

Me.OnDrawItem(sbdievent)
protected virtual void OnDrawItem (
    StatusBarDrawItemEventArgs sbdievent
)
protected:
virtual void OnDrawItem (
    StatusBarDrawItemEventArgs^ sbdievent
)
protected void OnDrawItem (
    StatusBarDrawItemEventArgs sbdievent
)
protected function OnDrawItem (
    sbdievent : StatusBarDrawItemEventArgs
)

参数

备注

引发事件时会通过委托调用事件处理程序。有关更多信息,请参见 引发事件

OnDrawItem 方法还允许派生类对事件进行处理而不必附加委托。这是在派生类中处理事件的首选技术。

给继承者的说明 在派生类中重写 OnDrawItem 时,一定要调用基类的 OnDrawItem 方法,以便已注册的委托对事件进行接收。

示例

下面的代码示例演示如何创建所有者描述的 StatusBarPanel,它显示自定义背景和当前日期。本示例要求已将 StatusBar 控件的 DrawItem 事件连接到本示例中定义的事件处理程序。

Private Sub StatusBar1_DrawItem(ByVal sender As Object, ByVal sbdevent As System.Windows.Forms.StatusBarDrawItemEventArgs) Handles StatusBar1.DrawItem

   ' Create a StringFormat object to align text in the panel.
   Dim sf As New StringFormat()
   ' Format the String of the StatusBarPanel to be centered.
   sf.Alignment = StringAlignment.Center
   sf.LineAlignment = StringAlignment.Center

   ' Draw a black background in owner-drawn panel.
   sbdevent.Graphics.FillRectangle(Brushes.Black, sbdevent.Bounds)
   ' Draw the current date (short date format) with white text in the control's font.
   sbdevent.Graphics.DrawString(DateTime.Today.ToShortDateString(), StatusBar1.Font, Brushes.White, _
         New RectangleF(sbdevent.Bounds.X, sbdevent.Bounds.Y, _
         sbdevent.Bounds.Width, sbdevent.Bounds.Height), sf)
End Sub
private void DrawMyPanel(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
{
   // Create a StringFormat object to align text in the panel.
   StringFormat sf = new StringFormat();
   // Format the String of the StatusBarPanel to be centered.
   sf.Alignment = StringAlignment.Center;
   sf.LineAlignment = StringAlignment.Center;

   // Draw a black background in owner-drawn panel.
   sbdevent.Graphics.FillRectangle(Brushes.Black, sbdevent.Bounds);
   // Draw the current date (short date format) with white text in the control's font.
   sbdevent.Graphics.DrawString(DateTime.Today.ToShortDateString(), 
      statusBar1.Font,Brushes.White,sbdevent.Bounds,sf);
}
private:
   void DrawMyPanel( Object^ /*sender*/, System::Windows::Forms::StatusBarDrawItemEventArgs^ sbdevent )
   {
      // Create a StringFormat object to align text in the panel.
      StringFormat^ sf = gcnew StringFormat;

      // Format the String of the StatusBarPanel to be centered.
      sf->Alignment = StringAlignment::Center;
      sf->LineAlignment = StringAlignment::Center;

      // Draw a back blackground in owner-drawn panel.
      sbdevent->Graphics->FillRectangle( Brushes::Black, sbdevent->Bounds );

      // Draw the current date (short date format) with white text in the control's font.
      sbdevent->Graphics->DrawString( DateTime::Today.ToShortDateString(), statusBar1->Font, Brushes::White, sbdevent->Bounds, sf );
   }
private void DrawMyPanel(Object sender, 
    System.Windows.Forms.StatusBarDrawItemEventArgs sbdEvent)
{
    // Create a StringFormat object to align text in the panel.
    StringFormat sf = new StringFormat();
    // Format the String of the StatusBarPanel to be centered.
    sf.set_Alignment(StringAlignment.Center);
    sf.set_LineAlignment(StringAlignment.Center);
    // Draw a back blackground in owner-drawn panel.
    sbdEvent.get_Graphics().FillRectangle(Brushes.get_Black(), 
        sbdEvent.get_Bounds());
    // Draw the current date (short date format) with white text 
    // in the control's font.
    sbdEvent.get_Graphics().DrawString(DateTime.get_Today().
        ToShortDateString(), statusBar1.get_Font(), Brushes.get_White(),
        RectangleF.op_Implicit(sbdEvent.get_Bounds()), sf);
} //DrawMyPanel

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

StatusBar 类
StatusBar 成员
System.Windows.Forms 命名空间
DrawItem
StatusBarDrawItemEventArgs