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);
}
}
Author: coder
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();
}
}
OnInputLanguageChanged
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class InternationalText : System.Windows.Forms.Form {
internal System.Windows.Forms.Label lblInternationalText;
internal System.Windows.Forms.Label lblCharCode;
private System.Windows.Forms.TextBox textBox1;
public InternationalText() {
this.lblInternationalText = new System.Windows.Forms.Label();
this.lblCharCode = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.lblInternationalText.Location = new System.Drawing.Point(8, 64);
this.lblInternationalText.Size = new System.Drawing.Size(288, 23);
this.lblCharCode.Location = new System.Drawing.Point(8, 96);
this.lblCharCode.Size = new System.Drawing.Size(88, 23);
this.textBox1.Location = new System.Drawing.Point(8, 24);
this.textBox1.Size = new System.Drawing.Size(288, 20);
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(304, 134);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.lblCharCode,
this.lblInternationalText});
this.MaximizeBox = false;
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new InternationalText());
}
protected override void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
MessageBox.Show(e.InputLanguage.Culture.Name);
}
protected override void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
MessageBox.Show(e.InputLanguage.Culture.Name);
}
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
lblInternationalText.Text += e.KeyChar.ToString();
lblCharCode.Text = ((int)e.KeyChar).ToString();
}
}