190 likes | 225 Views
Learn about .NET WinForms programming model for building Windows-based GUI applications using the rich class library provided by .NET. Includes classes like Form, Menu, TextBox, Button, ListView, and more.
E N D
.NET Class 4 – Windows-based Application
WinForm Application • Homogeny programming model. • Rich class library • Classes are shared by all .NET languages. • A programming model for GUI applications.
The Windows Forms Programming Model • A Form is a top level window. • An application’s main window is a form, and any other top-level window the application has. • Using .NET namespaces, mainly System.WinForms. • Includes classes such as: • Form (behavior of windows) • Menu • Control classes: TextBox, Button, ListView etc. (referred as Button or System.WinForms.Button)
WinForms Prog. Model - Cont. • An instance of WinForms.Form represents the main application window. • Needed properties are accessed easily: • Form’s border style – BorderStyle property. • Any windows application must run a static method Run of an System.WinForms.Application class. This method displays the window and provides a message loop. (Messages – for instance, mouse moves, input, choice from menus etc.)
Each class contains some virtual methods which can be overridden, such as a respond to a ‘message’ like mouse movement. • System.WinForms.Form.OnPaint() is called when a form’s client area needs updating.
Form’s events • All control classes fire events – such as: System.WinForms.Button fire Click events when they are clicked. • To respond to a button click – the following code must be included: MyButton.Click += new EventHandler (OnButtonClicked); ••• private void OnButtonClicked(object sender, EventArgs e) { MessageBox.Show ("Click!"); } • EventHandlers are delegates – defined in the System Namespage. • The first parameter OnButtonClicked: who fired the event. • The second parameter, can be ignored in click events, may be used when more information should be passed.
Hello World Windows Forms using System; using System.WinForms; using System.Drawing; public class MyForm : Form { public MyForm () { Text = "Windows Forms Demo"; } protected override void OnPaint (PaintEventArgs e) { e.Graphics.DrawString ("Hello, world", Font, new SolidBrush (Color.Black), ClientRectangle); } public static void Main (string[] args) { Application.Run (new MyForm ()); } }
In the constructor, the Text property is initialized (title of window). • Override virtual OnPaint – which is responsible of rendering the form on screen. Here, it will write “Hello World” in the form’s client area. • OnPaint is passed PaintEventArgs object, which contains properties such as Graphics and ClipRectangle. • Graphics – reference to a Graphics object (device context). • ClipRectangle – a rectangle object which describes which part of the form is invalid.
ImageView Application (Ver. 1) using System; using System.WinForms; using System.Drawing; public class MyForm : Form { public MyForm () { // Set the form's title Text = "Image Viewer"; // Set the form's size ClientSize = new Size (640, 480); } public static void Main (string[] args) { Application.Run (new MyForm ()); } }
ImageView Application (Ver. 2)(Add an Option Menu) public class MyForm : Form { public MyForm () { ... // Create a menu MainMenu menu = new MainMenu (); MenuItem item = menu.MenuItems.Add ("&Options"); item.MenuItems.Add (new MenuItem ("E&xit", new EventHandler (OnExit))); // Attach the menu to the form Menu = menu; } // Handler for the Exit command private void OnExit (object sender, EventArgs e) { Close (); } ... public static void Main (string[] args) { Application.Run (new MyForm ()); } }
Add An Open Command (Ver. 3) public class MyForm : Form { protected int _FilterIndex = -1; protected Bitmap _MyBitmap; public MyForm () {... MenuItem item = menu.MenuItems.Add ("&Options"); item.MenuItems.Add (new MenuItem ("&Open...", new EventHandler (OnOpenImage), Shortcut.CtrlO)); item.MenuItems.Add ("-"); item.MenuItems.Add (new MenuItem ("E&xit", new EventHandler (OnExit))); // Attach the menu to the form Menu = menu; } ...
// Handler for the Open command private void OnOpenImage (object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog (); ofd.Filter = "Image Files (JPEG, GIF, BMP, etc.)|" + "*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.png|" + "JPEG files (*.jpg;*.jpeg)|*.jpg;*.jpeg|" + "GIF Files (*.gif)|*.gif|" + "BMP Files (*.bmp)|*.bmp|" + "TIFF Files (*.tif;*.tiff)|*.tif;*.tiff|" + "PNG Files (*.png)|*.png|" + "All files (*.*)|*.*"; if (_FilterIndex != -1) ofd.FilterIndex = _FilterIndex; if (ofd.ShowDialog() == DialogResult.OK) { String fileName = ofd.FileName; if (fileName.Length != 0) { _FilterIndex = ofd.FilterIndex; try { _MyBitmap = new Bitmap (fileName); Text = "Image Viewer - " +fileName; Invalidate (); } catch { MessageBox.Show (String.Format ("{0} is not " + "a valid image file", fileName), "Error", MessageBox.OK | MessageBox.IconError); } } } } ...
Ver. 3 notes • Shortcut -- System.WinForms enumeration. • OnOpenImage creates an OpenFile Dialog object, initializes it with the filter string, for ‘files of type’ field. • If user clicks ‘ok’ name of file is extracted by reading it from OpenFileDialog’s FileName proerty. • Bitmap is a System.Drawing.Bitmap shorthand, for handling image files. • Try-catch to avoid opening a non-image file. • MessageBox.Show method is used to print and error. • Invalidating is a Form method that forces a repaint by invalidating a form’s client area. • Still no code to render the image into the form.
Notes – Ver. 4 – Override OnPaint • Make use of the Graphics object: Graphics g=e.Graphics; g.Drawings(_MyBitmap, 0, 0, _MyBitmap.Width, _MyBitmap.Height); • DrawImage is doing the actual painting.
Notes – Ver 5 – Add Scroll Bars. • Use a pair of properties from System.WinForms.ScrolableControl. • Enable (boolean) autoscrolling – scroll will automatically apear • AutoScrollMinSize is set to the chosen BitMap • Add scroll parameters to ‘OnPaint’ to locate the scroll bars on windows.
Notes – ver. 6 – a size fit option • Add a field to store user’s preference. • Add the menu items in constructor. • Add command handlers. • SetStyle – called with the ControlStyles parameter ResizeRedraw
Notes – ver 7 – Add code to check/uncheck menu items • Handle the choice when menu is opened • One event handler • Connected to the item.Popup (main option choice event)