|
GDI+ Programming with C# Authors: Chand M. Published year: 2003 Pages: 41/145 |
4.5 System Pens and System BrushesSystem pens and system brushes are pens and brushes that are used to create system colors. In this section we will discuss how to create and use system pens and brushes. There are two ways to create system pens and brushes. First, you can create pens and brushes using the SystemColors class. SystemColors represents the system colors in GDI+, providing static properties for system colors, such as ActiveBorder and ControlText . The second way to create system pens and brushes uses the SystemPens and SystemBrushes classes. For performance reasons, it is a good idea to use the SystemPens and SystemBrushes classes rather than creating pens and brushes by using the SystemColors class. 4.5.1 System PensThe SystemPens class represents a pen created with the system colors. This class has a static property for each system color that represents the system pen with that particular color . Table 4.16 lists the properties of the SystemPens class. The SystemPens class also provides a method - FromSystemColor -that creates a Pen object from a Color structure. To create a system pen, we pass a SystemColors object. The following code shows how to use the FromSystemColor method: Table 4.16. SystemPens properties
Pen pn = SystemPens.FromSystemColor( SystemColors.HotTrack); 4.5.2 System BrushesThe SystemBrushes class represents a Brush object using the system colors. All properties of SystemBrushes are static read-only properties. Table 4.17 describes these properties. Table 4.17. SystemBrushes properties
Note The MSDN documentation states that the SystemBrushes properties return a SolidBrush object, but that statement is not quite accurate. These properties return a Brush object that must be cast to a SolidBrush object. If you run the code without casting them, the compiler throws an error. The SystemBrushes class also provides a FromSystemColor method, which creates a Brush object from a specified system color. The following code shows how to use the FromSystemColor method: SolidBrush brush = (SolidBrush)SystemBrushes.FromSystemColor (SystemColors.ActiveCaption);
Listing 4.26 uses SystemBrushes and SystemPens objects to draw two lines and a rectangle. Listing 4.26 Using the SystemBrushes and SystemPens classes
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; // Create a pen using SystemPens Pen pn = SystemPens.FromSystemColor( SystemColors.HotTrack); // Create a brush using SystemBrushes SolidBrush brush = (SolidBrush)SystemBrushes.FromSystemColor (SystemColors.ActiveCaption); // Draw lines and rectangles g.DrawLine(pn, 20, 20, 20, 100); g.DrawLine(pn, 20, 20, 100, 20); g.FillRectangle(brush, 30, 30, 50, 50); // YOU CAN'T DISPOSE OF SYSTEM PENS AND // BRUSHES. IF YOU TRY, GDI+ WILL GENERATE // AN ERROR. //pn.Dispose(); //brush.Dispose(); }
Figure 4.31 shows the output from Listing 4.26. Figure 4.31. Using system pens and system brushes
|
|
GDI+ Programming with C# Authors: Chand M. Published year: 2003 Pages: 41/145 |