.NET Class 4 – Windows-based Application. WinForm Application Homogeny programming model. Rich class library Classes are shared by all.NET languages.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
110-H1 More VB Tools Creating a Menu: What is a menu? a group of (related) commands displayed at at the top of an application Top level menu Disabled command.
User Interface Programming in C#: Graphics
C# - Files and Streams Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
© by Pearson Education, Inc. All Rights Reserved. continued …
Programming Based on Events
Advanced Object-Oriented Programming Features
Copyright © 2012 Pearson Education, Inc. Chapter 2 Introduction to Visual C#
Chapter 13: Advanced GUI and Graphics
Creating Menu. Objectives Create a menu system for a form –Create a menu –Create a menu titles –Create a menu items –Create a submenu –Modify menu –Edit.
C# Programming: From Problem Analysis to Program Design1 Introduction to Windows Programming C# Programming: From Problem Analysis to Program Design 3.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
1 Chapter 26 D&D – Graphics Outline 26.1 Introduction 26.3 Graphics Contexts and Graphics Objects 26.4 Color Control 26.5 Font Control 26.6 Drawing Lines,
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Beginning Programming with the Visual Studio.NET Environment.
Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to.
Dr Dat Tran - Week 4 Lecture Notes 1 MenuStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences &
1 Windows Graphics. 2 Objectives You will be able to Use the Windows GDI+ to draw arbitrary figures and text on a Windows form. Add a handler for the.
Object Oriented Programming Graphical User Interfaces Dr. Mike Spann
Lecture 7: WinForms & Controls, Part MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Visual Studio.NET ships with a wealth.
Object Oriented Programming Graphics and Multimedia Dr. Mike Spann
CSC 298 Windows Forms.
Getting Started The structure of a simple wxWidgets program, Look at where and how a wxWidgets application starts and ends, how to show the main window,
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
1 Working with Menus and Dialog Boxes. 2 Objectives You will be able to Create and edit menus for Windows Forms applications. Write code to handle menu.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Dr Dat Tran - Week 4 Lecture Notes 1 ToolStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences &
G RAPHICAL U SER I NTERFACE C ONCEPTS : P ART 1 1 Outline Introduction Windows Forms Event-Handling Model - Basic Event Handling.
1 Scroll Bars Providing Scrollbars. 2 Objectives You will be able to: Use Windows Graphics to display tabular information on a Windows form. Add graphics.
COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual Programming.
BIL528 – Bilgisayar Programlama II Introduction 1.
Lecture 6: Introduction to Graphical User Interfaces (GUIs)
Introduction to Windows Programming
Visual Basic.NET BASICS Lesson 1 A First Look at Microsoft Visual Basic.NET.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Controls. Adding Controls to Form -You can pick controls from the toolbox. -To add the controls from Toolbox to the Form You have be in design view. -To.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Microsoft Visual Basic 2005 BASICS Lesson 1 A First Look at Microsoft Visual Basic.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Ray Konopka Creating Custom Microsoft.NET Framework Controls in Delphi.
Object-Oriented Application Development Using VB.NET 1 Chapter 10 VB.NET GUI Components Overview.
Ray Konopka Developing Custom.NET Components DevCon Course No: 3118.
Slide 1 Using Menu Bar & Common Dialog Boxes. Slide 2 Setting Up the Main Items v First open the form on which you want the menu located v Then start.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Windows Programming C# Software Development. Overview  DLLs / PInvoke  DirectX .NET Component Library  Custom Controls  Event Model (in C++)  GUI.
Compunet Corporation Programming with Visual Basic.NET Working with Menus and Dialog Boxes Week 14 Tariq Aziz and Kevin Jones.
Graphical User Interface Components Version 1.1. Obectives Students should understand how to use these basic Form components to create a Graphical User.
Appendix A: Windows Forms. 2 Overview Describe the structure of a Windows Forms application –application entry point –forms –components and controls Introduce.
User Interface Programming in C#: Basics and Events Chris North CS 3724: HCI.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
Customizing Menus and Toolbars CHAPTER 12 Customizing Menus and Toolbars.
Windows form programming. namespace MyNamespace { public class MyForm : System.Windows.Forms.Form { public MyForm() { this.Text = "Hello Form"; }
Module 3: Creating Windows-based Applications. Overview Creating the Main Menu Creating and Using Common Dialog Boxes Creating and Using Custom Dialog.
Excel Tutorial 8 Developing an Excel Application
Computing with C# and the .NET Framework
Object Oriented Programming
Chapter 2 – Introduction to the Visual Studio .NET IDE
Graphics and Multimedia
Windows Desktop Applications
EE 422C Java FX.
How to Add Images Using an 'openFile' Dialog
Chapter 2 – Introduction to the Visual Studio .NET IDE
CASE Tools Graphical User Interface Programming Using C#
Copyright © 2006 Thomas P. Skinner
Module 8: Creating Windows-based Applications
Presentation transcript:

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