//---------------------------------------------------------------------------- // Copyright (C) 2013-2015 Fabrice HARROUET (ENIB) // // Permission to use, copy, modify, distribute and sell this software // and its documentation for any purpose is hereby granted without fee, // provided that the above copyright notice appear in all copies and // that both that copyright notice and this permission notice appear // in supporting documentation. // The author makes no representations about the suitability of this // software for any purpose. // It is provided "as is" without express or implied warranty. //---------------------------------------------------------------------------- using System; using System.Windows.Forms; using System.Drawing; public class MyForm : Form { protected static int _count=0; public MyForm() // see http://zetcode.com/gui/csharpwinforms/ { ++_count; this.Text=_count+": Hello C# World"; this.Size=new Size(250,250); try { this.Icon=new Icon("favicon.ico"); } catch(Exception e) { Console.WriteLine("!!! {0}",e); } MainMenu mainMenu=new MainMenu(); MenuItem file=mainMenu.MenuItems.Add("&File"); file.MenuItems.Add(new MenuItem("Close &Window", new EventHandler(this._onClose), Shortcut.CtrlW)); file.MenuItems.Add(new MenuItem("&Quit", new EventHandler(this._onQuit), Shortcut.CtrlQ)); this.Menu=mainMenu; Panel pnl1=new Panel(); pnl1.Parent=this; pnl1.Dock=DockStyle.Fill; Panel pnl2=new Panel(); pnl2.Parent=this; pnl2.Dock=DockStyle.Bottom; pnl2.SizeChanged+=new EventHandler(_onSizeChanged); StatusBar sb=new StatusBar(); sb.Parent=this; sb.Text="Ready"; TextBox tb=new TextBox(); tb.Parent=pnl1; tb.Dock=DockStyle.Fill; tb.Multiline=true; Button btn1=new Button(); btn1.Parent=pnl2; btn1.Text="Btn1"; btn1.Location=new Point(30,10); btn1.Click+=new EventHandler(_onButtonClick); btn1.MouseEnter+=new EventHandler(_onMouseEnter); Button btn2=new Button(); btn2.Parent=pnl2; btn2.Text="Btn2"; btn2.Location=new Point(100,10); btn2.Anchor=AnchorStyles.Right; btn2.Click+=new EventHandler(_onButtonClick); btn2.MouseEnter+=new EventHandler(_onMouseEnter); this.Closed+=new EventHandler(_onClosed); this.CenterToScreen(); Console.WriteLine("Form Handle: {0:x}",(ulong)Handle); } protected void _onSizeChanged(object sender, EventArgs evt) { Console.WriteLine("_onSizeChanged: sender={0} evt={1}",sender,evt); Console.WriteLine("Handle: {0:X} Width: {1} Height: {2}", (ulong)Handle,Width,Height); } protected void _onButtonClick(object sender, EventArgs evt) { Console.WriteLine("_onButtonClick: sender={0} evt={1}",sender,evt); if(_count>1) MessageBox.Show("Button Clicked!"); else new MyForm().Show(); } protected void _onMouseEnter(object sender, EventArgs evt) { Console.WriteLine("_onMouseEnter: sender={0} evt={1}",sender,evt); } protected void _onClose(object sender, EventArgs evt) { Console.WriteLine("_onClose: sender={0} evt={1}",sender,evt); Close(); } protected void _onQuit(object sender, EventArgs evt) { Console.WriteLine("_onQuit: sender={0} evt={1}",sender,evt); Application.Exit(); } protected void _onClosed(object sender, EventArgs evt) { Console.WriteLine("_onClosed: sender={0} evt={1}",sender,evt); if(--_count==0) { Console.WriteLine("Last close"); Application.Exit(); } } } public class Test { public static void Main(string[] args) { new MyForm().Show(); Application.Run(); } } //----------------------------------------------------------------------------