using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Win32; public class Form1 : Form { private System.Windows.Forms.ListBox listBoxMessages; private System.Windows.Forms.Button buttonChooseColor; private ColorDialog chooseColorDialog = new ColorDialog(); public Form1() { this.listBoxMessages = new System.Windows.Forms.ListBox(); this.buttonChooseColor = new System.Windows.Forms.Button(); this.SuspendLayout(); this.listBoxMessages.Size = new System.Drawing.Size(288, 199); this.buttonChooseColor.Location = new System.Drawing.Point(0, 208); this.buttonChooseColor.Size = new System.Drawing.Size(104, 23); this.buttonChooseColor.Text = "Choose Color"; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 232); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.buttonChooseColor, this.listBoxMessages}); this.ResumeLayout(false); buttonChooseColor.Click += new EventHandler(OnClickChooseColor); try { if (ReadSettings() == false) listBoxMessages.Items.Add("No information in registry"); else listBoxMessages.Items.Add("Information read in from registry"); StartPosition = FormStartPosition.Manual; } catch (Exception e) { listBoxMessages.Items.Add("A problem occurred reading in data from registry:"); listBoxMessages.Items.Add(e.Message); } } void OnClickChooseColor(object Sender, EventArgs e) { if (chooseColorDialog.ShowDialog() == DialogResult.OK) BackColor = chooseColorDialog.Color; } void SaveSettings() { RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software", true); RegistryKey wroxKey = softwareKey.CreateSubKey("JJJ"); RegistryKey selfPlacingWindowKey = wroxKey.CreateSubKey("SelfPlacingWindow"); selfPlacingWindowKey.SetValue("BackColor", (object)BackColor.ToKnownColor()); selfPlacingWindowKey.SetValue("Red", (object)(int)BackColor.R); selfPlacingWindowKey.SetValue("Green", (object)(int)BackColor.G); selfPlacingWindowKey.SetValue("Blue", (object)(int)BackColor.B); selfPlacingWindowKey.SetValue("Width", (object)Width); selfPlacingWindowKey.SetValue("Height", (object)Height); selfPlacingWindowKey.SetValue("X", (object)DesktopLocation.X); selfPlacingWindowKey.SetValue("Y", (object)DesktopLocation.Y); selfPlacingWindowKey.SetValue("WindowState", (object)WindowState.ToString()); } bool ReadSettings() { RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software"); RegistryKey wroxKey = softwareKey.OpenSubKey("JJJ"); if (wroxKey == null) return false; RegistryKey selfPlacingWindowKey = wroxKey.OpenSubKey("SelfPlacingWindow"); if (selfPlacingWindowKey == null) return false; else listBoxMessages.Items.Add("Successfully opened key " + selfPlacingWindowKey.ToString()); int redComponent = (int)selfPlacingWindowKey.GetValue("Red"); int greenComponent = (int)selfPlacingWindowKey.GetValue("Green"); int blueComponent = (int)selfPlacingWindowKey.GetValue("Blue"); this.BackColor = Color.FromArgb(redComponent, greenComponent, blueComponent); listBoxMessages.Items.Add("Background color: " + BackColor.Name); int X = (int)selfPlacingWindowKey.GetValue("X"); int Y = (int)selfPlacingWindowKey.GetValue("Y"); this.DesktopLocation = new Point(X, Y); listBoxMessages.Items.Add("Desktop location: " + DesktopLocation.ToString()); this.Height = (int)selfPlacingWindowKey.GetValue("Height"); this.Width = (int)selfPlacingWindowKey.GetValue("Width"); listBoxMessages.Items.Add("Size: " + new Size(Width, Height).ToString()); string initialWindowState = (string)selfPlacingWindowKey.GetValue("WindowState"); listBoxMessages.Items.Add("Window State: " + initialWindowState); this.WindowState = (FormWindowState)FormWindowState.Parse (WindowState.GetType(), initialWindowState); return true; } [STAThread] static void Main() { Application.Run(new Form1()); } }
Not in TaskBar
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 { private Button myButton; public Form1() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.ShowInTaskbar=false; myButton = new Button(); myButton.Text = "Minimize the window and you won't find it in TaskBar"; myButton.Location = new System.Drawing.Point(64, 32); myButton.Size = new System.Drawing.Size(450, 50); Controls.Add(myButton); } static void Main() { Application.Run(new Form1()); } }
MinimumWindow Size
using System; using System.Drawing; using System.Windows.Forms; class HowdyWorldFullFit: Form { public static void Main() { Application.Run(new HowdyWorldFullFit()); } public HowdyWorldFullFit() { ResizeRedraw = true; MinimumSize = SystemInformation.MinimumWindowSize + new Size(0,1); } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Font font = new Font("Times New Roman", 10, FontStyle.Italic); SizeF sizef = grfx.MeasureString(Text, font); float fScaleHorz = cx / sizef.Width; float fScaleVert = cy / sizef.Height; grfx.ScaleTransform(fScaleHorz, fScaleVert); grfx.DrawString(Text, font, new SolidBrush(clr), 0, 0); } }
Full screen and KeyEvent and MouseEvent
// This Article is a simple one to show the usage of KeyEvent and MouseEvent. // Please move the mouse or press a key to Exit this Program. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ScreenSave { public class KeyEventandMouseEvent : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Timer timer1; private System.ComponentModel.IContainer components; private Boolean boolFlag; private int ixStart; private int iyStart; public KeyEventandMouseEvent() { // // Required for Windows Form Designer support // InitializeComponent(); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // timer1 // this.timer1.Enabled = true; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // label1 // this.label1.Font = new System.Drawing.Font("Times New Roman", 14F, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.label1.ForeColor = System.Drawing.Color.Lime; this.label1.Location = new System.Drawing.Point(8, 280); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(312, 32); this.label1.TabIndex = 0; this.label1.Text = "Roller - Developed in C-Sharp"; this.label1.Click += new System.EventHandler(this.label1_Click); // // KeyEventandMouseEvent // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64))); this.ClientSize = new System.Drawing.Size(568, 32); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label1}); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.KeyPreview = true; this.Name = "KeyEventandMouseEvent"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "KeyEventandMouseEvent"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.OnKeyPressEvent); this.Load += new System.EventHandler(this.KeyEventandMouseEvent_Load); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMoveEvent); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new KeyEventandMouseEvent()); } private void timer1_Tick(object sender, System.EventArgs e) { int i; i = label1.Location.X; if (i >= 750) i = 0; else i = label1.Location.X + 5; label1.Location = new Point(i,280); } private void KeyEventandMouseEvent_Load(object sender, System.EventArgs e) { } private void OnMouseMoveEvent(object sender,MouseEventArgs e) { //Application.Exit(); if (ixStart == 0 && iyStart == 0) { ixStart = e.X; iyStart = e.Y; } else if (e.X != ixStart || e.Y != iyStart) Application.Exit(); } private void OnKeyPressEvent(object sender, System.Windows.Forms.KeyPressEventArgs e) { Application.Exit(); } private void label1_Click(object sender, System.EventArgs e) { } } }
Group Box mouse enter event
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 { private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.CheckBox checkBox1; private System.Windows.Forms.CheckBox checkBox2; private System.Windows.Forms.CheckBox checkBox3; private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.RadioButton radioButton3; private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.checkBox1 = new System.Windows.Forms.CheckBox(); this.checkBox2 = new System.Windows.Forms.CheckBox(); this.checkBox3 = new System.Windows.Forms.CheckBox(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.radioButton3 = new System.Windows.Forms.RadioButton(); this.button1 = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.radioButton1, this.radioButton2, this.radioButton3}); this.groupBox1.Location = new System.Drawing.Point(8, 120); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(120, 144); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Color"; this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter); // // checkBox1 // this.checkBox1.Location = new System.Drawing.Point(8, 8); this.checkBox1.Name = "checkBox1"; this.checkBox1.TabIndex = 1; this.checkBox1.Text = "Circle"; // // checkBox2 // this.checkBox2.Location = new System.Drawing.Point(8, 40); this.checkBox2.Name = "checkBox2"; this.checkBox2.TabIndex = 2; this.checkBox2.Text = "Rectangle"; // // checkBox3 // this.checkBox3.Location = new System.Drawing.Point(8, 72); this.checkBox3.Name = "checkBox3"; this.checkBox3.TabIndex = 3; this.checkBox3.Text = "String"; // // radioButton1 // this.radioButton1.Location = new System.Drawing.Point(8, 32); this.radioButton1.Name = "radioButton1"; this.radioButton1.TabIndex = 4; this.radioButton1.Text = "Red"; this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); // // radioButton2 // this.radioButton2.Location = new System.Drawing.Point(8, 64); this.radioButton2.Name = "radioButton2"; this.radioButton2.TabIndex = 5; this.radioButton2.Text = "Green"; // // radioButton3 // this.radioButton3.Location = new System.Drawing.Point(8, 96); this.radioButton3.Name = "radioButton3"; this.radioButton3.TabIndex = 6; this.radioButton3.Text = "Blue"; // // button1 // this.button1.Location = new System.Drawing.Point(8, 280); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(112, 32); this.button1.TabIndex = 4; this.button1.Text = "Draw"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(408, 317); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1, this.checkBox3, this.checkBox2, this.checkBox1, this.groupBox1}); this.Name = "Form1"; this.Text = "CheckBox and RadioButton Sample"; this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new Form1()); } private void groupBox1_Enter(object sender, System.EventArgs e) { Console.WriteLine("group box enter event"); } private void radioButton1_CheckedChanged(object sender, System.EventArgs e) { Console.WriteLine("Radio Button checked changed event"); } private void button1_Click(object sender, System.EventArgs e) { Graphics g = Graphics.FromHwnd(this.Handle); String str = ""; Rectangle rc = new Rectangle(150, 50, 250, 250); if(radioButton1.Checked) { str = "red"; } if(radioButton2.Checked) { str+="Green"; } if(radioButton3.Checked) { str+="Blue"; } if (checkBox1.Checked) { str+="Ellipse"; } if (checkBox2.Checked) { str += "Rectangle"; } if (checkBox3.Checked) { g.FillRectangle(new SolidBrush(Color.White), rc); g.DrawString(str, new Font("Verdana", 12), new SolidBrush(Color.Black), rc); } } }
Add control to a form window
using System; using System.Windows.Forms; using System.Drawing; public class PushMe2 : Form { Button pushMeButton; public PushMe2() { pushMeButton = new Button(); pushMeButton.Text = "Push Me"; pushMeButton.Height = 60; pushMeButton.Width = 80; pushMeButton.Top = 60; pushMeButton.Left = 60; pushMeButton.Click += new EventHandler(ButtonClicked); this.Controls.Add(pushMeButton); this.Height = 200; this.Width = 200; this.StartPosition = FormStartPosition.CenterScreen; this.Visible = true; } public void ButtonClicked(object source, EventArgs e) { Button b = (Button)source; if ( b.Text == "Push Me" ) { b.Text = "Ouch"; } else { b.Text = "Push Me"; } } static void Main() { Application.Run(new PushMe2()); } }
Change Form window background
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form{ private System.ComponentModel.IContainer components; public Form1(){ InitializeComponent(); } #region Windows Form Designer generated code private void InitializeComponent(){ this.components = new System.ComponentModel.Container(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.timer1.Enabled = true; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); this.AutoScaleBaseSize = new System.Drawing.Size(6, 15); this.ClientSize = new System.Drawing.Size(292, 260); this.Name = "Form1"; this.Text = "LinearGradientBrush Demo"; this.Load += new System.EventHandler(this.Form1_Load); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } #endregion static void Main(){ Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { this.BackColor = Color.FromArgb(255, 0, 0, 255); } private System.Windows.Forms.Timer timer1; private float angle = 0; private LinearGradientBrush GetBrush() { return new LinearGradientBrush( new Rectangle( 20, 20, 200, 100), Color.Orange, Color.Yellow, 0.0F, true); } private void Rotate( Graphics graphics, LinearGradientBrush brush ) { brush.RotateTransform(angle); brush.SetBlendTriangularShape(.5F); graphics.FillRectangle(brush, brush.Rectangle); } private void Rotate(Graphics graphics) { angle += 5 % 360; Rotate(graphics, GetBrush()); } private void timer1_Tick(object sender, System.EventArgs e) { Rotate(CreateGraphics()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Rotate(e.Graphics); } }