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(); } }
GUI Windows Form
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); } }
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); } }
Form Focus event
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class FocusForm : System.Windows.Forms.Form { private System.Windows.Forms.TextBox txtFocusForm; private System.Windows.Forms.Button btFocusForm; public FocusForm() { this.txtFocusForm = new System.Windows.Forms.TextBox(); this.btFocusForm = new System.Windows.Forms.Button(); this.SuspendLayout(); this.txtFocusForm.Location = new System.Drawing.Point(8, 8); this.txtFocusForm.Size = new System.Drawing.Size(336, 20); this.txtFocusForm.LostFocus += new System.EventHandler(this.txtFocusForm_LostFocus); this.txtFocusForm.GotFocus += new System.EventHandler(this.txtFocusForm_GotFocus); this.btFocusForm.Location = new System.Drawing.Point(8, 40); this.btFocusForm.Size = new System.Drawing.Size(336, 23); this.btFocusForm.Click += new System.EventHandler(this.btFocusForm_Click); this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(352, 70); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.btFocusForm, this.txtFocusForm}); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new FocusForm()); } protected void txtFocusForm_LostFocus(object sender, EventArgs e) { Console.WriteLine("Goodbye!"); } protected void txtFocusForm_GotFocus(object sender, EventArgs e) { Console.WriteLine("Hello!"); } private void btFocusForm_Click(object sender, System.EventArgs e) { bool canFocus = txtFocusForm.CanFocus; bool containsFocus = this.ContainsFocus; bool focused = txtFocusForm.Focused; Console.WriteLine("Textbox can focus: " + canFocus + " Form children contain focus: " + containsFocus + " Textbox has focus: " + focused); txtFocusForm.Focus(); } }
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(); } }
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(); } }
Form Window event: closing, closed, load, activated, deactivated
using System; using System.Windows.Forms; using System.ComponentModel; public class MainWindow : Form { private string lifeTimeInfo; public MainWindow() { this.Closing += new CancelEventHandler(MainForm_Closing); this.Load += new EventHandler(MainForm_Load); this.Closed += new EventHandler(MainForm_Closed); this.Activated += new EventHandler(MainForm_Activated); this.Deactivate += new EventHandler(MainForm_Deactivate); } protected void MainForm_Closing(object sender, CancelEventArgs e) { DialogResult dr = MessageBox.Show("Do you REALLY want to close this app?", "Closing event!", MessageBoxButtons.YesNo); if (dr == DialogResult.No) e.Cancel = true; else e.Cancel = false; } protected void MainForm_Load(object sender, System.EventArgs e) { lifeTimeInfo += "Load event "; } protected void MainForm_Activated(object sender, System.EventArgs e) { lifeTimeInfo += "Activate event "; } protected void MainForm_Deactivate(object sender, System.EventArgs e) { lifeTimeInfo += "Deactivate event "; } protected void MainForm_Closed(object sender, System.EventArgs e) { lifeTimeInfo += "Closed event "; MessageBox.Show(lifeTimeInfo); } public static void Main(string[] args) { Application.Run(new MainWindow()); } }