home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Toolbars and Status Bars / ToggleButtons / ToggleButtons.cs < prev    next >
Encoding:
Text File  |  2001-01-15  |  3.4 KB  |  104 lines

  1. //--------------------------------------------
  2. // ToggleButtons.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class ToggleButtons: Form
  9. {
  10.      protected Panel   panel;
  11.      protected ToolBar tbar;
  12.      protected string  strText = "Toggle";
  13.      protected Color   clrText = SystemColors.WindowText;
  14.      FontStyle         fontstyle = FontStyle.Regular;
  15.  
  16.      public static void Main()
  17.      {
  18.           Application.Run(new ToggleButtons());
  19.      }
  20.      public ToggleButtons()
  21.      {
  22.           Text = "Toggle Buttons";
  23.  
  24.                // Create panel to fill interior.
  25.  
  26.           panel = new Panel();
  27.           panel.Parent = this;
  28.           panel.Dock = DockStyle.Fill;
  29.           panel.BackColor = SystemColors.Window;
  30.           panel.ForeColor = SystemColors.WindowText;
  31.           panel.Resize += new EventHandler(PanelOnResize);
  32.           panel.Paint += new PaintEventHandler(PanelOnPaint);
  33.  
  34.                // Create ImageList.
  35.  
  36.           Bitmap bm = new Bitmap(GetType(), 
  37.                                  "ToggleButtons.FontStyleButtons.bmp");
  38.  
  39.           ImageList imglst = new ImageList();
  40.           imglst.ImageSize = new Size(bm.Width / 4, bm.Height);
  41.           imglst.Images.AddStrip(bm);
  42.           imglst.TransparentColor = Color.White;
  43.  
  44.                // Create ToolBar.
  45.  
  46.           tbar = new ToolBar();
  47.           tbar.ImageList = imglst;
  48.           tbar.Parent = this;
  49.           tbar.ShowToolTips = true;
  50.           tbar.ButtonClick += 
  51.                          new ToolBarButtonClickEventHandler(ToolBarOnClick);
  52.  
  53.                // Create ToolBarButtons.
  54.  
  55.           FontStyle[] afs = { FontStyle.Bold, FontStyle.Italic,
  56.                               FontStyle.Underline, FontStyle.Strikeout };
  57.  
  58.           for (int i = 0; i < 4; i++)
  59.           {
  60.                ToolBarButton tbarbtn = new ToolBarButton();
  61.                tbarbtn.ImageIndex = i;
  62.                tbarbtn.Style = ToolBarButtonStyle.ToggleButton;
  63.                tbarbtn.ToolTipText = afs[i].ToString();
  64.                tbarbtn.Tag = afs[i];
  65.  
  66.                tbar.Buttons.Add(tbarbtn);
  67.           }
  68.      }
  69.      void ToolBarOnClick(object obj, ToolBarButtonClickEventArgs tbbcea)
  70.      {
  71.           ToolBarButton tbarbtn = tbbcea.Button;
  72.  
  73.                // If the Tag isn't a FontStyle, don't do anything.
  74.  
  75.           if (tbarbtn.Tag == null || 
  76.               tbarbtn.Tag.GetType() != typeof(FontStyle))
  77.                return;
  78.  
  79.                // Set or clear the bit in the fontstyle field.
  80.  
  81.           if (tbarbtn.Pushed)
  82.                fontstyle |= (FontStyle) tbarbtn.Tag;
  83.           else
  84.                fontstyle &= ~(FontStyle) tbarbtn.Tag;
  85.  
  86.           panel.Invalidate();
  87.      }
  88.      void PanelOnResize(object obj, EventArgs ea)
  89.      {
  90.           Panel panel = (Panel) obj;
  91.           panel.Invalidate();
  92.      }
  93.      void PanelOnPaint(object obj, PaintEventArgs pea)
  94.      {
  95.           Panel    panel = (Panel) obj;
  96.           Graphics grfx  = pea.Graphics;
  97.           Font     font  = new Font("Times New Roman", 72, fontstyle);
  98.           SizeF    sizef = grfx.MeasureString(strText, font);
  99.  
  100.           grfx.DrawString(strText, font, new SolidBrush(clrText),
  101.                           (panel.Width - sizef.Width) / 2,
  102.                           (panel.Height - sizef.Height) / 2);
  103.      }
  104. }