Set ClientSize to change the form window size

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

class AutoScaleDemo: Form
{
public static void Main()
{
Application.Run(new AutoScaleDemo());
}
public AutoScaleDemo()
{
Font = new Font(“Arial”, 12);
FormBorderStyle = FormBorderStyle.FixedSingle;

int[] aiPointSize = { 8, 12, 16, 24, 32 };

for (int i = 0; i < aiPointSize.Length; i++) { Button btn = new Button(); btn.Parent = this; btn.Text = "Use " + aiPointSize[i] + "-point font"; btn.Tag = aiPointSize[i]; btn.Location = new Point(4, 16 + 24 * i); btn.Size = new Size(80, 16); btn.Click += new EventHandler(ButtonOnClick); } ClientSize = new Size(88, 16 + 24 * aiPointSize.Length); AutoScaleBaseSize = new Size(4, 8); } protected override void OnPaint(PaintEventArgs pea) { pea.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), 0, 0); } void ButtonOnClick(object obj, EventArgs ea) { Button btn = (Button) obj; SizeF sizefOld = GetAutoScaleSize(Font); Font = new Font(Font.FontFamily, (int) btn.Tag); SizeF sizefNew = GetAutoScaleSize(Font); Scale(sizefNew.Width / sizefOld.Width,sizefNew.Height / sizefOld.Height); } } [/csharp]

Set ResizeRedraw property

   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class RandomClearResizeRedraw: Form
{
     public static void Main()      
     {
          Application.Run(new RandomClearResizeRedraw());
     }
     public RandomClearResizeRedraw()      
     {
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics graphics = pea.Graphics;
          Random   rand = new Random();
   
          graphics.Clear(Color.FromArgb(rand.Next(256),
                                    rand.Next(256),
                                    rand.Next(256)));
     }
}

    


Use Font from Form to paint string on a form

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

class InheritHelloWorld : Form {
    public static void Main() {
        Application.Run(new InheritHelloWorld());
    }
    public InheritHelloWorld() {
        Text = "Inherit " + Text;
    }
    protected override void OnPaint(PaintEventArgs pea) {
        Graphics graphics = pea.Graphics;

        graphics.DrawString("Hello from InheritHelloWorld!",
                        Font, Brushes.Black, 0, 100);
    }
}

    


Print out Form size and position related information

   
 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class FormSize: Form
{
     public static void Main()
     {
          Application.Run(new FormSize());
     }
     public FormSize()
     {
          BackColor = Color.White;
     }
     protected override void OnMove(EventArgs ea)
     {
          Invalidate();
     }
     protected override void OnResize(EventArgs ea)
     {
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics graphics = pea.Graphics;
          string   str  = "Location: "        + Location        + "
"   +
                          "Size: "            + Size            + "
"   +
                          "Bounds: "          + Bounds          + "
"   +
                          "Width: "           + Width           + "
"   +
                          "Height: "          + Height          + "
"   +
                          "Left: "            + Left            + "
"   +
                          "Top: "             + Top             + "
"   +
                          "Right: "           + Right           + "
"   +
                          "Bottom: "          + Bottom          + "

" +
                          "DesktopLocation: " + DesktopLocation + "
"   +
                          "DesktopBounds: "   + DesktopBounds   + "

" +
                          "ClientSize: "      + ClientSize      + "
"   +
                          "ClientRectangle: " + ClientRectangle;
   
          graphics.DrawString(str, Font, Brushes.Black, 0, 0);
    }
}

    


Scroll Shapes

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 Point rectangleTopLeft = new Point(0, 0);
private Size rectangleSize = new Size(200, 200);
private Point ellipseTopLeft = new Point(50, 200);
private Size ellipseSize = new Size(200, 150);
private Pen bluePen = new Pen(Color.Blue, 3);
private Pen redPen = new Pen(Color.Red, 2);

protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
Graphics dc = e.Graphics;
Size scrollOffset = new Size(this.AutoScrollPosition);
if (e.ClipRectangle.Top + scrollOffset.Width < 350 || e.ClipRectangle.Left + scrollOffset.Height < 250) { Rectangle rectangleArea = new Rectangle (rectangleTopLeft + scrollOffset, rectangleSize); Rectangle ellipseArea = new Rectangle (ellipseTopLeft + scrollOffset, ellipseSize); dc.DrawRectangle(bluePen, rectangleArea); dc.DrawEllipse(redPen, ellipseArea); } } public Form1() { this.BackColor = Color.White; this.Size = new System.Drawing.Size(300, 300); this.AutoScrollMinSize = new Size(250, 350); } public static void Main() { Application.Run(new Form1()); } } [/csharp]

Form Paint event

   



using System;
using System.Drawing;
using System.Windows.Forms;
   
class PaintEvent
{
     public static void Main()
     {
          Form form   = new Form();
          form.Text   = "Paint Event";
          form.Paint += new PaintEventHandler(MyPaintHandler);
   
          Application.Run(form);
     }
     static void MyPaintHandler(object objSender, PaintEventArgs pea)
     {
          Graphics graphics = pea.Graphics;
   
          graphics.Clear(Color.Chocolate);
     }
}

           
          


Form Key Press action


   

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

public class Form1 : System.Windows.Forms.Form{
   public Form1() {
      InitializeComponent();
   }
   private void InitializeComponent() {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Name = "Form1";
      this.Text = "Form1";
      this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
   }

   static void Main() {
      Application.Run(new Form1());
   }

   private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
      Console.WriteLine("Key Press");
   }
}