This means that when you use a hatch brush, you must specify the type of pattern you want to use, through one of the available hatch styles. To make the filled area more interesting, you also specify the color to use when drawing the pattern. To get a hatch brush, you use the HatchBrush class. One of its constructors has the following syntaxes: public HatchBrush(HatchStyle style, Color foreColor);
The foreColor argument is the color that will be used to draw the pattern. The style argument is the hatch style you want to apply. Some of the available styles are: using System; using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; public class Exercise : Form { public Exercise() { InitializeComponent(); } void InitializeComponent() { Paint += new PaintEventHandler(Exercise_Paint); } private void Exercise_Paint(object sender, PaintEventArgs e) { HatchBrush brushBackDiag = new HatchBrush(HatchStyle.BackwardDiagonal, Color.FromArgb(0, 0, 255)); HatchBrush brushCross = new HatchBrush(HatchStyle.Cross, Color.FromArgb(200, 0, 0)); HatchBrush brushDarkDown = new HatchBrush(HatchStyle.DarkDownwardDiagonal, Color.Salmon); HatchBrush brushDarkHorz = new HatchBrush(HatchStyle.DarkHorizontal, Color.Navy); HatchBrush brushDarkUpDiag = new HatchBrush(HatchStyle.DarkUpwardDiagonal, Color.Pink); HatchBrush brushVertical = new HatchBrush(HatchStyle.DarkVertical, Color.FromArgb(255, 0, 255)); HatchBrush brushDashDnDiag = new HatchBrush(HatchStyle.DashedDownwardDiagonal, Color.FromArgb(255, 128, 0)); HatchBrush brushDashHorz = new HatchBrush(HatchStyle.DashedHorizontal, Color.FromArgb(0, 128, 192)); HatchBrush brushDashUpDiag = new HatchBrush(HatchStyle.DashedUpwardDiagonal, Color.Green); HatchBrush brushDashVert = new HatchBrush(HatchStyle.DashedVertical, Color.Firebrick); HatchBrush brushDiagBrisk = new HatchBrush(HatchStyle.DiagonalBrick, Color.Fuchsia); HatchBrush brushDiagCross = new HatchBrush(HatchStyle.DiagonalCross, Color.Moccasin); HatchBrush brushDivot = new HatchBrush(HatchStyle.Divot, Color.Goldenrod); HatchBrush brushDotDiamond = new HatchBrush(HatchStyle.DottedDiamond, Color.Gainsboro); HatchBrush brushDottedGrid = new HatchBrush(HatchStyle.DottedGrid, Color.Khaki); HatchBrush brushForDiag = new HatchBrush(HatchStyle.ForwardDiagonal, Color.Maroon); HatchBrush brushHorz = new HatchBrush(HatchStyle.Horizontal, Color.Red); HatchBrush brushHorzBrick = new HatchBrush(HatchStyle.HorizontalBrick, Color.SaddleBrown); HatchBrush brushLgChkBoard = new HatchBrush(HatchStyle.LargeCheckerBoard, Color.RoyalBlue); HatchBrush brushLgConfetti = new HatchBrush(HatchStyle.LargeConfetti, Color.MistyRose); HatchBrush brushLgGrid = new HatchBrush(HatchStyle.LargeGrid, Color.Purple); HatchBrush brushLtDnDiag = new HatchBrush(HatchStyle.LightDownwardDiagonal, Color.DarkCyan); HatchBrush brushLtHorz = new HatchBrush(HatchStyle.LightHorizontal, Color.PowderBlue); HatchBrush brushUpDiag = new HatchBrush(HatchStyle.LightUpwardDiagonal, Color.SeaGreen); HatchBrush brushLtVert = new HatchBrush(HatchStyle.LightVertical, Color.Olive); e.Graphics.FillRectangle(brushBackDiag, 20, 20, 80, 60); e.Graphics.FillRectangle(brushCross, 120, 20, 80, 60); e.Graphics.FillRectangle(brushDarkDown, 220, 20, 80, 60); e.Graphics.FillRectangle(brushDarkHorz, 320, 20, 80, 60); e.Graphics.FillRectangle(brushDarkUpDiag, 420, 20, 80, 60); e.Graphics.FillRectangle(brushVertical, 20, 100, 80, 60); e.Graphics.FillRectangle(brushDashDnDiag, 120, 100, 80, 60); e.Graphics.FillRectangle(brushDashHorz, 220, 100, 80, 60); e.Graphics.FillRectangle(brushDashUpDiag, 320, 100, 80, 60); e.Graphics.FillRectangle(brushDashVert, 420, 100, 80, 60); e.Graphics.FillRectangle(brushDashVert, 20, 180, 80, 60); e.Graphics.FillRectangle(brushDiagBrisk, 120, 180, 80, 60); e.Graphics.FillRectangle(brushDiagCross, 220, 180, 80, 60); e.Graphics.FillRectangle(brushDivot, 320, 180, 80, 60); e.Graphics.FillRectangle(brushDotDiamond, 420, 180, 80, 60); e.Graphics.FillRectangle(brushDottedGrid, 20, 260, 80, 60); e.Graphics.FillRectangle(brushForDiag, 120, 260, 80, 60); e.Graphics.FillRectangle(brushHorz, 220, 260, 80, 60); e.Graphics.FillRectangle(brushHorzBrick, 320, 260, 80, 60); e.Graphics.FillRectangle(brushLgChkBoard, 420, 260, 80, 60); e.Graphics.FillRectangle(brushLgGrid, 20, 340, 80, 60); e.Graphics.FillRectangle(brushLtDnDiag, 120, 340, 80, 60); e.Graphics.FillRectangle(brushLtHorz, 220, 340, 80, 60); e.Graphics.FillRectangle(brushUpDiag, 320, 340, 80, 60); e.Graphics.FillRectangle(brushLtVert, 420, 340, 80, 60); } } public class Program { public static int Main() { Application.Run(new Exercise()); return 0; } } This would produce:
If you use the above constructor to fill out a shape, the selected pattern would be drawn on top of a black color used as the background. If you want to use a different background, use the following constructor to initialize the brush: public HatchBrush(HatchStyle hatchstyle, Color foreColor, Color backColor); The backColor argument passed as a Color value will be used as the background Color. Here are examples of specifying the back color: private void Exercise_Paint(object sender, PaintEventArgs e) { HatchStyle[] hsBrush = { HatchStyle.BackwardDiagonal, HatchStyle.Cross, HatchStyle.Divot, HatchStyle.DarkDownwardDiagonal, HatchStyle.DarkHorizontal, HatchStyle.ForwardDiagonal, HatchStyle.DarkUpwardDiagonal, HatchStyle.DarkVertical, HatchStyle.HorizontalBrick, HatchStyle.DashedDownwardDiagonal, HatchStyle.DashedHorizontal, HatchStyle.DashedVertical, HatchStyle.LargeCheckerBoard, HatchStyle.DiagonalBrick, HatchStyle.Horizontal, HatchStyle.DiagonalCross, HatchStyle.DottedGrid, HatchStyle.DottedDiamond, HatchStyle.LightUpwardDiagonal, HatchStyle.LargeConfetti, HatchStyle.LargeGrid, HatchStyle.LightDownwardDiagonal, HatchStyle.OutlinedDiamond HatchStyle.LightHorizontal, HatchStyle.LightVertical }; Color[] ForeColors = { Color.FromArgb(0, 0, 255), Color.FromArgb(200, 0, 0), Color.Salmon, Color.Navy, Color.Pink, Color.FromArgb(255, 0, 255), Color.FromArgb(255, 128, 0), Color.FromArgb(0, 128, 192), Color.Green, Color.Firebrick, Color.Fuchsia, Color.Moccasin, Color.Goldenrod, Color.Gainsboro, Color.Khaki, Color.Maroon, Color.DarkCyan, Color.Purple, Color.MistyRose, Color.RoyalBlue, Color.Red, Color.SaddleBrown, Color.Olive, Color.SeaGreen, Color.PowderBlue }; Color[] BackColors = { Color.Azure, Color.DarkBlue, Color.AntiqueWhite, Color.Aqua, Color.DarkGray, Color.Aquamarine, Color.Azure, Color.Beige, Color.DarkGoldenrod, Color.Bisque, Color.DarkKhaki, Color.BlanchedAlmond, Color.Brown, Color.DarkCyan, Color.AliceBlue, Color.BurlyWood, Color.CadetBlue, Color.DarkMagenta, Color.Coral, Color.Chartreuse, Color.CornflowerBlue, Color.Cornsilk, Color.Crimson, Color.Cyan, Color.DarkGreen }; Random rnd = new Random(); HatchBrush brushBackDiag = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushCross = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushDarkDown = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushDarkHorz = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushDarkUpDiag = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushVertical = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushDashDnDiag = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushDashHorz = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushDashUpDiag = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushDashVert = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushDiagBrisk = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushDiagCross = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushDivot = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushDotDiamond = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushDottedGrid = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushForDiag = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushHorz = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushHorzBrick = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushLgChkBoard = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushLgConfetti = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushLgGrid = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushLtDnDiag = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushLtHorz = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushUpDiag = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); HatchBrush brushLtVert = new HatchBrush(hsBrush[rnd.Next(25)], ForeColors[rnd.Next(25)], BackColors[rnd.Next(25)]); e.Graphics.FillRectangle(brushBackDiag, 20, 20, 80, 60); e.Graphics.FillRectangle(brushCross, 120, 20, 80, 60); e.Graphics.FillRectangle(brushDarkDown, 220, 20, 80, 60); e.Graphics.FillRectangle(brushDarkHorz, 320, 20, 80, 60); e.Graphics.FillRectangle(brushDarkUpDiag, 420, 20, 80, 60); e.Graphics.FillRectangle(brushVertical, 20, 100, 80, 60); e.Graphics.FillRectangle(brushDashDnDiag, 120, 100, 80, 60); e.Graphics.FillRectangle(brushDashHorz, 220, 100, 80, 60); e.Graphics.FillRectangle(brushDashUpDiag, 320, 100, 80, 60); e.Graphics.FillRectangle(brushDashVert, 420, 100, 80, 60); e.Graphics.FillRectangle(brushDashVert, 20, 180, 80, 60); e.Graphics.FillRectangle(brushDiagBrisk, 120, 180, 80, 60); e.Graphics.FillRectangle(brushDiagCross, 220, 180, 80, 60); e.Graphics.FillRectangle(brushDivot, 320, 180, 80, 60); e.Graphics.FillRectangle(brushDotDiamond, 420, 180, 80, 60); e.Graphics.FillRectangle(brushDottedGrid, 20, 260, 80, 60); e.Graphics.FillRectangle(brushForDiag, 120, 260, 80, 60); e.Graphics.FillRectangle(brushHorz, 220, 260, 80, 60); e.Graphics.FillRectangle(brushHorzBrick, 320, 260, 80, 60); e.Graphics.FillRectangle(brushLgChkBoard, 420, 260, 80, 60); e.Graphics.FillRectangle(brushLgGrid, 20, 340, 80, 60); e.Graphics.FillRectangle(brushLtDnDiag, 120, 340, 80, 60); e.Graphics.FillRectangle(brushLtHorz, 220, 340, 80, 60); e.Graphics.FillRectangle(brushUpDiag, 320, 340, 80, 60); e.Graphics.FillRectangle(brushLtVert, 420, 340, 80, 60); } At any time, to find out the color used to paint a pattern, you can access the brush's ForegroundColor property. To know the color used as background, you can access the brush's BackgroundColor property. To know the hatch style used on the current brush, you can access its HatchStyle property.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||