Is it possible to construct an event handler which has both mouse and paint
event arguments. Are there examples out there which utilize this type of event
handler ? If so can someone please direct me to it. Thank you. Something like this ?! Is this legal ?
private void Painter_MouseDown(object sender, MouseEventArgs e, PaintEventArgs p)
Henry LagosPosted Jul 28, 2008, 10:15 AM
The problem is the following: You have two objects a photo and a drawing both of which implement the interface IPhotoDraw which "decorates" the objects with a string tag and/or a colored border when an event is fired. I decided that the event would be a mouse click which would place the tag or the border on each of the objects. The problem was that I couldn't figure out a way in which to fire a paint event upon a mouse click. Refreshing the screen after implementing the decorators is an option. I tried creating an EventHandler Method + EventArgs Class for this problem but was sort of confused on where to place it and where to fire it from. If you have any suggestions they are welcome . Thank You.
Henry LagosPosted Jul 28, 2008, 9:59 AM
Firstly, thank you for your replies. I fooled around with both suggestions and decided to apply the Invalidate( ) method since it requires less typing and is more direct. The program below utilizes the Invalidate( ) method and runs. However, I like the idea of creating the custom-generated event handler and would like to be able to execute programs in that manner as well. How would you apply the an EventHandler + EventArgs class for this problem ? Perhaps ...
public class MousePaintEventArgs : EventArgs {
public MousePaintEventArgs ( Object source,MouseEventArgs e, PaintEventArgs p ) {
public MouseEventArgs me;
public PaintEventArgs pe;
this.me = e;
this.pe = p;
} ???????????????????????????? suggestions are welcome ????????????
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using Given;
// Decorator Pattern Example Judith Bishop Aug 2007
// Draws a photograph and a user-generated drawing in a window
// of fixed size . Has decorators that are BorderedPhotos and
// TaggedPhotos that can be composed and added in different
// combinations
namespace Given {
interface IPhotoDraw {
void Drawer( Object source, PaintEventArgs e );
void Drawer2( Object source, PaintEventArgs e );
}
// The original Photo class
public class Photo : Form, IPhotoDraw {
Image image;
public Photo ( ) {
image = new Bitmap( "jug.jpg" );
this.Text = "Lemonade";
this.Paint += new PaintEventHandler( Drawer );
}
public void Drawer( Object source, PaintEventArgs e ) {
e.Graphics.DrawImage( image, 30, 20 );
}
public void Drawer2( Object source, PaintEventArgs e ) {
}
}
// Hominid class in which a user-drawing can be decorated
// with tags and borders
public class Hominid : Form, IPhotoDraw {
static Image image;
private Pen p;
public string pen_color = "red";
private bool mouse_down = false;
private Point last_point = Point.Empty;
private Graphics graphics, gfx_seen;
public Hominid ( ) {
image = new Bitmap( "human.jpg" );
graphics = Graphics.FromImage(image);
p = new Pen(Color.FromName(pen_color));
this.BackColor = Color.Blue;
this.Text = "Hominid";
this.Paint += new PaintEventHandler( Drawer );
this.MouseDown += new MouseEventHandler(this.User_Drawing_MouseDown );
this.MouseUp += new MouseEventHandler(this.User_Drawing_MouseUp );
this.MouseMove += new MouseEventHandler(this.User_Drawing_MouseMove );
}
private void User_Drawing_MouseDown(object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Right ) {
image.Save("human.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
} else {
mouse_down = true;
}
}
private void User_Drawing_MouseUp(object sender, MouseEventArgs e ) {
last_point = new Point(e.X, e.Y);
mouse_down = false;
}
protected void User_Drawing_MouseMove(object sender, MouseEventArgs e ) {
if ( mouse_down ) {
Point pMousePos = new Point(e.X, e.Y);
gfx_seen = CreateGraphics();
gfx_seen.DrawLine(new Pen(Color.Black),pMousePos, last_point);
graphics.DrawLine(new Pen(Color.Black),pMousePos, last_point);
}
last_point = new Point(e.X, e.Y);
}
public void Drawer( Object source, PaintEventArgs e ) {
e.Graphics.DrawImage( image, 30, 20 );
}
public void Drawer2( Object source, PaintEventArgs e ) {
}
}
}
class DecoratorPatternExample {
// This simple BorderedPhoto decorator adds a colored border of fixed size
class BorderedPhoto : Form, IPhotoDraw {
IPhotoDraw photo;
Color color;
public BorderedPhoto( IPhotoDraw p, Color c ) {
photo = p;
color = c;
this.Paint += new PaintEventHandler( Drawer );
this.MouseDown += new MouseEventHandler( Border_Check );
}
public void Drawer( Object source, PaintEventArgs e ) {
photo.Drawer( source, e );
}
public void Drawer2( Object source, PaintEventArgs e ) {
photo.Drawer2( source, e );
e.Graphics.DrawRectangle( new Pen( color, 10 ), 25, 15, 215, 225 );
}
public void Border_Check( Object source, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Left ) {
this.Paint += new PaintEventHandler( Drawer2 );
Invalidate( );
} else {
this.Paint -= new PaintEventHandler( Drawer2 );
Invalidate( );
}
}
}
// The TaggedPhoto decorator keeps track of the tag number which it
// a specific place to be written
class TaggedPhoto : Form, IPhotoDraw { tags = new List ( );
IPhotoDraw photo;
string tag;
int number;
static int count;
static List
public TaggedPhoto( IPhotoDraw p, string t ) {
photo = p;
tag = t;
tags.Add(tag);
number = ++count;
this.Paint += new PaintEventHandler( Drawer );
this.MouseDown += new MouseEventHandler( Tag_Check );
}
public void Drawer( Object source, PaintEventArgs e ) {
photo.Drawer( source, e );
}
public void Drawer2( Object source, PaintEventArgs e ) {
photo.Drawer2( source, e );
e.Graphics.DrawString( tag, new Font( "Arial", 16 ), new SolidBrush( Color.Black ), new PointF( 80, 80+number*20 ) );
}
public string ListTaggedPhotos( ) {
string s = "Tagged Photos are: ";
foreach( string tag in tags ) s+= tag+" ";
return s;
}
public void Tag_Check( Object source, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Right ) {
this.Paint += new PaintEventHandler( Drawer2 );
Invalidate( );
} else {
this.Paint -= new PaintEventHandler( Drawer2 );
Invalidate( );
}
}
}
// Application.Run acts as a simple client
static void Main( ) {
IPhotoDraw photo, drawing;
TaggedPhoto colorTaggedPhoto, foodTaggedPhoto, composition2;
BorderedPhoto composition1;
// Compose a photo with two TaggedPhotos and a Green BorderedPhoto
photo = new Photo( );
Application.Run( (Photo)photo );
foodTaggedPhoto = new TaggedPhoto( photo, "Lemonade" );
//Application.Run( foodTaggedPhoto );
colorTaggedPhoto = new TaggedPhoto( foodTaggedPhoto, "Green" );
Application.Run( colorTaggedPhoto );
composition1 = new BorderedPhoto( colorTaggedPhoto, Color.Green );
Application.Run( composition1 );
Console.WriteLine( colorTaggedPhoto.ListTaggedPhotos( ) );
// Compose a drawing with two TaggedPhotos and a Green Bordered Drawing
drawing = new Hominid( );
Application.Run( (Hominid)drawing );
foodTaggedPhoto = new TaggedPhoto( drawing, "Hominid" );
Application.Run( foodTaggedPhoto );
colorTaggedPhoto = new TaggedPhoto( foodTaggedPhoto, "Orange" );
Application.Run( colorTaggedPhoto );
composition1 = new BorderedPhoto( colorTaggedPhoto, Color.Orange );
Application.Run( composition1 );
Console.WriteLine( colorTaggedPhoto.ListTaggedPhotos( ) );
// Compose a drawing with one TaggedPhoto and a Red Bordered Drawing
drawing = new Hominid( );
Application.Run( (Hominid)drawing );
composition1 = new BorderedPhoto( drawing, Color.Red );
Application.Run( composition1 );
composition2 = new TaggedPhoto( composition1, "Stick Figure" );
Application.Run( composition2 );
Console.WriteLine( composition2.ListTaggedPhotos( ) );
}
}
Ryan AlfordPosted Jul 27, 2008, 11:05 AM
Bechir BejaouiPosted Jul 26, 2008, 7:20 PM