Using ToolBarButton : ToolBar : GUI Windows Form C# Source Code


Custom Search

C# Source Code » GUI Windows Form » ToolBar »

 

Using ToolBarButton








    
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class ToggleButtons: Form
{
     protected Panel   panel = new Panel();
     protected ToolBar tbar;
     protected string  strText = "Toggle";
     protected Color   clrText = SystemColors.WindowText;
     FontStyle         fontstyle = FontStyle.Regular;
   
     public static void Main()
     {
          Application.Run(new ToggleButtons());
     }
     public ToggleButtons()
     {
          panel.Parent = this;
          panel.Dock = DockStyle.Fill;
          panel.BackColor = SystemColors.Window;
          panel.ForeColor = SystemColors.WindowText;
          panel.Resize += new EventHandler(PanelOnResize);
          panel.Paint += new PaintEventHandler(PanelOnPaint);
   
          Bitmap bm = new Bitmap(GetType(), "ToggleButtons.bmp");
   
          ImageList imglst = new ImageList();
          imglst.ImageSize = new Size(bm.Width / 4, bm.Height);
          imglst.Images.AddStrip(bm);
          imglst.TransparentColor = Color.White;
   
          tbar = new ToolBar();
          tbar.ImageList = imglst;
          tbar.Parent = this;
          tbar.ShowToolTips = true;
          tbar.ButtonClick += new ToolBarButtonClickEventHandler(ToolBarOnClick);
   
          FontStyle[] afs = { FontStyle.Bold, FontStyle.Italic,
                              FontStyle.Underline, FontStyle.Strikeout };
   
          for (int i = 0; i < 4; i++)
          {
               ToolBarButton tbarbtn = new ToolBarButton();
               tbarbtn.ImageIndex = i;
               tbarbtn.Style = ToolBarButtonStyle.ToggleButton;
               tbarbtn.ToolTipText = afs[i].ToString();
               tbarbtn.Tag = afs[i];
   
               tbar.Buttons.Add(tbarbtn);
          }
     }
     void ToolBarOnClick(object obj, ToolBarButtonClickEventArgs tbbcea)
     {
          ToolBarButton tbarbtn = tbbcea.Button;
   
          if (tbarbtn.Tag == null ||
              tbarbtn.Tag.GetType() != typeof(FontStyle))
               return;
   
          if (tbarbtn.Pushed)
               fontstyle |= (FontStyle) tbarbtn.Tag;
          else
               fontstyle &= ~(FontStyle) tbarbtn.Tag;
   
          panel.Invalidate();
     }
     void PanelOnResize(object obj, EventArgs ea)
     {
          Panel panel = (Panel) obj;
          panel.Invalidate();
     }
     void PanelOnPaint(object obj, PaintEventArgs pea)
     {
          Panel    panel = (Panel) obj;
          Graphics grfx  = pea.Graphics;
          Font     font  = new Font("Times New Roman", 72, fontstyle);
          SizeF    sizef = grfx.MeasureString(strText, font);
   
          grfx.DrawString(strText, font, new SolidBrush(clrText),
                          (panel.Width - sizef.Width) / 2,
                          (panel.Height - sizef.Height) / 2);
     }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo GUI Windows Form
» ToolBar