Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Handling Events in a Decorator

When we construct an actual Decorator containing the mouse and paint methods we just showed, we have to connect the event handling system to these methods. We do this in the constructor for the Decorator by creating an EventHandler class for the mouse enter and hover events and a MouseEventHandler for the move and leave events. It is important to note that the events we are catching are events on the contained button, rather than on the surrounding Panel. So the control to which we add the handlers is the button itself.

public CoolDecorator(Control c) { 
contl = c;          //copy in control 
//mouse over, enter handler 

EventHandler evh = new EventHandler(mouseEnter); 
      c.MouseHover += evh; 
      c.MouseEnter+= evh; 
//mouse move handler 
c.MouseMove += new MouseEventHandler(mouseMove); 
c.MouseLeave += new EventHandler(mouseLeave); 

Similarly, we create a PaintEventHandler for the paint event.

//paint handler catches button's paint 
c.Paint += new PaintEventHandler( paint); 

Layout Considerations

If you create a Windows form containing buttons, the GUI designer automatically generates code to add that Control to the Controls array for that Window. We want to change this by adding the button to the Controls array for the new panel, adding the panel to the Controls array for the Window, and removing the button from that array. Here is the code to add the panel and remove the button in the Form initialization method.

//add outside decorator to the layout 
//and remove the button from the layout 
this.Controls.AddRange(new System.Windows.Forms.Control[] {cdec} ); 
this.Controls.Remove (btButtonA); 

This is the code to add the button to the Decorator panel.

public CoolDecorator(Control c) { 
      contl = c;           //copy in control 
      //add button to controls contained in panel 
      this.Controls.AddRange(new Control[] {contl} ); 

Control Size and Position

When we decorate the button by putting it in a Panel, we need to change the coordinates and sizes so that the Panel has the size and coordinates of the button and the button has a location of (0, 0) within the panel. This also happens in the CoolDecorator constructor.

this.Location = p; 
contl.Location =new Point(0,0); 

this.Name = "deco"+contl.Name ; 
this.Size = contl.Size; 
x1 = c.Location.X - 1; 
y1 = c.Location.Y - 1; 
x2 = c.Size.Width; 
y2 = c.Size.Height; 

We also create instances of the Pens we will use in the Paint method in this constructor.

//create the overwrite pens 
gPen = new Pen(c.BackColor, 2); //gray pen overwrites borders 
bPen = new Pen(Color.Black , 1); 
wPen = new Pen(Color.White, 1); 

This program is shown in Figure 17-1, with the mouse hovering over one of the buttons.

Figure 17-1Figure 17-1. The A button and B button are CoolButtons, which are outlined when a mouse hovers over them. Here the B button is outlined.


  • + Share This
  • 🔖 Save To Your Account