Button FlatStyle Styles

   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class ButtonStyles: Form
{
     public static void Main()
     {
          Application.Run(new ButtonStyles());
     }
     public ButtonStyles()
     {
          int y = 0;
   
          foreach (FlatStyle fs in Enum.GetValues(typeof(FlatStyle)))
          {
               Button btn    = new Button();
               btn.Parent    = this;
               btn.FlatStyle = fs;
               btn.Text      = fs.ToString();
               btn.Location  = new Point(50, y += 50);
          }
     }
}

    


Button Localtion

   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class SimpleButton: Form
{
     public static void Main()
     {
          Application.Run(new SimpleButton());
     }
     public SimpleButton()
     {
          Text = "Simple Button";
   
          Button btn   = new Button();
          btn.Parent   = this;
          btn.Text     = "Click Me!";
          btn.Location = new Point(100, 100);
          btn.Click   += new EventHandler(ButtonOnClick);
     }
     void ButtonOnClick(object obj, EventArgs ea)
     {
          Graphics grfx   = CreateGraphics();
          Point    ptText = Point.Empty;
          string   str    = "Button clicked!";
   
          grfx.DrawString(str, Font, new SolidBrush(ForeColor), ptText);
          grfx.Dispose();
     }
}

    


Button Name, TabIndex, Text

   
 

using System;
using System.Windows.Forms;

class MainForm : Form
{
    private Label label1;
    private TextBox textBox1;
    private Button button1;

    public MainForm()
    {
         this.label1 = new Label();
         this.textBox1 = new TextBox();
         this.button1 = new Button();
         this.SuspendLayout();

         this.label1.Location = new System.Drawing.Point(16, 36);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(128, 16);
         this.label1.TabIndex = 0;
         this.label1.Text = "Please enter your name:"; 

         this.textBox1.Location = new System.Drawing.Point(152, 32);
         this.textBox1.Name = "textBox1";
         this.textBox1.TabIndex = 1;
         this.textBox1.Text = "";

         this.button1.Location = new System.Drawing.Point(109, 80);
         this.button1.Name = "button1";
         this.button1.TabIndex = 2;
         this.button1.Text = "Enter";
         this.button1.Click += new System.EventHandler(this.button1_Click);

         this.ClientSize = new System.Drawing.Size(292, 126);
         this.Controls.Add(this.button1);
         this.Controls.Add(this.textBox1);
         this.Controls.Add(this.label1);
         this.Name = "form1";
         this.Text = "Visual C#";

         this.ResumeLayout(false);
     }
     private void button1_Click(object sender, System.EventArgs e)
     {
        System.Console.WriteLine("User entered: " + textBox1.Text);
        MessageBox.Show("Welcome, " + textBox1.Text, "Visual C#");
     }
     [STAThread]
     public static void Main()
     {
        Application.EnableVisualStyles();
        Application.Run(new MainForm());
     }
}

    


Button Image, Size, Parent

   
 



using System;
using System.Drawing;
using System.Windows.Forms;
   
class BitmapButtons: Form
{
     int    cxBtn, cyBtn, dxBtn;
     Button btnLarger, btnSmaller;
   
     public static void Main()
     {
          Application.Run(new BitmapButtons());
     }
     public BitmapButtons()
     {
          ResizeRedraw = true;
   
          dxBtn = Font.Height;
          btnLarger = new Button();
          btnLarger.Parent = this;
          btnLarger.Image  = new Bitmap(GetType(), "LargerButton.bmp") ;
   
          cxBtn = btnLarger.Image.Width  + 8;
          cyBtn = btnLarger.Image.Height + 8;
   
          btnLarger.Size   = new Size(cxBtn, cyBtn);
          btnLarger.Click += new EventHandler(ButtonLargerOnClick);
   
          btnSmaller = new Button();
          btnSmaller.Parent = this;
          btnSmaller.Image  = new Bitmap(GetType(), "SmallerButton.bmp");
          btnSmaller.Size   = new Size(cxBtn, cyBtn);
          btnSmaller.Click += new EventHandler(ButtonSmallerOnClick);
   
          OnResize(EventArgs.Empty);
     }
     protected override void OnResize(EventArgs ea)
     {
          base.OnResize(ea);
   
          btnLarger.Location = new Point(ClientSize.Width / 2 - cxBtn - dxBtn / 2,
                                  (ClientSize.Height - cyBtn) / 2);
          btnSmaller.Location = new Point(ClientSize.Width / 2 + dxBtn / 2,
                                  (ClientSize.Height - cyBtn) / 2);
     }
     void ButtonLargerOnClick(object obj, EventArgs ea)
     {
          Left   = 50;
          Top    = 50;
          Width  = 50;
          Height = 50;
     }
     void ButtonSmallerOnClick(object obj, EventArgs ea)
     {
          Left   = 200;
          Top    = 200;
          Width  = 20;
          Height = 20;
     }
}

    


Set Label Border style to FixedSingle


   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
   private System.Windows.Forms.Label firstLabel;
   private System.Windows.Forms.Label secondLabel;
   private System.Windows.Forms.ToolTip labelsToolTip;

   public Form1() {
       InitializeComponent();
   }

   private void InitializeComponent()
   {
      this.firstLabel = new System.Windows.Forms.Label();
      this.secondLabel = new System.Windows.Forms.Label();
      this.labelsToolTip = new System.Windows.Forms.ToolTip(new System.ComponentModel.Container());
      this.SuspendLayout();
      // 
      // firstLabel
      // 
      this.firstLabel.AutoSize = true;
      this.firstLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
      this.firstLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.firstLabel.Location = new System.Drawing.Point(12, 20);
      this.firstLabel.Name = "firstLabel";
      this.firstLabel.Size = new System.Drawing.Size(92, 18);
      this.firstLabel.TabIndex = 0;
      this.firstLabel.Text = "This is a label.";
      this.labelsToolTip.SetToolTip(this.firstLabel, "First Label");
      // 
      // secondLabel
      // 
      this.secondLabel.AutoSize = true;
      this.secondLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
      this.secondLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.secondLabel.Location = new System.Drawing.Point(12, 69);
      this.secondLabel.Name = "secondLabel";
      this.secondLabel.Size = new System.Drawing.Size(133, 18);
      this.secondLabel.TabIndex = 1;
      this.secondLabel.Tag = "";
      this.secondLabel.Text = "This is another Label.";
      this.labelsToolTip.SetToolTip(this.secondLabel, "Second Label");
      // 
      // ToolTipExampleForm
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(252, 124);
      this.Controls.Add(this.secondLabel);
      this.Controls.Add(this.firstLabel);
      this.Name = "ToolTipExampleForm";
      this.Text = "ToolTip Demonstration";
      this.ResumeLayout(false);
      this.PerformLayout();

   }

  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}



           
          


Bitmap Viewer Host


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System.Drawing;
using System.Windows.Forms;
using System.IO;

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace BitmapViewerHost
{
    /// <summary>
    /// Summary description for BitmapViewerHost.
    /// </summary>
    public class BitmapViewerHost : System.Windows.Forms.Form
    {
        private BitmapViewer bitmapViewer1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public BitmapViewerHost()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.bitmapViewer1 = new BitmapViewer();
            this.SuspendLayout();
            // 
            // bitmapViewer1
            // 
            this.bitmapViewer1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.bitmapViewer1.Dimension = 80;
            this.bitmapViewer1.Directory = null;
            this.bitmapViewer1.DockPadding.All = 1;
            this.bitmapViewer1.Location = new System.Drawing.Point(12, 12);
            this.bitmapViewer1.Name = "bitmapViewer1";
            this.bitmapViewer1.Size = new System.Drawing.Size(300, 244);
            this.bitmapViewer1.Spacing = 10;
            this.bitmapViewer1.TabIndex = 0;
            this.bitmapViewer1.PictureSelected += new BitmapViewer.PictureSelectedDelegate(this.bitmapViewer1_PictureSelected);
            // 
            // BitmapViewerHost
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(320, 266);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.bitmapViewer1});
            this.Name = "BitmapViewerHost";
            this.Text = "BitmapViewerHost";
            this.Load += new System.EventHandler(this.BitmapViewerHost_Load);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new BitmapViewerHost());
        }

        private void BitmapViewerHost_Load(object sender, System.EventArgs e)
        {
            bitmapViewer1.Directory = Application.StartupPath;
            bitmapViewer1.Directory = "c:windows";
        }

        private void bitmapViewer1_PictureSelected(object sender, PictureSelectedEventArgs e)
        {
                MessageBox.Show("You chose " + e.FileName);
        }

    
    }
    /// <summary>
    /// Summary description for BitmapViewer.
    /// </summary>
    public class BitmapViewer : System.Windows.Forms.UserControl
    {
        internal System.Windows.Forms.Panel pnlPictures;
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public BitmapViewer()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            // TODO: Add any initialization after the InitForm call

        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Component Designer generated code
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pnlPictures = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // pnlPictures
            // 
            this.pnlPictures.AutoScroll = true;
            this.pnlPictures.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pnlPictures.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pnlPictures.Location = new System.Drawing.Point(1, 1);
            this.pnlPictures.Name = "pnlPictures";
            this.pnlPictures.Size = new System.Drawing.Size(530, 218);
            this.pnlPictures.TabIndex = 1;
            // 
            // BitmapViewer
            // 
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.pnlPictures});
            this.DockPadding.All = 1;
            this.Name = "BitmapViewer";
            this.Size = new System.Drawing.Size(532, 220);
            this.ResumeLayout(false);

        }
        #endregion
    
        public delegate void PictureSelectedDelegate(object sender, PictureSelectedEventArgs e);
        public event PictureSelectedDelegate PictureSelected;

        // The directory that will be scanned for image.
        private string directory="";

        // Each picture box will be a square of dimension X dimension pixels.
        private int dimension;

        // The space between the images and the top, left, and right sides.
        private int border = 5;

        // The space between each image.
        private int spacing;

        // The images that were found in the selected directory.
        private ArrayList images = new ArrayList();


        public string Directory
        {
            get
            {
                return directory;
            }
            set
            {
                directory = value;
                GetImages();
                UpdateDisplay();
            }
        }

        public int Dimension
        {
            get
            {
                return dimension;
            }
            set
            {
                dimension = value;
                UpdateDisplay();
            }
        }

        public int Spacing
        {
            get
            {
                return spacing;
            }
            set
            {
                spacing = value;
                UpdateDisplay();
            }
        }



        private void GetImages()
        {
            images.Clear();
            if (this.Directory != "" &amp;&amp; this.directory != null)
            {
                DirectoryInfo dir = new DirectoryInfo(directory);
                foreach (FileInfo file in dir.GetFiles("*.bmp"))
                {
                    images.Add(new NamedImage(Bitmap.FromFile(file.FullName), file.Name));
                }
            }
        }

        private void UpdateDisplay()
        {
            // Clear the current display.
            pnlPictures.Controls.Clear();

            // Row and Col will track the current position where pictures are
            // being inserted. They begin at the top-right corner.
            int row = border, col = border;

            // Iterate through the Images collection, and create PictureBox controls.
            foreach (NamedImage image in images)
            {
                PictureBox pic = new PictureBox();
                pic.Image = image.Image;
                pic.Tag = image.FileName;
                pic.Size = new Size(dimension, dimension);
                pic.Location = new Point(col, row);
                pic.BorderStyle = BorderStyle.FixedSingle;

                // StrechImage mode gives us the "thumbnail" ability.
                pic.SizeMode = PictureBoxSizeMode.StretchImage;

                // Display the picture.
                pnlPictures.Controls.Add(pic);

                // Move to the next column.
                col += dimension + spacing;

                // Handle the event.
                pic.Click += new EventHandler(this.pic_Click);

                // Move to next line if no more pictures will fit.
                if ((col + dimension + spacing + border) > this.Width)
                {
                    col = border;
                    row += dimension + spacing;
                }
            }

        }


        public void RefreshImages()
        {
            GetImages();
            UpdateDisplay();
        }


        protected override void OnSizeChanged(System.EventArgs e)
        {
            UpdateDisplay();
            base.OnSizeChanged(e);
        }


        private PictureBox picSelected;

        private void pic_Click(object sender, System.EventArgs e)
        {
            // Clear the border style from the last selected picture box.
            if (picSelected != null)
            {
                picSelected.BorderStyle = BorderStyle.FixedSingle;
            }

            // Get the new selection.
            picSelected = (PictureBox)sender;
            picSelected.BorderStyle = BorderStyle.Fixed3D;

            // Fire the selection event.
            PictureSelectedEventArgs args = new 
                PictureSelectedEventArgs((string)picSelected.Tag, picSelected.Image);
            if (PictureSelected != null)
            {
                PictureSelected(this, args);
            }

        }



        private class NamedImage
        {
            public Image Image;
            public string FileName;

            public NamedImage(Image image, string fileName)
            {
                this.Image = image;
                this.FileName = fileName;
            }
        }

    
    }
    public class PictureSelectedEventArgs : EventArgs
    {
        public string FileName;
        public Image Image;

        public PictureSelectedEventArgs(String fileName, Image image)
        {
            this.FileName = fileName;
            this.Image = image;
        }

    }


    
}


           
          


Bitmap Other



   

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace BitmapOther_c
{
    /// <summary>
    /// Summary description for BitmapOther.
    /// </summary>
  public class BitmapOther : System.Windows.Forms.Form
  {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public BitmapOther()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

        #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      // 
      // BitmapOther
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Name = "BitmapOther";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "BitmapOther";
      this.Load += new System.EventHandler(this.BitmapOther_Load);

    }
        #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new BitmapOther());
    }

    private void BitmapOther_Load(object sender, System.EventArgs e)
    {
    
    }

    protected override void OnPaint ( PaintEventArgs e )
    {
      Bitmap BMP = new Bitmap("Colorbars.JPG");
      Point Pt = new Point(20,20);

   //   BMP.SetPixel(15,20,Color.Black);
      BMP.MakeTransparent( BMP.GetPixel(15,25) );

      e.Graphics.DrawImage(BMP, Pt);
      e.Graphics.DrawLine(new Pen(Brushes.GreenYellow,30),60,60,200,60);


      
    }
   }
    }



           
          


BitmapOther-c.zip( 5 k)