Dress Up Tool Tips

zhaozj2021-02-08  249

Delphi skills

Dress Up Tool Tips

What is a tool tip? In the Windows program, when the mouse moves to the corresponding button corresponding to the toolbar, a small window will pop up to illustrate the function of the button, this small window is called the tool prompt. In general, its background color is pale yellow, the font color is black. In the face of such a thousand faces, I don't know if you are bored, do you want to give it a chamber, make it change your face? Below we will use Delphi as a tool, which is moving some of the surgery.

In Delphi, we can set the Hint and Showhint properties of the control to display the toolpump bar. Of course, this is not what we hope. Fortunately, Delphi provides us with HintColor properties in the Application object, with which you can set the background color of the tooltip strip. Write Application.hintColor: = CLRED, then run, how about running, how? Good effect, the background turns red. The next question is how we will change the color of the display text. (Well, let me think about it)

There is a ThintWindow class in Delphi, and you know what it is doing. Nice, Delphi is to achieve tooltip strips through it. Only four statements in its creation (create):

Constructor ThintWindow.create (Aowner: Tcomponent);

Begin

Inherited Create (Aowner);

Color: = $ 80fff;

Canvas.Font: = Screen.hintFont;

Canvas.brush.style: = bsclear;

END;

It first overloads the creation method of the parent class, and then sets the background color, font, and brush style. From here you can imagine, if we create a THintWindow derive class, and overload its creation method, then redefine the font in this method, don't we achieve what we want? Then let us act.

First create a new application, add a button button1 for testing on the Form1 form, set the showhint attribute to true, the hint property is "this is a button", then declares the Myhint class in front of the iMplementation section of the code page:

Type

MyHint = Class (thisTwindow)

Constructor Create (Aowner: Tcomponent); OVERRIDE;

END;

This class has only one way, with the Override keyword to indicate the creation method of the parent class (ThintWindow). The code is then written to this method in the Implementation section:

Constructor myHint.create (Aowner: tcomponent);

Begin

Inherited Create (Aowner); // Rearily loaded parent class method

WITH Canvas Do Begin // Setting Fonts

Font.name: = '体 _GB2312';

Font.color: = Clyellow;

Font.size: = 20;

Brush.style: = bsclear;

END;

END;

In this process we redefined the name, color, and size of the font, and now you can use this new class. So how do you use it? Here is a point: there is a hidden variable hintwindowclass in Delphi, which is the tool that specifies the tool prompts used in the program runtime, which is as follows: var hintwindowclass: thintwindowclass = thintWindow;

In the program we can dynamically change this variable, such as writing in the creation event of the main form: hintwindowclass: = myhint; this will allow it to use our newly created classes. Ok, now I am running a program, see how the effect? The font of the tooltip strip turns into a body, the size is also bigger, color ..., how color does not change? The new problem appears, or then look at this THINTWINDOW code.

The problem is in this THINTWINDOW's Paint method, its code is as follows:

Procedure thinkwindow.paint;

VAR

R: TRECT;

Begin

R: = ClientRect;

INC (R.LEFT, 2);

INC (R.TOP, 2);

Canvas.font.color: = clinfotext; // Please pay attention to this sentence

DrawText (Canvas.Handle, Pchar (CAPTION), -1, R, DT_LEFT OR DT_NOPREFIX OR DT_WORDBREAK or DRAWTEXTBIDEFLAGSREADINLY);

END;

It turns out that ThintWindow re-changes in its Paint method. No way, I have to rewrite the Paint method. In the MyHint class, a reload method is declared: procedure pressure; Override; then move the code of the ThintWindow's Paint method, and then remove the canvas.font.color: = clinfotext; this sentence is removed. At this time, I'm telling it, and then run the program to see, this problem is not.

Maybe you will say: "In fact, we don't have to override the ThintWindow's Create method, just overload the Paint method, then set the background and fonts in it, which will save much more." Of course, this is also available, But you will find a little regret, what is it? Try it yourself. (What? No ?! When I didn't say it!)

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

New Post(0)