[ Pobierz całość w formacie PDF ]
.The second uses a canvas to draw thesame ellipse in an application written with Delphi.procedure TMyWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);varPenHandle, OldPenHandle: HPEN;BrushHandle, OldBrushHandle: HBRUSH;beginPenHandle := CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); { create blue pen }OldPenHandle := SelectObject(PaintDC, PenHandle); { tell DC to use blue pen }BrushHandle := CreateSolidBrush(RGB(255, 255, 0)); { create a yellow brush }OldBrushHandle := SelectObject(PaintDC, BrushHandle); { tell DC to use yellow brush }Ellipse(HDC, 10, 10, 50, 50); { draw the ellipse }SelectObject(OldBrushHandle); { restore original brush }DeleteObject(BrushHandle); { delete yellow brush }SelectObject(OldPenHandle); { restore original pen }DeleteObject(PenHandle); { destroy blue pen }end;procedure TForm1.FormPaint(Sender: TObject);beginwith Canvas dobeginPen.Color := clBlue; { make the pen blue }Brush.Color := clYellow; { make the brush yellow }Ellipse(10, 10, 50, 50); { draw the ellipse }end;end;45-2 Devel oper s Gui deUs i n g t h e c a n v a sUsing the canvasThe canvas class encapsulates graphics controls at several levels, including high-levelfunctions for drawing individual lines, shapes, and text; intermediate properties formanipulating the drawing capabilities of the canvas; and in the VCL, provides low-level access to the Windows GDI.Table 45.1 summarizes the capabilities of the canvas.Table 45.1 Canvas capability summaryLevel Operation ToolsHigh Drawing lines and shapes Methods such as MoveTo, LineTo, Rectangle,and EllipseDisplaying and measuring text TextOut, TextHeight, TextWidth, andTextRect methodsFilling areas FillRect and FloodFill methodsIntermediate Customizing text and graphics Pen, Brush, and Font propertiesManipulating pixels Pixels property.Copying and merging images Draw, StretchDraw, BrushCopy, and CopyRectmethods; CopyMode propertyLow Calling Windows GDI functions Handle propertyFor detailed information on canvas classes and their methods and properties, seeonline Help.Working with picturesMost of the graphics work you do in Delphi is limited to drawing directly on thecanvases of components and forms.Delphi also provides for handling stand-alonegraphic images, such as bitmaps, metafiles, and icons, including automaticmanagement of palettes.There are three important aspects to working with pictures in Delphi:" Using a picture, graphic, or canvas" Loading and storing graphics" Handling palettesUsing a picture, graphic, or canvasThere are three kinds of classes in Delphi that deal with graphics:" A canvas represents a bitmapped drawing surface on a form, graphic control,printer, or bitmap.A canvas is always a property of something else, never a stand-alone class.Usi ng gr aphi cs i n component s 45-3Wo r k i n g wi t h p i c t u r e s" A graphic represents a graphic image of the sort usually found in a file or resource,such as a bitmap, icon, or metafile.Delphi defines classes TBitmap, TIcon, andTMetafile, all descended from a generic TGraphic.You can also define your owngraphic classes.By defining a minimal standard interface for all graphics, TGraphicprovides a simple mechanism for applications to use different kinds of graphicseasily." A picture is a container for a graphic, meaning it could contain any of the graphicclasses.That is, an item of type TPicture can contain a bitmap, an icon, a metafile,or a user-defined graphic type, and an application can access them all in the sameway through the picture class.For example, the image control has a propertycalled Picture, of type TPicture, enabling the control to display images from manykinds of graphics.Keep in mind that a picture class always has a graphic, and a graphic might have acanvas.(The only standard graphic that has a canvas is TBitmap.) Normally, whendealing with a picture, you work only with the parts of the graphic class exposedthrough TPicture.If you need access to the specifics of the graphic class itself, you canrefer to the picture s Graphic property.Loading and storing graphicsAll pictures and graphics in Delphi can load their images from files and store themback again (or into different files).You can load or store the image of a picture at anytime.CLX Note You can also load images from and save them to a Qt MIME source, or a streamobject if creating CLX components.To load an image into a picture from a file, call the picture s LoadFromFile method.Tosave an image from a picture into a file, call the picture s SaveToFile method.LoadFromFile and SaveToFile each take the name of a file as the only parameter.LoadFromFile uses the extension of the file name to determine what kind of graphicobject it will create and load.SaveToFile saves whatever type of file is appropriate forthe type of graphic object being saved.To load a bitmap into an image control s picture, for example, pass the name of abitmap file to the picture s LoadFromFile method:procedure TForm1.LoadBitmapClick(Sender: TObject);beginImage1.Picture.LoadFromFile('RANDOM.BMP');end;The picture recognizes.bmp as the standard extension for bitmap files, so it createsits graphic as a TBitmap, then calls that graphic s LoadFromFile method.Because thegraphic is a bitmap, it loads the image from the file as a bitmap.45-4 Devel oper s Gui deWo r k i n g wi t h p i c t u r e sHandling palettesFor VCL components, when running on a palette-based device (typically, a 256-colorvideo mode), Delphi controls automatically support palette realization.That is, if youhave a control that has a palette, you can use two methods inherited from TControl tocontrol how Windows accommodates that palette
[ Pobierz całość w formacie PDF ]