Course 2D_WPF: 2D-Computer Graphics with C# + WPF Chapter C1a: The Intro Project Written in XAML and C#

Similar documents
Course 2DCis: 2D-Computer Graphics with C# Chapter C1: The Intro Project

Course 2D_SL: 2D-Computer Graphics with Silverlight Chapter C5: The Complete Code of PathAnimation. Copyright by V. Miszalok, last update:

Course 2D_SL: 2D-Computer Graphics with Silverlight Chapter C1: The Intro Project

Course 3D_WPF: 3D-Computer Graphics with C# + WPF Chapter C4: The complete code of Sphere

Course 3D_WPF: 3D-Computer Graphics with C# + WPF Chapter C4: Sphere

visual studio vs#d express windows desktop

CPSC Tutorial 6

INFORMATICS LABORATORY WORK #4

Course 3D_WPF: 3D-Computer Graphics with C# + WPF Chapter C3: Dice

Course 2DCis: 2D-Computer Graphics with C# Chapter C1: Comments to the Intro Project

Tutorial 5 Completing the Inventory Application Introducing Programming

CS3240 Human-Computer Interaction

Workspace Desktop Edition Developer's Guide. Customize Views and Regions

WPF Graphics and Multimedia

Learning to use the drawing tools

Note: many examples in this section taken or adapted from Pro WPF 4.5 C#, Matthew MacDonald, apress, 2012, pp

CS3240 Human-Computer Interaction Lab Sheet Lab Session 4 Media, Ink, & Deep Zoom

Interface Builders and Interface Description Languages

In this lecture we will briefly examine a few new controls, introduce the concept of scope, random numbers, and drawing simple graphics.

XAML - BUTTON. The Button class represents the most basic type of button control. The hierarchical inheritance of Button class is as follows

SHAPES & TRANSFORMS. Chapter 12 of Pro WPF : By Matthew MacDonald Assist Lect. Wadhah R. Baiee. College of IT Univ.

We will start our journey into Processing with creating static images using commands available in Processing:


Using Visual Studio. Solutions and Projects

Lab 7: Silverlight API

Class Test 5. Create a simple paint program that conforms to the following requirements.

Windows Presentation Foundation

Developing for Mobile Devices Lab (Part 1 of 2)

DOT.NET MODULE 6: SILVERLIGHT

CSV Roll Documentation

Your First Windows Form

While the press might have you believe that becoming a phoneapp

Weather forecast ( part 2 )

Kinect: getting started. Michela Goffredo University Roma TRE

Chapter 13. Graphics, Animation, Sound and Drag-and-Drop The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

CIS 3260 Intro. to Programming with C#

Visual C# Program: Simple Game 3

Chapter 2. Ans. C (p. 55) 2. Which is not a control you can find in the Toolbox? A. Label B. PictureBox C. Properties Window D.

XAML. Chapter 2 of Pro WPF : By Matthew MacDonald Assist Lect. Wadhah R. Baiee. College of IT Univ. of Babylon Understanding XAML

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Responding to the Mouse

CST242 Windows Forms with C# Page 1

Course 3D_MDX: 3D-Graphics with Managed DirectX 9.0 Chapter 6: Mesh Viewer

Create a memory DC for double buffering

Quick Guide for the ServoWorks.NET API 2010/7/13

WebVisit User course

In the first class, you'll learn how to create a simple single-view app, following a 3-step process:

RadPDFViewer For Silverlight and WPF

Painting your window

Variables One More (but not the last) Time with feeling

sharpcorner.com/uploadfile/37db1d/4958/default.aspx?articleid=cb0b291c-52ae-4b80-a95c- 438d76fa1145

Getting started 7. Setting properties 23

COPYRIGHTED MATERIAL PHOTOSHOP WORKSPACE. Interface Overview 3. Menus 15. The Toolbox 29. Palettes 61. Presets and Preferences 83 WEB TASKS

Developing Desktop Apps for Ultrabook Devices in Windows* 8: Adapting Existing Apps By Paul Ferrill

CSC 355 PROJECT 4 NETWORKED TIC TAC TOE WITH WPF INTERFACE

ComponentOne. HyperPanel for WPF

WPF Viewer for Reporting Services 2008/2012 Getting Started

The Fundamentals. Document Basics

Chapter 9 Getting Started with Impress

THE JAVASCRIPT ARTIST 15/10/2016

CS3240 Human-Computer Interaction Lab Sheet Lab Session 2

Krita Vector Tools

2 Getting Started. Getting Started (v1.8.6) 3/5/2007

Getting started 7. Setting properties 23

Recitation 3 Further Work with Dreamweaver and Photoshop: Refining your Web Site

JASCO CANVAS PROGRAM OPERATION MANUAL

HYPERSTUDIO TOOLS. THE GRAPHIC TOOL Use this tool to select graphics to edit. SPRAY PAINT CAN Scatter lots of tiny dots with this tool.

SciGraphica. Tutorial Manual - Tutorials 1and 2 Version 0.8.0

My First iphone App. 1. Tutorial Overview

Lab - Task Manager in Windows 7 and Vista

Forms/Distribution Acrobat X Professional. Using the Forms Wizard

Course 3D_XNA: 3D-Computer Graphics with XNA Chapter C1: Moving Triangles

Class #1. introduction, functions, variables, conditionals

CST272 Getting Started Page 1

using System.IO; using System.Collections.Generic; using System.Xml.Linq;

Working with Tables in Word 2010

Hands-On Lab. Hello Windows Phone

Lesson 2: First Java Programs

Course 2DCis: 2D-Computer Graphics with C# Chapter C5: Comments to the Controls Project

BASICS OF MOTIONSTUDIO

Intermediate Microsoft Office 2016: Word

Advanced Programming C# Lecture 3. dr inż. Małgorzata Janik

CPSC Tutorial 5

Click on the empty form and apply the following options to the properties Windows.

VMware Horizon Client for Chrome OS User Guide. 04 JAN 2018 VMware Horizon Client for Chrome OS 4.7

There are six main steps in creating web pages in FrontPage98:

MoleMash for App Inventor 2. Getting Started. Introduction. Workshop, S.1

Anima-LP. Version 2.1alpha. User's Manual. August 10, 1992

CPSC 481 Tutorial 10 Expression Blend. Brennan Jones (based on tutorials by Bon Adriel Aseniero and David Ledo)

Getting Started (1.8.7) 9/2/2009

Happy Haunting Halloween

New Insights into Process Deviations Using Multivariate. Control Charts

Charting 1. There are several ways to access the charting function There are three autolayouts which include a chart.

StickFont Editor v1.01 User Manual. Copyright 2012 NCPlot Software LLC

EPIQ & Affiniti Report Template Editor

Default Parameters and Shapes. Lecture 18

P3e REPORT WRITER CREATING A BLANK REPORT

SlickEdit Gadgets. SlickEdit Gadgets

Mobile Programming Lecture 1. Getting Started

Bouml Tutorial. The tutorial must be read in order because I will not repeat each time the general commands to call a menu etc...

Transcription:

1 Course 2D_WPF: 2D-Computer Graphics with C# + WPF Chapter C1a: The Intro Project Written in XAML and C# An Empty Window Copyright by V. Miszalok, last update: 2011-02-08 Guidance for Visual C# 2010 Express, see Introduction into all Courses. 1) Main Menu after start of VC# 2010: Tools Options check lower left checkbox: Show all Settings Projects and Solutions Visual Studio projects location: C:\temp 2) Main Menu after start of VC# 2010: File New Project... Installed templates: WPF Application Name: intro1_xaml_cs OK. 3) Stop the automatic code formatter before it will drive you crazy. Main menu of VC# 2010 Tools Options... An Options-window appears. Double-click the branch Text Editor. Double-click C#. Double-click Formatting. Click General. Uncheck all three check boxes. Click Indentation. Uncheck all four check boxes. Click New Lines. Uncheck all thirteen check boxes. Click Spacing. Uncheck all twenty three check boxes. Click IntelliSense. Uncheck all six check boxes. Quit the Options- window with the OK-button. 4) Click Debug in the main menu of VC# 2010. A submenu opens. Click Start Debugging F5. The default program code automatically compiles, links and starts. Our program starts automatically as stand-alone window containing three parts: - main window = MainWindow with a blue title row - three buttons Minimize, Maximize, Close - a narrow frame with 4 movable borders and 4 movable edges. Enlarge and shrink the window by dragging its borders and edges. - Kill MainWindow by clicking the close button in its right upper edge. Build the project: Debug Build Solution F6. Save the project: File Save All Ctrl+Shift+S Name: intro1_xaml_cs Location: C:\temp uncheck Create directory for solution Save. Minimize VC# 2010. intro1_xaml_cs.exe can now be started as a stand-alone windows program as follows: - Look for C:\temp\intro1_XAML_CS\bin\Release. - Double click intro1_xaml_cs.exe. - You can start an arbitrary number of instances of intro1_xaml_cs.exe. - (Caution: You will have kill all instances before You can write new versions.) Important: Always finish all instances of intro1_xaml_cs before writing new code and starting it! Start the Task Manager with Ctrl+Alt+Del and check if any intro1_xaml_cs.exe-processes are still running and kill them.

Hallo World 2 If there is no Solution Explorer-window, open it via the main menu of VC# 2010 View Solution Explorer. 1. Click the MainWindow.xaml-branch inside the Solution Explorer-window and replace its complete default code by these lines: <Window x:class="intro1_xaml_cs.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="A first WPF-Program written in XAML and C#" Height="300" Width="300" Top="50" Left="50" Background="Cornsilk" Foreground="Red" Content="Hello World "> </Window> 2. Click the MainWindow.xaml.cs-branch below the MainWindow.xaml-branch inside the Solution Explorer-window and replace its complete default code by these lines: using System; using System.Windows; namespace intro1_xaml_cs { public partial class MainWindow:Window { public MainWindow() { InitializeComponent(); Content += DateTime.Now.ToString(); Click the Debug-tab of the main menu obove the main window. A sub-menu opens. Click Start Debugging F5. The program automatically compiles, links and starts again. Exercises: Try out other start positions, other window sizes, other strings, other brushes and other colors. Important Tip: In case of mistype, VC# 2010 presents a Message Box: There were build errors.... You quit with No. An Error List-window with warnings and errors will appear in Visual Studio below Your program. In this error list scroll up to the first error (ignore the warnings!). Double click the line with the first error. The cursor jumps automatically into Your code into the line where the error was detected. Look for mistypes in this line and remove them. (Sometimes You will not find the error in this line but above, where You forgot a comma or a semicolon.) Ignore all errors below the first error (in most cases they are just followers of the first one) and compile. Repeat this procedure until further error message boxes disappear and Your program compiles, links and starts as expected.

Window Size 3 Version 2: Finish all instances of intro1_xaml_cs and replace the complete codes of version 1 by: MainWindow.xaml: <Window x:class="intro1_xaml_cs.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="A first WPF-Program written in XAML and C#" Height="300" Width="300" Top="50" Left="50" SizeChanged="Window_SizeChanged" > <TextBox Name="centerText" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center"/> </Window> MainWindow.xaml.cs: using System; using System.Windows; using System.Windows.Media; using System.Windows.Controls; using System.Windows.Threading; namespace intro1_xaml_cs { public partial class MainWindow:Window { public MainWindow() { InitializeComponent(); private void Window_SizeChanged( object sender, SizeChangedEventArgs e ) { //Compose the text of the "centertext"-textbox: String s1 = "Hello World " + DateTime.Now.ToString() + "\n"; int width = Convert.ToInt32( this.width ); int height = Convert.ToInt32( this.height ); String s2 = "Window Size = " + width.tostring() + " x " + height.tostring(); centertext.text = s1 + s2; Click Debug in the main menu above the main window and Start Debugging F5. Drag the window border and observe the content of the text box. Left, right, top, bottom Version 3: Finish all instances of intro1_xaml_cs and replace the object by this one: MainWindow.xaml: <TextBox Name="left" Text="left" VerticalAlignment ="Center" DockPanel.Dock="Left" /> <TextBox Name="right" Text="right" VerticalAlignment ="Center" DockPanel.Dock="Right" /> <TextBox Name="top" Text="top" HorizontalAlignment="Center" DockPanel.Dock="Top" /> <TextBox Name="bottom" Text="bottom" HorizontalAlignment="Center" DockPanel.Dock="Bottom" /> <TextBox Name="centerText" HorizontalAlignment="Center" VerticalAlignment="Center"/> Drag the window border and observe how the text boxes follow.

Line, Rectangle, Ellipse 4 Version 4a: Finish intro1_xaml_cs and replace the complete -object by a Canvas-object. (It will be needed in the next version 4b to draw 2 lines, a rectangle and an ellipse.) Just for fun let's fill up the background of the canvas with a 3-color-brush. <Canvas Name ="mycanvas"> <Canvas.Background> <LinearGradientBrush> <GradientStop Color="Blue" Offset="0" /> <GradientStop Color="Green" Offset="0.5"/> <GradientStop Color="Red" Offset="1" /> </LinearGradientBrush> </Canvas.Background> <TextBox Name="left" Text="left" VerticalAlignment ="Center" DockPanel.Dock="Left" /> <TextBox Name="right" Text="right" VerticalAlignment ="Center" DockPanel.Dock="Right" /> <TextBox Name="top" Text="top" HorizontalAlignment="Center" DockPanel.Dock="Top" /> <TextBox Name="bottom" Text="bottom" HorizontalAlignment="Center" DockPanel.Dock="Bottom" /> <TextBox Name="centerText" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Canvas> In MainWindow.xaml.cs in private void Window_SizeChanged(... ) add these four lines below centertext.text = s1 + s2;: //mycanvas automatically resizes to the new window size, but its content //mypanel doesn't and must be forced to resize to full mycanvas-size:: mypanel.width = mycanvas.actualwidth; mypanel.height = mycanvas.actualheight; Drag the window border and notice that the new Canvas mycanvas is still invisible and that Version 4a looks and behaves exactly as Version 3 did. ************************************************************************************************************************ Version 4b: Finish intro1_xaml_cs. In MainWindow.xaml add two lines, a rectangle and an ellipse to <Canvas Name ="mycanvas"> until MainWindow.xaml looks like this: <Window x:class="intro1_xaml_cs.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="A first WPF-Program written in XAML and C#" Height="300" Width="300" Top="50" Left="50" SizeChanged="Window_SizeChanged"> <Canvas Name="myCanvas"> <Canvas.Background> <LinearGradientBrush> <GradientStop Color="Blue" Offset="0" /> <GradientStop Color="Green" Offset="0.5"/> <GradientStop Color="Red" Offset="1" /> </LinearGradientBrush> </Canvas.Background> <Line Name="line1" Stroke="Black" StrokeThickness="3"/> <Line Name="line2" Stroke="Black" StrokeThickness="3"/> <Rectangle Name="rect" Stroke="Black" StrokeThickness="3" Fill="White"/> <Ellipse Name="elli" Stroke="Black" StrokeThickness="3"/> <TextBox Name="left" Text="left" VerticalAlignment ="Center" DockPanel.Dock="Left" /> <TextBox Name="right" Text="right" VerticalAlignment ="Center" DockPanel.Dock="Right" /> <TextBox Name="top" Text="top" HorizontalAlignment="Center" DockPanel.Dock="Top" /> <TextBox Name="bottom" Text="bottom" HorizontalAlignment="Center" DockPanel.Dock="Bottom" /> <TextBox Name="centerText" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center"/> </Canvas> </Window>

5 In MainWindow.xaml.cs change private void Window_SizeChanged(... ) until it looks like this: Do not touch the 2 lowermost braces of MainWindow.xaml.cs! private void Window_SizeChanged( object sender, SizeChangedEventArgs e ) { //compose the text of the "centertext"-textbox String s1 = "Hello World " + DateTime.Now.ToString() + "\n"; int width = Convert.ToInt32( this.width ); int height = Convert.ToInt32( this.height ); String s2 = "Window Size = " + width.tostring() + " x " + height.tostring() + "\n"; width = Convert.ToInt32( mycanvas.actualwidth ); height = Convert.ToInt32( mycanvas.actualheight ); String s3 = "Client Size = " + width.tostring() + " x " + height.tostring() + "\n"; String s4 = String.Format( "Font Size = {0,2:F1", centertext.fontsize ); centertext.text = s1 + s2 + s3 + s4; //mycanvas automatically resizes to the new window size, //but its content doesn't and must be forced to resize to full mycanvas-size: line1.x1 = 0; line1.y1 = 0; line1.x2 = mycanvas.actualwidth; line1.y2 = mycanvas.actualheight; line2.x1 = mycanvas.actualwidth; line2.y1 = 0; line2.x2 = 0; line2.y2 = mycanvas.actualheight; Canvas.SetLeft( rect, mycanvas.actualwidth /5 ); //20% empty left space Canvas.SetLeft( elli, mycanvas.actualwidth /5 ); //20% empty left space Canvas.SetTop ( rect, mycanvas.actualheight/5 ); //20% empty top space Canvas.SetTop ( elli, mycanvas.actualheight/5 ); //20% empty top space rect.width = elli.width = 3 * mycanvas.actualwidth / 5; //width = 60% rect.height = elli.height = 3 * mycanvas.actualheight / 5; //height = 60% mypanel.width = mycanvas.actualwidth; mypanel.height = mycanvas.actualheight; Resize the program window. Window Size Animation Version5: Finish intro1_xaml_cs. Declare and initialize a global variable double zoom = 1.1; in the head of public partial class MainWindow : Window, start a DispatcherTimer-object in the constructor and write its event handler "private void TimerOnTick(...)". Replace the complete code of MainWindow.xaml.cs by : using System; using System.Windows; using System.Windows.Media; using System.Windows.Controls; using System.Windows.Threading; namespace intro1_xaml_cs { public partial class MainWindow:Window { double zoom = 1.1; public MainWindow() { InitializeComponent(); //mytimer is a clock intended to animate the window size DispatcherTimer mytimer = new DispatcherTimer(); mytimer.interval = TimeSpan.FromMilliseconds( 1 ); mytimer.tick += TimerOnTick; mytimer.start(); private void TimerOnTick( Object sender, EventArgs args ) { if ( this.actualwidth < 200 ) zoom = 1.1; //fast enlargement if ( this.actualwidth > 800 ) zoom = 0.99; //slow shrinking this.width *= zoom; this.height *= zoom; centertext.fontsize *= zoom;

private void Window_SizeChanged( object sender, SizeChangedEventArgs e ) { //compose the text of the "centertext"-textbox String s1 = "Hello World " + DateTime.Now.ToString() + "\n"; int width = Convert.ToInt32( this.width ); int height = Convert.ToInt32( this.height ); String s2 = "Window Size = " + width.tostring() + " x " + height.tostring() + "\n"; width = Convert.ToInt32( mycanvas.actualwidth ); height = Convert.ToInt32( mycanvas.actualheight ); String s3 = "Client Size = " + width.tostring() + " x " + height.tostring() + "\n"; String s4 = String.Format( "Font Size = {0,2:F1", centertext.fontsize ); centertext.text = s1 + s2 + s3 + s4; //adjust all contents of "MainWindow" to its new window size line1.x1 = 0; line1.y1 = 0; line1.x2 = mycanvas.actualwidth; line1.y2 = mycanvas.actualheight; line2.x1 = mycanvas.actualwidth; line2.y1 = 0; line2.x2 = 0; line2.y2 = mycanvas.actualheight; Canvas.SetLeft( rect, mycanvas.actualwidth /5 ); Canvas.SetLeft( elli, mycanvas.actualwidth /5 ); Canvas.SetTop ( rect, mycanvas.actualheight/5 ); Canvas.SetTop ( elli, mycanvas.actualheight/5 ); rect.width = elli.width = 3 * mycanvas.actualwidth / 5; rect.height = elli.height = 3 * mycanvas.actualheight / 5; mypanel.width = mycanvas.actualwidth; mypanel.height = mycanvas.actualheight; 6 Observe the content of the central text box. Ellipse Animation Version 5: Finish intro1_xaml_cs. In MainWindow.xaml replace the line <Ellipse Name="elli" Stroke="Black" StrokeThickness="3"> by: <Ellipse Name="elli" Stroke="Black" StrokeThickness="3"> <Ellipse.Fill> <RadialGradientBrush x:name="ellibrush" RadiusX="0.1" RadiusY="0.1" SpreadMethod="Repeat"> <GradientStop x:name="stop1" Offset="0" /> <GradientStop x:name="stop2" Offset="0.5"/> <GradientStop x:name="stop3" Offset="1 "/> </RadialGradientBrush> </Ellipse.Fill> </Ellipse> In MainWindow.xaml.cs write four additional declarations in the head of public partial class MainWindow:Window below the line double zoom = 1.1;: double angle = 0; Random r = new Random(); Byte r1, g1, b1, r2, g2, b2, r3, g3, b3; Point radialgradientbrushorigin = new Point( 0.5, 0.5 ); In the constructor public MainWindow() insert two new lines below the line mytimer.start();: //initial random colors of RadialGradientBrush "ellibrush" r1 = (Byte)r.Next(255); g1 = (Byte)r.Next(255); b1 = (Byte)r.Next(255); r2 = (Byte)r.Next(255); g2 = (Byte)r.Next(255); b2 = (Byte)r.Next(255); r3 = (Byte)r.Next(255); g3 = (Byte)r.Next(255); b3 = (Byte)r.Next(255);

7 Insert 10 new lines into the private void Window_SizeChanged(... )-function below the line: mypanel.height = mycanvas.actualheight;: //Increment colors of RadialGradientBrush "ellibrush". //Whenever a color byte exceeds 255, it automatically resets to 0. stop1.color = Color.FromRgb( r1++, g1++, b1++ ); stop2.color = Color.FromRgb( r2++, g2++, b2++ ); stop3.color = Color.FromRgb( r3++, g3++, b3++ ); //Move the center of RadialGradientBrush "ellibrush" slightly around point (0.5, 0.5). radialgradientbrushorigin.x = 0.5 + 0.05 * Math.Cos(angle); radialgradientbrushorigin.y = 0.5 + 0.05 * Math.Sin(angle); ellibrush.gradientorigin = radialgradientbrushorigin; angle += Math.PI / 32; Finish and remove the comment slashes // in front of ellibrush.gradientorigin = new Point( 0.5+0.05*Math.Cos(angle), 0.5+0.05*Math.Sin(angle) ); and start again. Complete Code The complete code of intro1_xaml_cs can be found here: C2D_WPF_Intro_XAML_CS_Code.htm. Exercises Change the initial window size from Width="300" Height="300" to Width="600" Height="600" 2) Change the stroke thickness from StrokeThickness="3" to StrokeThickness="10". 3) Change the timer interval from mytimer.interval = TimeSpan.FromMilliseconds( 1 ); to mytimer.interval = TimeSpan.FromMilliseconds( 100 ); 4) Lower the minimal window size and the zoom speed from if ( mycanvas.actualwidth < 200 ) zoom = 1.1; to if ( mycanvas.actualwidth < 100 ) zoom = 1.01; 5) Increase the maximal window size and the down zoom speed from if ( mycanvas.actualwidth > 800 ) zoom = 0.99; to if ( mycanvas.actualwidth > 1024 ) zoom = 0.98; 6) Reduce the number of rings of ellibrush from RadiusX="0.1" RadiusY="0.1" to RadiusX="0.15" RadiusY="0.15" 7) Speed up the angular velocitiy of the radial center of ellibrush from angle += Math.PI / 32; to angle += Math.PI / 8;