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 OnResize(EventArgs ea)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics graphics = pea.Graphics;
string str = "Location: " + Location + "
" +
"ClientRectangle: " + ClientRectangle;
graphics.DrawString(str, Font, Brushes.Black, 0, 0);
}
}
Form OnMove event
using System;
using System.Drawing;
using System.Windows.Forms;
class FormSize: Form
{
public static void Main()
{
Application.Run(new FormSize());
}
public FormSize()
{
Text = "Form Size";
BackColor = Color.White;
}
protected override void OnMove(EventArgs ea)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics graphics = pea.Graphics;
string str = "Location: " + Location + "
" +
"ClientRectangle: " + ClientRectangle;
graphics.DrawString(str, Font, Brushes.Black, 0, 0);
}
}
OnClick event
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
class PrintableForm: Form
{
public static void Main()
{
Application.Run(new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected override void OnClick(EventArgs ea)
{
PrintDocument prndoc = new PrintDocument();
prndoc.DocumentName = Text;
prndoc.PrintPage += new PrintPageEventHandler(PrintDocumentOnPrintPage);
prndoc.Print();
}
void PrintDocumentOnPrintPage(object obj, PrintPageEventArgs ppea)
{
Graphics grfx = ppea.Graphics;
SizeF sizef = grfx.VisibleClipBounds.Size;
DoPage(grfx, Color.Black, (int)sizef.Width, (int)sizef.Height);
}
protected virtual void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Pen pen = new Pen(clr);
grfx.DrawLine(pen, 0, 0, cx - 1, cy - 1);
grfx.DrawLine(pen, cx - 1, 0, 0, cy - 1);
}
}
OnKeyDown event
using System;
using System.Drawing;
using System.Windows.Forms;
class ExitOnX: Form
{
public static void Main()
{
Application.Run(new ExitOnX());
}
public ExitOnX()
{
Text = "Exit on X";
}
protected override void OnKeyDown(KeyEventArgs kea)
{
if (kea.KeyCode == Keys.X)
Close();
}
}
On Mouse Wheel
using System;
using System.Drawing;
using System.Windows.Forms;
class PoePoem: Form
{
public static void Main()
{
if (!SystemInformation.MouseWheelPresent)
{
Console.WriteLine("Program needs a mouse with a mouse wheel!");
return;
}
Application.Run(new PoePoem());
}
public PoePoem()
{
}
protected override void OnMouseWheel(MouseEventArgs mea)
{
Console.WriteLine(mea.Delta);
}
}
OnMouseEnter, OnMouseHover, OnMouseLeave event
using System;
using System.Drawing;
using System.Windows.Forms;
class EnterLeave: Form
{
bool bInside = false;
public static void Main()
{
Application.Run(new EnterLeave());
}
public EnterLeave()
{
}
protected override void OnMouseEnter(EventArgs ea)
{
bInside = true;
Invalidate();
}
protected override void OnMouseLeave(EventArgs ea)
{
bInside = false;
Invalidate();
}
protected override void OnMouseHover(EventArgs ea)
{
Graphics grfx = CreateGraphics();
grfx.Clear(Color.Red);
System.Threading.Thread.Sleep(500);
grfx.Clear(Color.Green);
grfx.Dispose();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
grfx.Clear(bInside ? Color.Green : BackColor);
}
}
Scrolling (AutoScrollMinSize)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
[STAThread]
static void Main() {
Application.Run(new Form1());
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g;
g = Graphics.FromHwnd(this.Handle);
GraphicsUnit units = GraphicsUnit.Pixel;
string path = "your.jpg";
Image im = Image.FromFile(path);
this.AutoScrollMinSize = new Size(im.Width, im.Height);
//this.AutoScroll = true;
Point P = this.AutoScrollPosition;
Rectangle dstR = this.ClientRectangle;
RectangleF srcR = new RectangleF(-P.X, -P.Y, dstR.Width, dstR.Height);
g.DrawImage(im, dstR, srcR, units);
g.Dispose();
}
}

