GUI Programming in C#
GUI Programming in C#
C# Background C# Materials
• C# = VB + Java (best of both!)
• Basic statements identical to C++, Java
• MS Visual Studio .Net (2005)
• Object-oriented only!
• main( ) is inside a class • MSDN (integrates with VS)
• No global variables
• “interfaces” • VS Dynamic Help
• No pointers (object references only), safe • Books
• No delete: automatic garbage collection
• MS Visual C# .NET, MS Press
• Error Handling, exceptions (try, catch)
– C# language
• GUI: Windows Forms – Window Forms, GDI+
• Libraries: Data structs, databases, …
• Component-based: (“assembly”) reflection • MSDN online
• No .h files
• Visual Studio
• .NET: CLR, multi-language, web, XML, services, …
1
GUI Topics Components API
• Components • Properties
• Like member fields
• Events
• Get, set
• Graphics • E.g. Button1.Text = “Press Me”
• Manipulation • Methods
• Like member functions
• Animation • Tell component to do something
• MVC • E.g. Button1.Show( )
• Events
• Like callback functions
• Receive notifications from component
• E.g. Button1.Click(e)
2
Interactive command line program Typical GUI program
program: GUI program:
• User input commands • User input commands
main() main()
{ {
decl data storage; decl data storage;
• Non-linear execution initialization code; • Non-linear execution initialization code;
3
Delegates Graphics
1. Register with a control to receive events • Screen is like a painter’s canvas
• Give Control a function pointer to your callback function
• this.button1.Click += new EventHandler(this.button1_Click); • App must paint its window contents
2. Receive events from control • GUI components paint themselves
• Control will call the function pointer • Anything else: Programmer
• private void button1_Click(object sender, EventArgs e){
1. button1.Click += button1_click( )
1. How to paint?
click 2. When to paint?
Button1 Button1_click()
Button
2. button1_Click( ) callback
4
Component Hierarchy Painting Components
• Each component has its own subwindow
• Subwindow = rectangular area within parent component
• Can paint any component
• Has own coordinate system • Panel is good for custom graphics area
• Clipping:
• Can’t paint outside its subwindow
• Can’t paint over child components?
(0,0) Panel Button
Panel
(0,0) Button
Button
(wb, hb)
(wp, hp)
5
Graphics Attributes Color
• Pen (for lines) • Combinations of Red, Green, Blue
• Color, width, dash, end caps, joins, • Alpha value = opacity
• Brush (for filling) • Each in [0, 255]
• Color, Solid, texture, pattern, gradient
• Font, String Format (for strings)
• C#: Color.FromArgb(255, 150, 0)
• Bitmap/Metafile (for images)
• Bmp, gif, jpeg, png, tiff, wmf, …
Color Re-Paint
• Combinations of Red, Green, Blue • Screen is like a painter’s canvas
• Alpha value = opacity • All windows paint on the same surface!
• Windows don’t “remember” whats under them
• Each in [0, 255]
• Need to re-paint when portions are newly
exposed
• C#: Color.FromArgb(255, 150, 0)
• Receive Repaint events
Hokie Orange • Open, resize, bring to front
• When other windows in front move, resize, close
6
MyApp Open WinExp, Notepad
7
MyApp gets repaint event MyApp gets repaint event
MyApp Form gets repaint event MyApp Form forwards repaint event to Button
• Erase (fill with background color) - usually automatically private void Form1_Paint(object sender, PaintEventArgs e){
done by the control Graphics g = e.Graphics;
g.DrawLine(new Pen(Color.Red,10), 10,10,300,300);
• Draw graphics }
8
Typical program structure for
Program Structure
Dynamic Graphics
C# WinApp:
• Store data structure of graphics items
Program State Class{
• E.g. user drawn picture in a paint program declare data storage;
-data structures
constructor(){
initialize data storage;
• Paint event: }
Paint event
• Erase window (draw background color) -display data cntrl1_paintevent(e){
• Draw graphics for items in data structure draw graphics from data;
}
9
Typical interaction sequence 1. Hit Testing
• Mouse down, mouse over
• select item by point-n-click: Hit testing
• Which dot did user click on?
MouseDown • Using components:
• Make each dot a simple component, like a Button
• act on item by drag: Dynamic update • Hit testing automatic, each component is a subwindow
• Receive events from components, check event source
MouseMove • rectangular items, not scalable, inherit from UserControl
• Using custom graphics:
• release item • Get click (x,y) from MouseDown event
• Iterate through data structure, test for hit
MouseUp
– E.g: if rect.contains(x,y)
• Data structure for fast lookup?
10
Problem: Flashing Solution: Double buffering
// in mouseMove event:
// erase previous rect: (must use screen coords, not window coords)
• Bonus: C# can do this for you! ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Dashed);
• Form1.DoubleBuffered = true; //VS 2005 // <update rect here based on mouse x,y>
• control maintains off-screen image // draw new rect:
ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Dashed);
SetStyle(ControlStyles.DoubleBuffer | //VS 2003
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
11
Drag-n-Drop Animation
• Drag and Drop API for GUI controls
• Supports data transfer
• Update components/graphics in a loop:
for(int i =0; i<100; i++)
DestControl.AllowDrop = True; button2.Left += 10;
12
Model-View-Controller (MVC) Model-View-Controller (MVC)
Model
Program State
-data structures
refresh
View Controller
UI:
View Data display events
User input
Paint event
-display data
refresh manipulate
Controller
Interaction events
-modify data
Model
Data: Data model
13
E.g. C# TreeView Control C# DataBase Controls
TreeView control
DataGrid control
View -scroll, sort, edit, … View
Controller Controller
treeView1.Nodes
Model
-columns Model
-rows
GUI Topics
Components
Events
Graphics
Manipulation
Animation
MVC
14