1 / 19

.NET

.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.

arlais
Télécharger la présentation

.NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. .NET Class 4 – Windows-based Application

  2. WinForm Application • Homogeny programming model. • Rich class library • Classes are shared by all .NET languages. • A programming model for GUI applications.

  3. 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)

  4. 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.)

  5. 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.

  6. 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.

  7. 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 ()); } }

  8. 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.

  9. 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 ()); } }

  10. 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 ()); } }

  11. 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; } ...

  12. // 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); } } } } ...

  13. 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.

  14. 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.

  15. 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.

  16. 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

  17. 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)

More Related