home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Buttons and Labels and Scrolls (Oh, My!) / TwoButtons / TwoButtons.cs next >
Encoding:
Text File  |  2001-01-15  |  2.1 KB  |  71 lines

  1. //-----------------------------------------
  2. // TwoButtons.cs ⌐ 2001 by Charles Petzold
  3. //-----------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class TwoButtons: Form
  9. {
  10.      readonly Button btnLarger, btnSmaller;
  11.      readonly int    cxBtn, cyBtn, dxBtn;
  12.  
  13.      public static void Main()
  14.      {
  15.           Application.Run(new TwoButtons());
  16.      }
  17.      public TwoButtons()
  18.      {
  19.           Text = "Two Buttons";
  20.           ResizeRedraw = true;
  21.  
  22.           cxBtn = 5 * Font.Height;
  23.           cyBtn = 2 * Font.Height;
  24.           dxBtn =     Font.Height;
  25.  
  26.           btnLarger = new Button();
  27.           btnLarger.Parent = this;
  28.           btnLarger.Text   = "&Larger";
  29.           btnLarger.Size   = new Size(cxBtn, cyBtn);
  30.           btnLarger.Click += new EventHandler(ButtonOnClick);
  31.  
  32.           btnSmaller = new Button();
  33.           btnSmaller.Parent = this;
  34.           btnSmaller.Text   = "&Smaller";
  35.           btnSmaller.Size   = new Size(cxBtn, cyBtn);
  36.           btnSmaller.Click += new EventHandler(ButtonOnClick);
  37.  
  38.          OnResize(EventArgs.Empty);
  39.      }
  40.      protected override void OnResize(EventArgs ea)
  41.      {
  42.           base.OnResize(ea);
  43.  
  44.           btnLarger.Location =
  45.                          new Point(ClientSize.Width / 2 - cxBtn - dxBtn / 2,
  46.                                   (ClientSize.Height - cyBtn) / 2);
  47.           btnSmaller.Location =
  48.                          new Point(ClientSize.Width / 2 + dxBtn / 2,
  49.                                   (ClientSize.Height - cyBtn) / 2);
  50.      }
  51.      void ButtonOnClick(object obj, EventArgs ea)
  52.      {
  53.           Button btn = (Button) obj;
  54.  
  55.           if (btn == btnLarger)
  56.           {
  57.                Left   -= (int)(0.05 * Width);
  58.                Top    -= (int)(0.05 * Height);
  59.                Width  += (int)(0.10 * Width);
  60.                Height += (int)(0.10 * Height);
  61.           }
  62.           else
  63.           {
  64.                Left   += (int)(Width  / 22f);
  65.                Top    += (int)(Height / 22f);
  66.                Width  -= (int)(Width  / 11f);
  67.                Height -= (int)(Height / 11f);
  68.           }
  69.      }
  70. }
  71.