[ Pobierz całość w formacie PDF ]
.8-6 Dev el oper s Gui deOv e r v i e w o f g r a p h i c s p r o g r a mmi n gThe task of setting the properties of pen is an ideal case for having different controlsshare same event handler to handle events.To determine which control actually gotthe event, you check the Sender parameter.To create one click-event handler for six pen-style buttons on a pen s toolbar, do thefollowing:1 Select all six pen-style buttons and select the Object Inspector|Events|OnClickevent and in the Handler column, type SetPenStyle.Delphi generates an empty click-event handler called SetPenStyle and attaches it tothe OnClick events of all six buttons.2 Fill in the click-event handler by setting the pen s style depending on the value ofSender, which is the control that sent the click event:procedure TForm1.SetPenStyle(Sender: TObject);beginwith Canvas.Pen dobeginif Sender = SolidPen then Style := psSolidelse if Sender = DashPen then Style := psDashelse if Sender = DotPen then Style := psDotelse if Sender = DashDotPen then Style := psDashDotelse if Sender = DashDotDotPen then Style := psDashDotDotelse if Sender = ClearPen then Style := psClear;end;end;Changing the pen modeA pen s Mode property lets you specify various ways to combine the pen s color withthe color on the canvas.For example, the pen could always be black, be an inverse ofthe canvas background color, inverse of the pen color, and so on.See TPen in onlineHelp for details.Getting the pen positionThe current drawing position the position from which the pen begins drawing itsnext line is called the pen position.The canvas stores its pen position in its PenPosproperty.Pen position affects the drawing of lines only; for shapes and text, youspecify all the coordinates you need.To set the pen position, call the MoveTo method of the canvas.For example, thefollowing code moves the pen position to the upper left corner of the canvas:Canvas.MoveTo(0, 0);Note Drawing a line with the LineTo method also moves the current position to theendpoint of the line.Wor k i ng wi t h gr aphi c s and mul t i medi a 8-7Ov e r v i e w o f g r a p h i c s p r o g r a mmi n gUsing brushesThe Brush property of a canvas controls the way you fill areas, including the interiorof shapes.Filling an area with a brush is a way of changing a large number ofadjacent pixels in a specified way.The brush has three properties you can manipulate:" Color property: Changes the fill color" Style property: Changes the brush style" Bitmap property: Uses a bitmap as a brush patternThe values of these properties determine the way the canvas fills shapes or otherareas.By default, every brush starts out white, with a solid style and no patternbitmap.You can use TBrushRecall for quick saving off and restoring the properties of brushes.Changing the brush colorA brush s color determines what color the canvas uses to fill shapes.To change thefill color, assign a value to the brush s Color property.Brush is used for backgroundcolor in text and line drawing so you typically set the background color property.You can set the brush color just as you do the pen color, in response to a click on acolor grid on the brush s toolbar (see Changing the pen color on page 8-6):procedure TForm1.BrushColorClick(Sender: TObject);beginCanvas.Brush.Color := BrushColor.ForegroundColor;end;Changing the brush styleA brush style determines what pattern the canvas uses to fill shapes.It lets youspecify various ways to combine the brush s color with any colors already on thecanvas.The predefined styles include solid color, no color, and various line andhatch patterns.To change the style of a brush, set its Style property to one of the predefined values:bsSolid, bsClear, bsHorizontal, bsVertical, bsFDiagonal, bsBDiagonal, bsCross, orbsDiagCross.This example sets brush styles by sharing a click-event handler for a set of eightbrush-style buttons.All eight buttons are selected, the Object Inspector|Events|OnClick is set, and the OnClick handler is named SetBrushStyle.Here is the handlercode:procedure TForm1.SetBrushStyle(Sender: TObject);beginwith Canvas.Brush dobeginif Sender = SolidBrush then Style := bsSolidelse if Sender = ClearBrush then Style := bsClear8-8 Dev el oper s Gui deOv e r v i e w o f g r a p h i c s p r o g r a mmi n gelse if Sender = HorizontalBrush then Style := bsHorizontalelse if Sender = VerticalBrush then Style := bsVerticalelse if Sender = FDiagonalBrush then Style := bsFDiagonalelse if Sender = BDiagonalBrush then Style := bsBDiagonalelse if Sender = CrossBrush then Style := bsCrosselse if Sender = DiagCrossBrush then Style := bsDiagCross;end;end;Setting the Brush Bitmap propertyA brush s Bitmap property lets you specify a bitmap image for the brush to use as apattern for filling shapes and other areas.The following example loads a bitmap from a file and assigns it to the Brush of theCanvas of Form1:varBitmap: TBitmap;beginBitmap := TBitmap.Create;tryBitmap.LoadFromFile('MyBitmap.bmp');Form1.Canvas.Brush.Bitmap := Bitmap;Form1.Canvas.FillRect(Rect(0,0,100,100));finallyForm1.Canvas.Brush.Bitmap := nil;Bitmap.Free;end;end;Note The brush does not assume ownership of a bitmap object assigned to its Bitmapproperty.You must ensure that the Bitmap object remains valid for the lifetime of theBrush, and you must free the Bitmap object yourself afterwards.Reading and setting pixelsYou will notice that every canvas has an indexed Pixels property that represents theindividual colored points that make up the image on the canvas [ Pobierz całość w formacie PDF ]