9.9 Utilizing Custom Designers

 <  Day Day Up  >  

You want to change the design-time behavior of a control without affecting the runtime functionality.


Technique

To create a custom designer for a control, define a new class derived from System.Windows.Forms.Design.ControlDesigner . To change the behavior of a form while it is being moved or sized within the forms designer, override the SelectionRules property. This property returns a SelectionRules value, which is a bit array of SelectionRules values defined in the System.Windows.Forms.Design.SelectionRules enumerated data type. In other words, perform a logic OR ( ) for each selection rule. To turn off horizontal sizing while still allowing vertical sizing, moving, and visibility, use the following selection rules:

 
 public override SelectionRules SelectionRules {     get     {         return System.Windows.Forms.Design.SelectionRules.TopSizeable              System.Windows.Forms.Design.SelectionRules.BottomSizeable              System.Windows.Forms.Design.SelectionRules.Moveable              System.Windows.Forms.Design.SelectionRules.Visible;     } } 

If you want to add visual cues to a control during design time, override the OnPaintAdornments method. This method is called after the Paint handler for the control and allows you to use the Graphics object within the PaintEventArgs parameter to add any additional visual adornments to the currently selected control:

 
 protected override void OnPaintAdornments(PaintEventArgs pe) {     pe.Graphics.DrawRectangle(new Pen( Color.Black ), 0, 0,         this.Control.Width-1, this.Control.Height-1); } 

The ControlDesigner contains several methods that allow you to hook into the design process based on a variety of different events. For instance, you can override the event handler for various mouse events such as MouseEnter to perform a custom action.

To associate a control designer with a control, apply the Designer attribute to the class declaration of the control. This attribute takes a type reference to the control designer you want to use, which you can find by using the typeof keyword:

 
 [Designer(typeof(RandomShapeDesigner))] public class RandomShape : System.Windows.Forms.Control { ... } 

Comments

A control designer is a useful construct that allows you to alter the design-time behavior of a control to aid the developer who is designing a Windows Form that uses the control. A control designer can either alter the default behavior of the forms designer or enhance its functionality. One way of altering the way the forms designer manipulates a control is to change the selection rules of a control. Selection rules determine whether a control can be resized or moved in addition to locking or changing the visibility attribute of the control.

The ControlDesigner class contains several virtual methods representing event delegates that you can override to alter how the forms designer responds to events fired as a result of user interaction. For instance, when the user moves a control on the Windows Forms, the default control designer captures this event and subsequently updates the Location property of the control. Similarly, if you override the OnMouseDragBegin method , your control designer can perform a custom action whenever this event occurs. Other event handlers also allow you to play an active role in the design of the control such as the OnPaintAdornments method, which is called when a control finishes painting, thereby allowing you to draw additional objects at design time.

It should be noted that a custom designer is only used during the design-time phase of a control. During runtime, the custom designer is out of the picture, so to speak. In other words, if you perform any custom painting through the OnPaintAdornments method, that code is not called when the application housing the Windows Form runs. The remaining two recipes of this chapter finish the discussion of control designers by looking at how to extend the design-time context menu of a control and how to add and remove additional properties of a control.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net