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.

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

PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
User Interface Programming in C#: Graphics
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
VB Controls and Events Week 7: Picture Box, Image Box, Option, Check box, Mouse over, Frames, Shapes.
Graphics and Multimedia. Outline Introduction Graphics Contexts and Graphics Objects Color Control.
Copyright © 2012 Pearson Education, Inc. Chapter 2 Introduction to Visual C#
1 Introduction to the Visual Studio.NET IDE Powerpoint slides modified from Deitel & Deitel.
1 Web Services Visual C# 2008 Step by Step Chapter 30.
InDesign CS3 Lesson 3 Working with Frames. Using Frames Frames are containers in which you place graphics or text. Frames can also be used as graphic.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
Graphics Images – PictureBox control Drawing graphics - Graphics object Multimedia controls PictureBox control Image property – select image Choose how.
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,
By: Zaiba Mustafa Copyright ©
Graphics and Multimedia In visual Studio. Net (C#)
Lecture Set 13 Drawing Mouse and Keyboard Events Part A - Drawing.
Getting Started Example ICS2O curriculum
1 Windows Printing. 2 Objectives You will be able to Output text and graphics to a printer. Print multipage documents. Use the standard Windows print.
Microsoft Visual Basic 2012 CHAPTER TWO Program and Graphical User Interface Design.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Hello World In C++ and Microsoft Visual C++. Directions to begin a project 1. Go to All Programs 2. Open Visual Studio C++ 3. Click on New Project 4.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface A Brief Introduction to GDI+ S.R.G. Fraser, Pro Visual C++/CLI.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
CST238 Week 5 Questions / Concerns? Announcements – HW#1 due (Project ideas) – Check-off Take Home lab#4 Recap New topics – Drawing Coming up: – GUI Bloopers.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
IE 411/511: Visual Programming for Industrial Applications
Graphics and Multimedia Part #2
BIM211 – Visual Programming Objects, Collections, and Events 1.
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.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
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.
Some graphics. Projects included A slideshow a dark moon moving phases of the moon billiards Your own icons and bitmaps Pie chart.
An Example of Windows Forms Applications Windows-based application –Win Forms Control structures (selection and repetition) Graphics Read integers from.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 13 Graphics, Animation, Sound and Drag-and-Drop.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 – Introduction to the Visual Studio .NET IDE
Lecture 5 Graphics Erick Pranata. » Graphics Overview » About GDI+ » Getting Started.
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.
Chapter 5. Shape tool  Vector images  Can be resized without loss of quality  Must rasterize to work with in some cases.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Exploring the Macromedia Flash Workspace – Lesson 2 1 Exploring the Macromedia Flash Workspace Lesson 2.
1 Graphic Device Interface (GDI). 2 Class Form A Form is a representation of any window displayed in your application. The Form class can be used to create.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Working with Shapes Guided Lesson. Objective  In this lesson you will learn how to insert a shape and format it by changing its fill color, outline color,
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to the Visual Studio.NET IDE Outline 2.1Introduction 2.2Visual Studio.NET Integrated.
Images Part 11 dbg. 2 Images The form and several controls can display a number of different types of image files in the BackgroundImage Property. These.
1.Begin by opening VB 2.Click New Project or the icon Hello World Tutorial NOTE: depending on your version of VB – the images might be slightly different.
List Boxes and Combo Boxes Provides a list of items to select from Various styles — choose based on Space available Need to select from an existing list.
Windows Programming C# Software Development. Overview  DLLs / PInvoke  DirectX .NET Component Library  Custom Controls  Event Model (in C++)  GUI.
Lecture Set 7 Procedures and Event Handlers Part B - The Structure of an Application Event Handlers.
Paint Tutorial Created February 2006 Start Paint: Start>Programs>Accessories>Paint.
1 Windows Forms II Chapter RadioButton / GroupBox Controls Used to solicit a multiple choice input. Radio buttons work as a group. Selecting one.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
 2002 Prentice Hall. All rights reserved. 1 Introduction to the Visual Studio.NET IDE Outline Introduction Visual Studio.NET Integrated Development Environment.
Introduction to Computer CC111
Chapter 2 – Introduction to the Visual Studio .NET IDE
Visual programming Chapter 1: Introduction
Graphics and Multimedia
Using Procedures and Exception Handling
Drawing Mouse and Keyboard Events Part A - Drawing
Chapter 2 – Introduction to the Visual Studio .NET IDE
CASE Tools Graphical User Interface Programming Using C#
Extend Text Editor to Draw shapes
Chapter 12 Graphics in Windows and the Web
Presentation transcript:

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 Paint event to your program. Draw text strings directly on a Windows form at any position. Draw lines, rectangles, circles, and ellipses directly on a Windows form at any position. Display images directly on a Windows form (without using a PictureBox control.) Use a resource from the running assembly. Cause your program to update the form when needed.

3 Windows Graphics The.NET Framework includes a general purpose graphics facility called GDI+ New and improved version of the old Graphical Device Interface (GDI) Not covered in our textbook. Use Visual Studio Help to get documentation. Search on Graphics Rich and complex subject. We will just scratch the surface. Even so, you will be able to do a lot.

4 Windows Graphics This presentation is based on Professional C# 2008 Christian Nagel, et al. Wrox, 2008

5 Windows Graphics You can draw directly on a Windows form. Lines Shapes Text More complex than using labels, textboxes, etc. But gives you more flexibility Think “Office Graphics” Not scientific visualization. Not video games.

6 Windows Graphics From the Graphics Help page: The Graphics object provides methods for drawing a variety of lines and shapes. Simple or complex shapes can be rendered in solid or transparent colors, or using user-defined gradient or image textures. Lines, open curves, and outline shapes are created using a Pen object. To fill in an area, such as a rectangle or a closed curve, a Brush object is required. For more information about how to create and use pens and brushes, see Pens, Brushes, and Colors in the MSDN documentation.Pens, Brushes, and Colors

7 The Point structure Represents a position on a two-dimensional surface Used to define locations on a form A Point has two integer properties: Xhorizontal position Yvertical position Normal constructor Point pt = new Point (10, 20);

8 The Size Structure Used to define size of windows, forms, controls, and other rectangular areas. Like Point, has two integer properties Width Height Can be constructed using a Point, or using separate integer parameters: Size aSize = new Size(pt); Size bSize = new Size (width, height);

9 The Rectangle Structure Defines both the location and the size of a rectangle. Two Constructors Size mySize = new Size (200, 300); Point startPoint = new Point( 10, 10); Rectangle myRect = new Rectangle (startPoint, mySize); int left = 10; int top = 10; int width = 200; int height = 300; Rectangle myRect = new Rectangle (left, top, width, height);

10 The Rectangle Structure Properties -- all integer Left Right Top Bottom Width Height

11 The Graphics Class You have to have a Graphics object in order to draw on a form. It provides the methods and properties that you need in order to draw.

12 The Paint Event A Paint event is broadcast each time a form needs to be redrawn. Controls handle this event internally. You never see it. They draw themselves. To do your own drawing on a form, you provide a handler for this event: A PaintEventHandler

13 How to create a PaintEventHandler In design mode Right click on the form Open the form’s Properties window Click on the “Events” icon lightening bolt Scroll down to Paint Enter name for your event handler Example: Form_Paint or just double click on the box.

14 How to create a PaintEventHandler The Events Icon Name for Paint Event Handler

15 How to create a PaintEventHandler Visual Studio creates a PaintEventHandler stub Opens the code window at that point private void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { } The “e” passed to this function includes (as a property) the Graphics object that you need to use to draw on the form. You will need to use this argument

16 Drawing Text on a Form using System; using System.Drawing; using System.Windows.Forms; … private void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Font myFont = new Font("Arial", 12); Point myPoint = new Point (20, 30); e.Graphics.DrawString ("Hello, World!", myFont, SystemBrushes.WindowText, myPoint); }

17 Here’s the result

18 Drawing Text on a Form This version of DrawString draws the specified string straight to the right, starting from the specified point. No wrap. If it runs off the form, you lose it.

19 Suppose we start close to the edge: private void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Font myFont = new Font("Arial", 12); Point myPoint = new Point (220, 30); e.Graphics.DrawString ("Hello, World!", myFont, SystemBrushes.WindowText, myPoint); }

20 Here is the result.

21 Drawing Text on a Form Another version of DrawString confines the string to a Rectangle specified by the caller. The Point that was the final argument is replaced by the bounding Rectangle.

22 Using a Bounding Rectangle for Text private void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Font myFont = new Font("Arial", 12); Rectangle boundingRect = new Rectangle (220, 20, 70, 200); e.Graphics.DrawString ("The quick brown fox jumped over the lazy dog's back.", myFont, SystemBrushes.WindowText, boundingRect); }

23 Here is the result

24 Let’s draw the Rectangle. private void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Font myFont = new Font("Arial", 12); Rectangle boundingRect = new Rectangle (220, 20, 70, 200); e.Graphics.DrawString ("The quick brown fox jumped over the lazy dog's back.", myFont, SystemBrushes.WindowText, boundingRect); e.Graphics.DrawRectangle (SystemPens.WindowText, boundingRect); }

25 Here is the result

26 Drawing Other Figures private void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Point topLeft = new Point (20, 20); Point bottomRight = new Point (200, 200); e.Graphics.DrawLine (SystemPens.WindowText, topLeft, bottomRight); }

27 Here is the result

28 How about an Ellipse? We specify a Rectangle to bound the ellipse. And this time, let’s create our own pen. private void Form_Paint(object sender System.Windows.Forms.PaintEventArgs e) { Pen redPen = new Pen(Color.Red, 4); // Create rectangle for ellipse. Rectangle rect = new Rectangle( 50, 50, 200, 100); // Draw ellipse to screen. e.Graphics.DrawEllipse(redPen, rect); }

29 Here it is.

30 Displaying an Image We can also display images. Note: No PictureBox control here. We will display the image directly with a Graphics object. Download an image to a convenient directory. Example: Downloads/USF_Bull_small.jpg Downloads/USF_Bull_small.jpg

31 Displaying an Image private void Form1_Paint(object sender, PaintEventArgs e) { Image bull = Point p = new Point(100, 100); e.Graphics.DrawImage(bull, p); }

32 Displaying an Image We can resize the image if necessary by providing a bounding rectangle.

33 Check Image Size

34 Bounding Rectangle for Image private void Form1_Paint(object sender, PaintEventArgs e) { Image bull = //Point p = new Point(100, 100); Point[] bounds = new Point[3]; bounds[0] = new Point(10, 10); // Top left bounds[1] = new Point(10 + bull.Width * 2, 10); // Top right bounds[2] = new Point(10, 10 + bull.Height*2); // Bottom left e.Graphics.DrawImage(bull, bounds); }

35 Bounding Rectangle for Image

36 Bounding Rectangle for Image We can even shear the image if desired. private void Form1_Paint(object sender, PaintEventArgs e) { Image bull = //Point p = new Point(100, 100); Point[] bounds = new Point[3]; bounds[0] = new Point(100, 10); // Top left bounds[1] = new Point(100 + bull.Width * 2, 10); // Top right bounds[2] = new Point(10, 10 + bull.Height*2); // bottom left e.Graphics.DrawImage(bull, bounds); }

37 Sheared Image

38 Using a Resource Displaying an image from a file is not convenient if we want to deploy the application. Have to deploy the image as well as the executable file. Put it in the right directory. We can avoid this problem by creating a resource. The image becomes a part of the assembly that we deploy.

39 Creating a Resource Add the image file to the project. Project > Add Existing Item Right click on USF_Bull_small.jpg and select Properties

40 Build Action Set the Build Action property of the.jpg file to Embedded Resource Compiler will add the image to the assembly as a resource. We can access the resource at run time to create the image. Don't need the file at run time.

41 Using a Resource private void Form1_Paint(object sender, PaintEventArgs e) { String resource_name System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly(); System.IO.Stream s = a.GetManifestResourceStream(resource_name); Image bull = Image.FromStream(s); Point p = new Point(100, 100); e.Graphics.DrawImage(bull, p); }

42 Program Running

43 Form Update What if we need to update the form? Example: Instead of “Hello, world!” let’s show the curent date and time.

44 Current Date and time private void Form_Paint(object sender, PaintEventArgs e) { Font myFont = new Font("Arial", 12); Point myPoint = new Point (20, 30); String timeString = DateTime.Now.ToString(); e.Graphics.DrawString (timeString, myFont, SystemBrushes.WindowText, myPoint); }

45 Program Running What can we do to update the time? Try moving, resizing, minimizing, maximizing.

46 Update Paint event happens when form is miniminzed and then displayed again. If we resize the window so that the time does not show and then enlarge it so that the time does show, we see an undated vesion of the time. Not if size change does not affect the text display.

47 Forcing a Paint Event Add a Timer component to the program. Drag and drop from the toolbox. Components section. Check properties. Be sure it is enabled. Set interval to 1000 (milliseconds). Add a “Tick” event handler. Double click on timer1 in the component tray.

48 Tick Event Handler private void Timer_Tick(object sender, EventArgs e) { Invalidate(); Update(); }

49 Running with Ticks Now the form updates itself every second.

50 What else? There is a lot more to learn about Windows graphics. For more, see Professional C# 2008 Christian Nagel, et al. Wrox, 2008 Chapter 33 Or any of several thick books on C# Or study the.NET documentation