Display transparent bitmap in C + + Builder

zhaozj2021-02-08  239

Look at the transparent bitmap in C Builder

We know that the GIF type image format support is expressed, while the BMP type image format is not supported. In the MICROSOFT tablet editor, if it is set to a transmissive color mode, it will be shown. However, in C Builder, the transparent bitmap can be displayed lightly. The transmissive bitmap has a large advantage than the transmissive GIF image, namely the GIF image format maximizes 256 color, but the bitmap can support true color.

---- Display transparent bitmap in C Builder

---- 1. Adding a TIMAGE component.

---- Next, I made a simple moving painting of a flying machine. Because the mobile plane is displayed with the transparent bitmap, it is very realistic.

---- (1) Creating a new process in C Builder, "File / New Application" creates a new process, and the window body unit uses the file Planemain.cpp save, the work file is powered by a Plane.cpp.

---- (2) Add a TButton component button1 on the Form1 window.

---- Add a TTIMER component Timer1, add three TIMAGE components image1, image2, and image3, add a TLABEL.

---- (3) Double-click Image1 on the form, transfer to the blue sky bitmap Bluesky.bmp, set its transpartant property to false, which is opaque, as a background; double click image2, transfer to the aircraft bitmap Plane.BMP Setting its transparent attribute to true, which is transparent, the AutoSize property is TRUE, put the image2 on the image1. At this time, it can be seen that the plane has been integrated into the blue sky. As a contrast, double click on Image3, transfer the bitmap Plane.BMP of the aircraft, and set its transparent attribute to false, which is an opaque, autosize property is TRUE. Set the CAPTION attribute of Label1 as "source bitmap". Setting the CAPTION attribute of Button1 is "start". Setting Timer1's enabled attribute to false, the Interval property is 100 (decided to fast, adjustable).

---- (4) Timer1's OntiMER event controlled aircraft movement, the code is as follows:

Void __fastcall tform1 :: Timer1Timer

(TOBJECT * Sender)

{Image2 -> left = image2 -> left 1;}

---- Button1's onclick event starts the plane, the code is as follows: void __fastcall tform1 :: Button1click

(TOBJECT * Sender)

{TIMER1 -> enabled = true;

---- The picture below is the result of the program:

---- 2. Turn it into the bitmap from the file, then draw on the screen. This method is flexible, and it is less in memory. Continue to the top section design:

---- (1) Add a TBUTTON component button2 on the window, set its CAPTION attribute to "display the transparent bitmap".

---- (2) Button2's onclick event shows the transparent bitmap, the code is as follows:

Void __fastcall tform1 :: button2click (Tobject * Sender)

// Enter the "Display Transfer Play" button

{

Graphics :: Tbitmap * Pbitmap = new graphics :: tbitmap ();

Try

{Pbitmap -> LoadFromfile ("Plane.BMP");

// Turn into a bitmap

Pbitmap -> Transparent = true;

// Setting the transmissature property

Pbitmap -> TransparentColor =

Pbitmap -> Canvas -> Pixels [2] [2];

/ / Take it from the bitmap

The transmissive color can also be directly given

Form1 -> Canvas -> DRAW (10, 20, Pbitmap);

// aa

// Display the transparent bitmap on the window,

No setup transparentmode

// The color of the pixel at the bitmap [2] [2] is a bright color

Pbitmap -> TransparentMode = TMAUTO;

// Set the transparent mode, TMAUTO:

TransparentColor property returns

// The color color of the top left corner pixel is made as a bright color;

// TMFixed: TransparentColor property returns a bitmap

The color of a pixel is made as a bright color.

Form1 -> Canvas -> DRAW (10,100, Pbitmap);

// bb

// Display the transparent bitmap on the window,

There is different from the AA line

// Color of the pixel in the uppermost corner of the bitmap is bright color

Image1 -> Canvas -> DRAW (30, 20, Pbitmap);

/ / Display the transparent bitmap on the back bitmap

}

Catch (...)

{

ShowMessage ("cannot be transferred or displayed!"); / / Show the wrong information

}

Delete Pbitmap; / / Delete

}

---- When this method is used to engage in the image, the back view image is required to be a bitmap, and the color bitmap of the light-clear bitmap and the back bitmap should be kept, so that the color of the bitmap will not change.

---- In addition, we can use the TimageList component to display the transparent image, and use the brushcopy method of the Tcanvas class to be translated into a manner that is now copied.

---- Trial in WIN 98 and C Builder3 at the above order.

转载请注明原文地址:https://www.9cbs.com/read-1576.html

New Post(0)