Csharp/C Sharp by API/System.Windows.Forms/StatusBar

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

new StatusBar()

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class SimpleStatusBar: Form
{
     public static void Main()
     {
          Application.Run(new SimpleStatusBar());
     }
     public SimpleStatusBar()
     {
          Text = "Simple Status Bar";
          ResizeRedraw = true;
   
          StatusBar sb = new StatusBar();
          sb.Parent = this;
          sb.Text = "My initial status bar text";
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
          Pen      pen  = new Pen(ForeColor);
   
          grfx.DrawLine(pen, 0, 0, ClientSize.Width, ClientSize.Height);
          grfx.DrawLine(pen, ClientSize.Width, 0, 0, ClientSize.Height);
     }
}


StatusBar.Items.AddRange

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.ruboBox lstFonts;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.StatusStrip statusBar;
  private System.Windows.Forms.ToolStripStatusLabel statusLabel;
  public Form1() {
        InitializeComponent();
    System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
    foreach (FontFamily family in fonts.Families)
    {
      lstFonts.Items.Add(family.Name);
    }
  }
  private void lstFonts_SelectedIndexChanged(object sender, EventArgs e)
  {
        this.Invalidate();
  }
  private void Form1_Paint(object sender, PaintEventArgs e)
  {
    if (lstFonts.SelectedIndex != -1)
    {
            e.Graphics.DrawString(lstFonts.Text, new Font(lstFonts.Text, 50), Brushes.Black, 10, 50);
            statusBar.Items[0].Text = lstFonts.Text;
    }
  }
  private void InitializeComponent()
  {
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.lstFonts = new System.Windows.Forms.ruboBox();
    this.label1 = new System.Windows.Forms.Label();
    this.statusBar = new System.Windows.Forms.StatusStrip();
    this.statusLabel = new System.Windows.Forms.ToolStripStatusLabel();
    this.groupBox1.SuspendLayout();
    this.statusBar.SuspendLayout();
    this.SuspendLayout();
    this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
          | System.Windows.Forms.AnchorStyles.Right)));
    this.groupBox1.Controls.Add(this.lstFonts);
    this.groupBox1.Controls.Add(this.label1);
    this.groupBox1.Location = new System.Drawing.Point(7, 0);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.Size = new System.Drawing.Size(497, 40);
    this.groupBox1.TabIndex = 1;
    this.groupBox1.TabStop = false;
    this.lstFonts.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList;
    this.lstFonts.DropDownWidth = 340;
    this.lstFonts.FormattingEnabled = true;
    this.lstFonts.Location = new System.Drawing.Point(100, 12);
    this.lstFonts.Name = "lstFonts";
    this.lstFonts.Size = new System.Drawing.Size(340, 21);
    this.lstFonts.TabIndex = 1;
    this.lstFonts.SelectedIndexChanged += new System.EventHandler(this.lstFonts_SelectedIndexChanged);
    this.label1.Location = new System.Drawing.Point(12, 16);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(80, 12);
    this.label1.TabIndex = 0;
    this.label1.Text = "Choose Font:";
    this.statusBar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.statusLabel});
    this.statusBar.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Table;
    this.statusBar.Location = new System.Drawing.Point(0, 155);
    this.statusBar.Size = new System.Drawing.Size(516, 22);
    this.statusBar.TabIndex = 2;
    this.statusLabel.Name = "statusLabel";
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(516, 177);
    this.Controls.Add(this.groupBox1);
    this.Controls.Add(this.statusBar);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Text = "Font Viewer";
    this.groupBox1.ResumeLayout(false);
    this.statusBar.ResumeLayout(false);
    this.ResumeLayout(false);
    this.PerformLayout();
  }
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }
}


StatusBar.Panels

 
  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class MainForm : System.Windows.Forms.Form
  {
    private StatusBar statusBar = new StatusBar();
    private StatusBarPanel sbPnlPrompt = new StatusBarPanel();
    private StatusBarPanel sbPnlTime = new StatusBarPanel();
    private Timer timer1 = new Timer();
    private MainMenu mainMenu;
    private System.ruponentModel.Container components;
    public MainForm()
    {
      InitializeComponent();
      Text = "Status Bar Example";
      CenterToScreen();
      BackColor = Color.CadetBlue;
      // Configure the timer.
      timer1.Interval = 1000;
      timer1.Enabled = true;
      timer1.Tick += new EventHandler(timer1_Tick);
      this.MenuComplete += new EventHandler(StatusForm_MenuDone);
      BuildMenuSystem();
      BuildStatBar();
    }
    private void InitializeComponent()
    {
      this.ruponents = new System.ruponentModel.Container();
      this.Size = new System.Drawing.Size(300,300);
      this.Text = "Form1";
    }
    static void Main() 
    {
      Application.Run(new MainForm());
    }
    // Clicked handlers.
    private void FileExit_Clicked(object sender, EventArgs e) 
    {    
      this.Close();
    }
        
    // Help | About Menu item handler
    private void HelpAbout_Clicked(object sender, EventArgs e) 
    {
      MessageBox.Show("The amazing menu app...");
    }
        
    // Selected handlers.
    private void FileExit_Selected(object sender, EventArgs e) 
    {
      sbPnlPrompt.Text = "Terminates this app";     
    }
    private void HelpAbout_Selected(object sender, EventArgs e) 
    {
      sbPnlPrompt.Text = "Displays app info";
    }
    // Other handlers...
    private void StatusForm_MenuDone(object sender, EventArgs e) 
    {
      sbPnlPrompt.Text = "Ready";
    }
    private void timer1_Tick(object sender, EventArgs e) 
    {
      DateTime t = DateTime.Now;
      string s = t.ToLongTimeString() ;
      sbPnlTime.Text = s ;    
    }
    private void BuildMenuSystem()
    {
      mainMenu = new MainMenu();
      
      MenuItem miFile = mainMenu.MenuItems.Add("&File");          
      miFile.MenuItems.Add(new MenuItem("E&xit",new EventHandler(this.FileExit_Clicked), Shortcut.CtrlX));
      miFile.MenuItems[0].Select += new EventHandler(FileExit_Selected);
      MenuItem miHelp = mainMenu.MenuItems.Add("Help");  
      miHelp.MenuItems.Add(new MenuItem("&About",  new EventHandler(this.HelpAbout_Clicked), Shortcut.CtrlA));
      miHelp.MenuItems[0].Select += new EventHandler(HelpAbout_Selected);
      this.Menu = mainMenu;      
    }
    private void BuildStatBar()
    {
      // Configure the status bar.
      statusBar.ShowPanels = true;
      statusBar.Panels.AddRange(new StatusBarPanel[] {sbPnlPrompt, sbPnlTime});
      
      // Configure prompt panel.
      sbPnlPrompt.BorderStyle = StatusBarPanelBorderStyle.None;
      sbPnlPrompt.AutoSize = StatusBarPanelAutoSize.Spring;
      sbPnlPrompt.Width = 62;
      sbPnlPrompt.Text = "Ready";
      
      // Configure time pane.
      sbPnlTime.Alignment = HorizontalAlignment.Right;
      sbPnlTime.Width = 76;
      try
      {
        Icon i = new Icon("status.ico");
        sbPnlPrompt.Icon = i;
      }
      catch(Exception e)
      {
        Console.WriteLine(e.Message);
      }
      this.Controls.Add(statusBar);  
    }
  }


StatusBar.Panels.Add

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class TwoStatusBarPanels: Form
{
     public static void Main()
     {
          Application.Run(new TwoStatusBarPanels());
     }
     public TwoStatusBarPanels()
     {
          Text = "Two Status Bar Panels";
          StatusBar sb = new StatusBar();
          sb.Parent = this;
          sb.ShowPanels = true;
   
          StatusBarPanel sbpanel1 = new StatusBarPanel();
          sbpanel1.Text = "Panel 1";
   
          StatusBarPanel sbpanel2 = new StatusBarPanel();
          sbpanel2.Text = "Panel 2";
   
          sb.Panels.Add(sbpanel1);
          sb.Panels.Add(sbpanel2);
     }
}


StatusBar.ShowPanels

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class MenuHelpFirstTry: Form
{
     StatusBarPanel sbpMenuHelp;
     string         strSavePanelText;
   
     public static void Main()
     {
          Application.Run(new MenuHelpFirstTry());
     }
     public MenuHelpFirstTry()
     {
   
          StatusBar sb = new StatusBar();
          sb.Parent = this;
          sb.ShowPanels = true;
   
          sbpMenuHelp = new StatusBarPanel();
          sbpMenuHelp.Text = "Ready";
          sbpMenuHelp.AutoSize = StatusBarPanelAutoSize.Spring;
   
          sb.Panels.Add(sbpMenuHelp);
   
          Menu = new MainMenu();
          EventHandler ehSelect = new EventHandler(MenuOnSelect);
          
          MenuItem mi = new MenuItem("File");
          mi.Select += ehSelect;
          Menu.MenuItems.Add(mi);
   
          mi = new MenuItem("Open");
          mi.Select += ehSelect;
          Menu.MenuItems[0].MenuItems.Add(mi);
   
          mi = new MenuItem("Close");
          mi.Select += ehSelect;
          Menu.MenuItems[0].MenuItems.Add(mi);
   
          mi = new MenuItem("Save");
          mi.Select += ehSelect;
          Menu.MenuItems[0].MenuItems.Add(mi);
   
          mi = new MenuItem("Edit");
          mi.Select += ehSelect;
          Menu.MenuItems.Add(mi);
   
          mi = new MenuItem("Cut");
          mi.Select += ehSelect;
          Menu.MenuItems[1].MenuItems.Add(mi);
          
          mi = new MenuItem("Copy");
          mi.Select += ehSelect;
          Menu.MenuItems[1].MenuItems.Add(mi);
   
          mi = new MenuItem("Paste");
          mi.Select += ehSelect;
          Menu.MenuItems[1].MenuItems.Add(mi);
     }
     protected override void OnMenuStart(EventArgs ea)
     {
          strSavePanelText = sbpMenuHelp.Text;
     }
     protected override void OnMenuComplete(EventArgs ea)
     {
          sbpMenuHelp.Text = strSavePanelText;
     }
     void MenuOnSelect(object obj, EventArgs ea)
     {
          MenuItem mi = (MenuItem) obj;
          string   str;
   
          switch (mi.Text)
          {
              case "File":   str = "file";    break;
              case "Open":   str = "Open";         break;
              case "Close":  str = "Close";        break;
              case "Save":   str = "Save";         break;
              case "Edit":   str = "edit";  break;
              case "Cut":    str = "cut";         break;
              case "Copy":   str = "Copy";                          break;
              case "Paste":  str = "Paste";        break;
              default:       str = "";                                   break;
          }
   
          sbpMenuHelp.Text = str;
     }
}