C ++ builder button design special effect Example 3

Source: Internet
Author: User

When using a program, the most frequently used visualization component can be called a button. If some special display effects are added to the buttons when we design the program, it will certainly add a lot of fun to your application. Next I will introduce three special image button display effects for you, shows the running effect.

1. Implement the image button (the start button in the middle) in the program design process, using the image creation button can not only make the color and shape of the button more flexible, but also do not need to write too much code, you can get twice the result with half the effort. The following describes how to use the image component design button.

The steps to design the image button are as follows:

1. Create a new window, insert an image component timage for the create button and name it image1, and add a tlabel component named label1.

2. Specify the picture attribute of the timage component as the image file initially displayed by the button. Set the autosize attribute to true to automatically match the image size. Set the transperate attribute to true to make the timage itself transparent;

3. Write the following event handle for the onmousedown and onmouseup events of the timage component:

// The status of the event handle when the mouse is pressed --------------------------------------

Void _ fastcall tform1: image2mousedown (tobject * sender, tmousebutton, tshiftstate shift, int X, int y)

{

Image1-> picture-> loadfromfile (". // start2.bmp"); // The image displayed when the mouse is pressed, with the file name start2.bmp

}

// Handle the status of the event handle when the mouse is opened --------------------------------------

Void _ fastcall tform1: image1mouseup (tobject * sender, tmousebutton, tshiftstate shift, int X, int y)

{

Image1-> picture-> loadfromfile (". // start1.bmp"); // The image displayed when the mouse is pressed, with the file name start1.bmp

}

4. Write the code to be processed for The onclick event of the timage component, that is, use the timage component as the same button component. In this example, we just make the program display a title "you pressed the image button ". The Code is as follows:

Void _ fastcall tform1: image1click (tobject * sender)

{

Label1-> caption = "you have selected the image button ";

}

2. Implement buttons of any shape (any shape in the button) if the button to be designed is a button of irregular shape, we only need to use the image processing software to draw two buttons of irregular shape. A highlighted button is flat or concave, and the rest are all designed to be the same. Then, add the onmousedown, onmouseup, and onclick events for the above example.

3. The realization of animation buttons is still based on the rational use of the timage component. The basic implementation principle is: Add a ttimer component in the form to control the circular transformation of the image, and then call the pre-designed continuous bitmap to replace it cyclically. After clicking the button, the event processing process is still implemented by The onclick event handle. The implementation steps are as follows:

1. Add a ttimer component to the form, set its enabled attribute to true, and set its interval attribute to 100; then add an image component timage used to create an animation button in the form and name it image1. Add a tlabel component named label1. Specify the picture attribute of the timage component as the image file initially displayed by the button. Set the autosize attribute to true to automatically match the image size. Set the transperate attribute to true to make the timage itself transparent;

2. Define an image counter in the private part of the class definition of the main form to control the alternate display of the image without repeating or stopping it. For example, int loadflag;

3. Define the ontimer event handle of the ttimer component as follows:

Void _ fastcall tform1: timer1timer (tobject * sender)

{

Showbtnimage (loadflag );

}

Note: This event handle calls a user-defined function, which determines which image to call for Display Based on the loadflag value.

The showbtnimage function contains the following content ://---------------------------------------------------------------------------

Void tform1: showbtnimage (int n)

{

If (loadflag> 0 & loadflag <6) image1-= ""> picture-> loadfromfile (". // btnimage3-"+ inttostr (n) + ". BMP "); // (2)

Loadflag ++; // (3)

}

Else

Loadflag = 1; // (4)

}

//----------------------------------------------------------------------------

Routine explanation:

(1) The sentence is used to determine whether loadflag is in 1 ~ This range is related to the number of images to be loaded. Because the current routine uses six images in series, the loadflag is limited to 1 ~ Between 6, the advantage of doing so is that the ontimer event each loaded image must be continuous and never repeated.

(2) Call the loadfromfile method of the picture object to load the image from the specified image file.

(3) increase the image display flag by 1 to ensure that the next called image is the next continuous image.

(4) When the loadflag mark exceeds the number of image files, it is automatically restored to 1, that is, the next mounted image file must be the first image. Finally, you must define the UDF in the private part of the program header file as follows:

Void showbtnimage (int n );

4. Write an event handle for The onclick event of the timage component to process specific program events. Example:

Void _ fastcall tform1: image1click (tobject * sender)

{

Label1-> caption = "you have selected this Animation button ";

}

5. Because the animation is continuously running, When you click a button, it is necessary to make the button look like a response to the user's action, therefore, the onmousedown and onmouseup events of the timage component must be handled as follows:

Void _ fastcall tform1: image1mousedown (tobject * sender, tmousebutton, tshiftstate shift, int X, int y)

{

Image1-> picture-> loadfromfile (". // BtnImage3-1.bmp"); timer1-> enabled = false;

}

//---------------------------------------------------------------------------

Void _ fastcall tform1: image1mouseup (tobject * sender, tmousebutton, tshiftstate shift, int X, int y)

{

Loadflag = 1; timer1-> enabled = true;

}

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.