C# UI Programming: Exploring Components, Events, Graphics, and Animation - Prof. Doug A. B, Study notes of Computer Science

An in-depth exploration of user interface programming in c#, focusing on components, events, graphics, manipulation, animation, and the mvc architecture. It covers the basics of c#, .net, windows forms, and gui development goals. It also discusses event-based programming, graphics, direct manipulation, animation, and mvc architectures.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-d9p
koofers-user-d9p šŸ‡ŗšŸ‡ø

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
User Interface Programming in C#:
Basics and Events
Chris North
CS 3724: HCI
GUI Development: Goals
1. General GUI programming concepts
• GUI components, layouts
• Event-based programming
• Graphics
• Direct Manipulation, Animation
• MVC architectures
• Data-driven UIs
2. C#, .NET
• Windows Forms
• Events, delegates
• GDI+
• Threads
• ADO.net
Goal: learn other languages quickly, same concepts
• VB, Xwin, Java 49, …
C# Background
• C# = VB + Java (best of both!)
• Basic statements identical to C++, Java
• Object-oriented only!
• main( ) is inside a class
• No global variables
• ā€œinterfacesā€
• No pointers (object references only), safe
• No delete: automatic garbage collection
• Error Handling, exceptions (try, catch)
• GUI: Windows Forms
• Libraries: Data structs, databases, …
• Component-based: (ā€œassemblyā€) reflection
• No .h files
• Visual Studio
• .NET: CLR, multi-language, web, XML, services, …
C# Materials
• MS Visual Studio .Net (2005)
• MSDN (integrates with VS)
• VS Dynamic Help
• Books
• MS Visual C# .NET, MS Press
– C# language
– Window Forms, GDI+
• MSDN online
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download C# UI Programming: Exploring Components, Events, Graphics, and Animation - Prof. Doug A. B and more Study notes Computer Science in PDF only on Docsity!

User Interface Programming in C#:

Basics and Events

Chris North

CS 3724: HCI

GUI Development: Goals

1. General GUI programming concepts

  • GUI components, layouts
  • Event-based programming
  • Graphics
  • Direct Manipulation, Animation
  • MVC architectures
  • Data-driven UIs

2. C#, .NET

  • Windows Forms
  • Events, delegates
  • GDI+
  • Threads
  • ADO.net

Goal: learn other languages quickly, same concepts

  • VB, Xwin, Java 49, …

C# Background

  • C# = VB + Java (best of both!)
  • Basic statements identical to C++, Java
  • Object-oriented only!
    • main( ) is inside a class
    • No global variables
  • ā€œinterfacesā€
  • No pointers (object references only), safe
  • No delete: automatic garbage collection
  • Error Handling, exceptions (try, catch)
  • GUI: Windows Forms
  • Libraries: Data structs, databases, …
  • Component-based: (ā€œassemblyā€) reflection
    • No .h files
  • Visual Studio
  • .NET: CLR, multi-language, web, XML, services, …

C# Materials

• MS Visual Studio .Net (2005)

• MSDN (integrates with VS)

• VS Dynamic Help

• Books

  • MS Visual C# .NET, MS Press
    • C# language
    • Window Forms, GDI+

• MSDN online

GUI Topics

• Components

• Events

• Graphics

• Manipulation

• Animation

• MVC

Components API

• Properties

  • Like member fields
  • Get, set
  • E.g. Button1.Text = ā€œPress Meā€

• Methods

  • Like member functions
  • Tell component to do something
  • E.g. Button1.Show( )

• Events

  • Like callback functions
  • Receive notifications from component
  • E.g. Button1.Click(e)

GUI Tree Structure

Panel Button Form Label GUI Internal structure containers Panel Button Form Label

Typical command line program

• Non-interactive

• Linear execution

program:

main() { code; code; code; code; code; code; code; code; code; code; code; code; }

Delegates

1. Register with a control to receive events

  • Give Control a function pointer to your callback function
  • this.button1.Click += new EventHandler(this.button1_Click);

2. Receive events from control

  • Control will call the function pointer
  • private void button1_Click(object sender, EventArgs e){ Button1 Button1_click() callback click
  1. button1_Click( )
  2. button1.Click += button1_click( )

Graphics

• Screen is like a painter’s canvas

• App must paint its window contents

  • GUI components paint themselves
  • Anything else: Programmer

1. How to paint?

2. When to paint?

Button

Pixels Coordinate System

• Upside-down Cartesian

• Ywindow = height - Ycartesian

(0,0) (^) (width,0) (0,height) (width, height)

Component Hierarchy

• Each component has its own subwindow

  • Subwindow = rectangular area within parent component
  • Has own coordinate system

• Clipping:

  • Can’t paint outside its subwindow
  • Can’t paint over child components? (0,0) (wp, hp)

(wb, hb) Panel Button Button

Painting Components

• Can paint any component

• Panel is good for custom graphics area

Button Panel

Painting in C#

1. The GDI+ graphics library:

using System.Drawing;

2. Get the ā€œgraphics contextā€ of a component:

Graphics g = myPanel.CreateGraphics( );

3. Paint in it:

g.DrawLine(pen, x1,y1, x2,y2);

Graphics Primitives

Draw Fill

  • Line (pt1,pt2)
  • Lines (pt list)
  • Arc (rect)
  • Curves, Bezier (pt list)
  • Ellipse (rect)
  • Rectangle (rect)
  • Polygon (pt list)
  • Image (img, x,y)
  • String (string, x,y) (^) label

MyApp Open WinExp, Notepad

Close WinExplorer

Repaint event sent to: Desktop, MyApp

Desktop gets repaint event

MyApp gets repaint event

MyApp Form gets repaint event

MyApp gets repaint event

MyApp Form forwards repaint event to Button

Repainting Static Graphics

• Repaint event:

  • Erase (fill with background color) - usually automatically done by the control
  • Draw graphics

In C#

• Receive ā€œpaintā€ event:

(select the event in VisStudio)

this.Paint += new PaintEventHandler(this.Form1_Paint); private void Form1_Paint(object sender, PaintEventArgs e){ Graphics g = e.Graphics; g.DrawLine(new Pen(Color.Red,10), 10,10,300,300); }

• OR: Override the OnPaint method

override void OnPaint(PaintEventArgs e){ base.OnPaint(e); //preserve base class functionality Graphics g = e.Graphics; g.DrawLine(new Pen(Color.Red,10), 10,10,300,300); }

• Can call Refresh( ) to force a repaint

Typical interaction sequence

• select item by point-n-click: Hit testing

• act on item by drag: Dynamic update

• release item

MouseDown MouseMove MouseUp

1. Hit Testing

• Mouse down, mouse over

• Which dot did user click on?

• Using components:

  • Make each dot a simple component, like a Button
  • Hit testing automatic, each component is a subwindow
  • Receive events from components, check event source
  • rectangular items, not scalable, inherit from UserControl

• Using custom graphics:

  • Get click (x,y) from MouseDown event
  • Iterate through data structure, test for hit
    • E.g: if rect.contains(x,y)
  • Data structure for fast lookup?

2. Dynamic Updating

• Dragging, stretching, …

• MouseMove events

• Using components:

  • mouseDown store x,y click in component
  • mouseMove
    • Calculate x,y delta
    • Move component by delta

• Using graphics:

  • (need to erase it, repaint other graphics, repaint new item)
  • Calculate delta, calculate new item location, store
  • Call Refresh( )
  • Draw new graphics in Paint event

Problem

• Dynamic manipulation on top of other graphics

  • Need to preserve (redraw) other graphics
  • Examples: MacDraw, powerpoint

• Simple solution:

  • Call refresh( ) or invalidate( ) while dragging
  • Paint( ) event restores other graphics
  • But: if lots of graphics, too slow & flashing!

Problem: Flashing

• Ugly flashing when repaint:

  • Paint background
  • Redraw shapes

Solution: Double buffering

Solution: Double buffering

• Double buffered repaint:

  • Draw all graphics in temporary off-screen image Ā» Paint background color Ā» Paint shapes
  • Then paint image to Window

• Bonus: C# can do this for you!

  • Form1.DoubleBuffered = true; //VS 2005
  • control maintains off-screen image SetStyle(ControlStyles.DoubleBuffer | //VS 2003 ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);

Rubber Band (XOR painting)

• Want multi-selection by stretching a rubber band

• Draw rubber band by inverting pixel colors

  • drawing with XOR once inverts background colors
  • drawing with XOR twice returns to original look
    • No need to refresh( ), fast! // in mouseMove event: // erase previous rect: (must use screen coords, not window coords) ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Dashed); // <update rect here based on mouse x,y> // draw new rect: ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Dashed);

Model-View-Controller (MVC)

Program State -data structures Paint event -display data Interaction events -modify data

Model

View

Controller

Model-View-Controller (MVC)

Data model Data display User input

Model

View Controller

UI:

Data: refresh manipulate refresh events

Advantages?

• Multiple views for a model

  • Multi-view applications (overview+detail, brushing,…)
  • Different users
  • Different UI platforms (mobile, client-side, server-side,…)
  • Alternate designs

• Multiple models

• Software re-use of parts

• Plug-n-play

• Maintenance

Multiple Views

Model

View

Controller

View

Controller

E.g. C# TreeView Control

TreeView control Nodes collection treeView1.Nodes Java: model listeners

Model

View

Controller

C# DataBase Controls

DataSet class -tables -columns -rows DataGrid control -scroll, sort, edit, …

Model

View

Controller

GUI Topics

Components

Events

Graphics

Manipulation

Animation

MVC