Fifth chapter-delphi Graphic image Programming (i) (2)

Source: Internet
Author: User
Tags requires textout

5.1.3 Tbrush object (painter)

A brush object is used to fill a shape, such as a rectangle or ellipse, with a brush color or pattern. Tbrush has a brush handle (hbrush).

The colors of the brush are defined in the Color property. The brush also has a bitmap property that can only be obtained at run time, and the brush can fill the graphic with a bitmap to produce special effects. Bitmap size is 8 pixel points, height 8 pixel dot wide.

The Style property defines how the brush fills the drawing.

5.1.4 TColor Type

The TColor type is used to define the color of an object. The color properties of many parts are tcolor types, and the TColor defined in the graphics unit is as follows:

TColor =-(color_endcolors + 1). $02FFFFF;

This is a 32-bit binary data. The graphic unit also defines some common color constants that are either mapped directly to the closest color in the system palette or to the system video color that is mapped to the color portion of the Wondows Control Panel.

The colors that are mapped directly into the system palette are:

Claqua,clblack,clblue,clbkgrray,clfuchsoa ... Clyellow

The mapper uses a 4-byte binary to define the color, and a low 3-bit byte represents the corresponding color of RGB, such as $00ff0000 Blue, $0000ff00 for pure green, $000000ff for pure red, $00000000 for black, $00ffffff for white. If the highest bit byte is $00, it represents the closest color in the system palette, the highest byte is $01, the closest color in the current palette is matched, and the highest byte is $02, then the second-closest color of the logical palette in the current Device description table matches.

The SelectPalette function of the Windows API creates a logical palette that implements the mapping of the logical palette to the hardware palette and requires a function Realizepalett

5.2 Development of graphics program

GRAPHEX.DPR is a simple graphical image application that runs in the state shown in Figure 5.2. The program can draw a variety of graphics with the mouse, set the brush color, brush fill mode, and also browse bitmaps, meta files, icons, change their size, and print. This section combines routines to describe the following issues:

Add an Accelerator button to the sidebar

Responding to mouse events

Set Brushes and brushes

Implementing the "Eraser" feature of a drawing

Join status bar

5.2.1 Add an Accelerator button to the sidebar

The accelerator button is a commonly used part of an application that is a quick form of an alternative menu, typically concentrating multiple accelerator buttons in a single toolbar for ease of use. There are three toolbars in the Graphex, they are tpancel parts, and there are several sets of acceleration buttons on the panel to set the drawing style, brushes, and brushes.

The Glyph property of each accelerator button is an image object that is used to indicate whether the button is being used. Glyph usually requires four images, respectively, to press, not press, select, failure of four states. Programmers can choose images based on personal preferences.

The accelerator button uses an image to tell the user its status and purpose, because the button has no caption, so the user should be given the correct prompt:

Set the Down property to a true value so that the accelerator button is pressed in state

Setting the Enable property to a false value causes the accelerator button to become inactive.

The default drawing tool in a routine is to draw a line, so the line-drawing button appears pressed when the application starts.

In an application, you often put buttons that are similar in function to one button at a time. When the other button is pressed, the button that was pressed automatically bounces, and the selection button is called a set of accelerator buttons.

To make more than one accelerator button a group, set the button's Graphindex property to the same value. The easiest way to do this is to select each acceleration button with the mouse in the design state, and then set the Graphindex value.

Sometimes the user presses a button that has been pressed, hoping that the button will bounce, so that no buttons are pressed and the Allowallup property is used to achieve the above function. For a set of accelerator buttons, setting the Allowallup of any of the buttons in the group makes each button in this group functional.

In the Graphex program, three sets of acceleration buttons and two individual acceleration buttons are designed. The first set of acceleration buttons is used to select the drawing tool, which is in the same panel as the two individual buttons, which are visible by default. The other two buttons represent brushes and brushes respectively. The second and third groups of acceleration buttons are located in two panels that are not visible by default. They represent different styles of brushes and brushes, only the first panel of the brush (or brush) button, the second (or third) panel will be displayed, so that users can choose the brush, painting brush.

5.2.2 Response to mouse events

The mouse is often used as a tool for drawing, and the application uses changes in the mouse position to draw a variety of different graphics. The mouse has three actions: mouse button Press, mouse movement, mouse button bounce. In Delphi, the corresponding three actions have three different events: Onmousedown,onmousemove,onmouseup.

When the Dlephi application detects a mouse action, it passes five parameters and invokes the corresponding event response.

These parameters can be used by programmers to define event programs. The five parameters are as follows table 5.4:

Table 5.4 Five parameters for mouse events

━━━━━━━━━━━━━━━━━━━━━━━━━━━

Parameter meaning

──────────────────────────

Sender object to detect mouse action

button refers to the mouse buttons: Left button, middle key, right button

The state of the Alt,ctrl,shift button when the Shift mouse action

The coordinates of the mouse when the X,y event occurs

━━━━━━━━━━━━━━━━━━━━━━━━━━━

The onmousedown event occurs when the mouse is pressed. Give a simple example to illustrate how a program can enter the event

Row response. If we want to appear "here" where the mouse is pressed.

Responding to mouse onmousedown events

You can call the TextOut method in this event:

Procedure Tform1.formmousedown (Sender:tobject,button:tmousebutton;

Shift:tshifstate; X,y:integer);

Begin

Canvas.textout (X, Y, ' here! ');

End

OnMouseUp event occurs when the user relaxes the mouse button. When this event occurs, the object that the mouse arrives at is not necessarily the object that the mouse is on when the mouse button is pressed. For example, a user can draw a line segment outside the form (the mouse is outside the form and the line segment is inside the window). The following code draws a line using the mouse:

Procedure Tform1.formmousedown (Sender:tobject)

Begin

Moveto (X,y);

End

Procedure Tform1.formmouse up (Sender:tobject)

Begin

Lineto (X, Y);

End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.