Pens

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); }

Pens are used for all drawing methods of the Graphics object with the exception of the DrawString method. A pen can be used to draw solid lines, dash patterns, and even brush-style lines such as gradient or hatch. To create brush-style pens, use the property Pen constructor for a new pen, or assign the existing brush to the pen's Brush property.

Figure 4.5 shows the interface for the Pen Demo. The property grid allows for changing the properties of the current pen to see the result.

Figure 4.5. The Pen Demo.

figure 4.5. the pen demo.

Unfortunately, pens are not very exciting and a lengthy explanation of their use is not necessary. However, the Pen Demo is interesting with regard to the use of the property grid. To attach the property grid to the current pen, the Pen Demo application creates a small class named PenProperties. The PenProperties class provides some of the properties of a pen and is used to hook up the property grid to the underlying pen. The best part of this is the minimal amount of effort needed to use the property grid. Listing 4.6 shows the source for the Pen Demo application.

Listing 4.6 Pen Demo Source
   1: using System;   2: using System.Drawing;   3: using System.Collections;   4: using System.ComponentModel;   5: using System.Windows.Forms;   6: using System.Data;   7:   8: namespace Pen_Demo   9: {  10:  11:     //Used by the property grid  12:     public class PenProperties {  13:  14:         //Implementation members  15:         private System.Drawing.Pen      thePen;  16:  17:         //Events  18:         public event EventHandler PropertyChanged;  19:  20:         //Properties  21:         public System.Drawing.Color Color {  22:             get {  return thePen.Color; }  23:             set {  24:                 thePen.Color = value;  25:                 OnPropertyChanged( new EventArgs( ) );  26:             }  27:         }  28:  29:         public System.Drawing.Drawing2D.DashCap DashCap {  30:             get {  return thePen.DashCap; }  31:             set {  32:                 thePen.DashCap = value;  33:                 OnPropertyChanged( new EventArgs( ) );  34:             }  35:         }  36:  37:         public System.Drawing.Drawing2D.DashStyle DashStyle {  38:             get {  return thePen.DashStyle; }  39:             set {  40:            thePen.DashStyle = value;  41:                 OnPropertyChanged( new EventArgs( ) );  42:             }  43:         }  44:  45:         public float Width {  46:             get {  return thePen.Width; }  47:             set {  48:                 thePen.Width = value;  49:                 OnPropertyChanged( new EventArgs( ) );  50:             }  51:         }  52:  53:         public float DashOffset {  54:             get {  return thePen.DashOffset; }  55:             set {  56:                 thePen.DashOffset = value;  57:                 OnPropertyChanged( new EventArgs( ) );  58:             }  59:         }  60: 61:         public System.Drawing.Drawing2D.LineCap StartCap {  62:             get {  return thePen.StartCap; }  63:             set {  64:                 thePen.StartCap = value;  65:                 OnPropertyChanged( new EventArgs( ) );  66:             }  67:         }  68:  69:         public System.Drawing.Drawing2D.LineCap EndCap {  70:             get {  return thePen.EndCap; }  71:             set {  72:                 thePen.EndCap = value;  73:                 OnPropertyChanged( new EventArgs( ) );  74:             }  75:         }  76:  77:         public PenProperties( Pen p ) {  78:             thePen = p;  79:    }  80:  81:         protected void OnPropertyChanged( EventArgs e ) {  82:             if( PropertyChanged != null )  83:                 PropertyChanged( this, e );  84:         }  85:     }  86:  87:     /// <summary>  88:     /// Summary description for Form1.  89:     /// </summary>  90:     public class Form1 : System.Windows.Forms.Form  91:     {  92:         /// <summary>  93:         /// Required designer variable.  94:         /// </summary>  95:         private System.ComponentModel.Container     components = null;  96:         private System.Windows.Forms.PropertyGrid   propertyGrid1;  97:         private System.Drawing.Pen                  thePen = null;  98:         private PenProperties                       penProps = null;  99: 100:         public Form1() 101:         { 102:             // 103:             // Required for Windows Form Designer support 104:             // 105:             InitializeComponent(); 106: 107:             thePen = new Pen( System.Drawing.Color.Black ); 108:             penProps = new PenProperties( thePen ); 109:             penProps.PropertyChanged += new EventHandler(  graphics/ccc.gifthis.OnPenPropertyChangedEventHandler ); 110:             propertyGrid1.SelectedObject = penProps; 111:             propertyGrid1.Refresh( ); 112: 113: 114: 115:         } 116: 117:         protected  override void OnSizeChanged( EventArgs e ) { 118:             base.OnSizeChanged( e ); 119:             this.Invalidate( ); 120:         } 121: 122: 123:         protected override void OnPaint( PaintEventArgs e ) { 124:             base.OnPaint( e ); 125:             Rectangle drawZone = new Rectangle( ClientRectangle.Left, 126:                                                 ClientRectangle.Top, 127:                                                 ClientRectangle.Width, 128:                                                 ClientRectangle.Height ); 129: 130:             //Offset for Property Grid 131:             drawZone.X += this.propertyGrid1.Width; 132: 133: 134:        drawZone.Inflate(-10,-10); 135: 136:             //Draw horizontal line 137:             e.Graphics.DrawLine( thePen, drawZone.Left, drawZone.Top, drawZone.Width, graphics/ccc.gif drawZone.Top ); 138: 139:             //Draw vertical line 140:             e.Graphics.DrawLine( thePen, drawZone.Left + (thePen.Width/2),   graphics/ccc.gifdrawZone.Top + thePen.Width + 5, drawZone.Left + (thePen.Width/2), drawZone.Bottom  ); 141: 142:             //Draw diag line 143:             e.Graphics.DrawLine( thePen, drawZone.Left + (2*thePen.Width) , graphics/ccc.gifdrawZone.Top + thePen.Width + 5, drawZone.Width , drawZone.Bottom  ); 144:         } 145: 146:         protected void OnPenPropertyChangedEventHandler( object sender, EventArgs e  graphics/ccc.gif) { 147:             this.Invalidate( ); 148:         } 149: 150: 151: 152:         /// <summary> 153:         /// Clean up any resources being used. 154:         /// </summary> 155:         protected override void Dispose( bool disposing ) 156:         { 157:             if( disposing ) 158:             { 159:                 if (components != null) 160:                 { 161:                     components.Dispose(); 162:                 } 163:             } 164:             base.Dispose( disposing ); 165:         } 166: 167:         #region Windows Form Designer generated code 168:         /// <summary> 169:         /// Required method for Designer support - do not modify 170:         /// the contents of this method with the code editor. 171:         /// </summary> 172:         private void InitializeComponent() 173:         { 174:             this.propertyGrid1 = new System.Windows.Forms.PropertyGrid(); 175:        this.SuspendLayout(); 176:             // 177:             // propertyGrid1 178:             // 179:             this.propertyGrid1.CommandsBackColor =  graphics/ccc.gifSystem.Drawing.SystemColors.Window; 180:             this.propertyGrid1.CommandsVisibleIfAvailable = true; 181:             this.propertyGrid1.Dock = System.Windows.Forms.DockStyle.Left; 182:             this.propertyGrid1.LargeButtons = false; 183:             this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar; 184:             this.propertyGrid1.Name = "propertyGrid1"; 185:             this.propertyGrid1.Size = new System.Drawing.Size(160, 357); 186:             this.propertyGrid1.TabIndex = 0; 187:             this.propertyGrid1.Text = "propertyGrid1"; 188:             this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window; 189:             this.propertyGrid1.ViewForeColor =  graphics/ccc.gifSystem.Drawing.SystemColors.WindowText; 190:             // 191:             // Form1 192:             // 193:             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 194:             this.BackColor = System.Drawing.SystemColors.Window; 195:             this.ClientSize = new System.Drawing.Size(520, 357); 196:             this.Controls.AddRange(new System.Windows.Forms.Control[] { 197:                                                  this.propertyGrid1 }  ); 198:             this.Name = "Form1"; 199:             this.Text = "Pen Demo"; 200:             this.ResumeLayout(false); 201: 202:         } 203:         #endregion 204: 205:         /// <summary> 206:         /// The main entry point for the application. 207:         /// </summary> 208:         [STAThread] 209:         static void Main() 210:         { 211:             Application.Run(new Form1()); 212:         } 213:  } 214: } 

As stated earlier, no work is necessary to make use of the property grid; just assign the SelectedObject property to the object you want the property grid to work with. The demo allows you to see the result of changing the properties of the underlying Pen object and how those changes affect the output generated by using the pen.

One item that requires explanation is the PropertyChanged event provided by the PenProperties class. This event is used to notify the parent form that the properties of the pen have been changed and the screen needs to be updated to reflect these changes. Events are an intrinsic part of .NET, C#, and Windows/Web Forms development. When a property of the PenProperties class has been modified, the protected method OnPropertyChanged is invoked. Having a protected virtual method for events is recommended by the current .NET coding standards available on the MSDN Web site. This allows for derived classes to have the first crack at the event and determine whether the event needs to be propagated to all event listeners.

The overall intent of the demo is to allow for a visual reference of the various effects the pen properties have when the pen is used to draw to the screen or printer. In addition, the PropertyGrid is a nice addition to the standard set of controls provided with Visual Studio; understanding how it works will soon become necessary as you begin developing custom controls. This is the same PropertyGrid used by the IDE during design-time, and custom controls are expected to work properly with the grid to provide manipulation of various design-time properties. Interaction with the property grid is covered from time to time as it relates to the current project.



    .NET Windows Forms Custom Controls
    User Interfaces in VB .NET: Windows Forms and Custom Controls
    ISBN: 1590590449
    EAN: 2147483647
    Year: 2002
    Pages: 74

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