The usage tips in Delphi are so simple. Just set the control you want to use Hint as follows:
ShowHint := True;
Hint := 'Prompt message';
It doesn't have to write a line of code, it's quite convenient.
But sometimes we want to customize the effects of the prompts to make them look more beautiful and personal. It doesn’t matter, Delphi has a complete way to let you write your favorite Hint effect.
Delphi's Hint function implementation is classified in the application class, so we can see several properties about Hint in the Application class. These properties can set the color of the Hint window, the residence time, the appearance time, etc. If these properties are set, the It has an impact on the Hint function of the entire project. The advantage of doing this is of course that it unifies the style of Hint and makes other classes ignore the implementation of Hint.
We can build a simple project, put a button, set the ShowHint of the button to True, and then set a value for the Hint. When running the program, a prompt window will appear when the cursor points to the button.
But if we write in the creation event of the main window:
PRocedure TForm1.FormCreate(Sender: TObject);
Begin
Application.ShowHint := False;
end;
After running the program, there will be no prompts. This shows that the ShowHint of Application controls whether the Hint of the entire project is displayed.
If you are tired of the color of the Hint window you usually see, you can set the HintColor of Application to another color. But there is a problem at this time. If HintColor is set to black, the prompt font is also black, and the prompt message will not be visible. To do this, we have to understand another global object. In fact, when the program runs, three global objects will be created: Application, Screen, and Mouse. The responsibilities of the three objects are very obvious. Screen encloses the state of running the project on the screen, it has a HintFont property that allows you to set the font of the prompt message.
We can write the following code:
procedure TForm1.Button1Click(Sender: TObject);
Begin
Application.HintColor := clBlack;
Screen.HintFont.Color := clWindow;
Screen.HintFont.Size := 14;
end;
Run the program to see the effect, and it prompts that the font has become white and has become larger.
In addition, Application has these three properties:
HintHidePause, HintPause, HintShortPause, controls the time displayed in the prompt window, etc. HintHidePause specifies the time the prompt window will be displayed on the screen, in milliseconds. HintPause specifies how long it takes for the prompt window to appear in milliseconds when you move the cursor to the prompted control. HintShortPause means that when you quickly move the cursor through a set of Hint controls, the interval of Hint is displayed. For example, there are two Hint controls. When your cursor quickly moves from Btn1 to Btn2, Hint will only be displayed after HintShortPause milliseconds.
There is a special attribute Hint in Application. We can't help but wonder that Hint specifies the prompt of the control. In fact, a big purpose of the Hint attribute is to give controls that cannot directly appear in Hint windows, so that they can appear prompts in other ways. For example, for menus, we cannot make the menu appear in the Hint window, but we can make the menu's Hint appear on the status bar.
We add a status bar to the main window of the project above, add a menu control, set several menu items, and set some strings for the recommended Hint attributes for each menu.
Then write down:
procedure TForm1.FormCreate(Sender: TObject);
Begin
Application.OnHint := WhenHint;
end;
procedure TForm1.WhenHint(sender: TObject);
Begin
StatusBar1.SimpleText := Application.Hint;
end;
When you run the program, when you point to the menu item, you will see that a prompt appears on the status bar.
As you can see above, some simple code can make the prompts unique. But people will never be satisfied. They always want to be able to make a better-looking Hint, and even put forward requirements on Hint's window style. Delphi engineers thought of this long ago. They set up a parent class of a prompt window through class inheritance, that is, the Hint window we see. We can write ourselves by inheriting it and overwriting the virtual methods it provides. prompt window.
Go and read the source code of HintWindow. As long as you cover a few virtual methods, you can make beautiful tips.
Although Delphi's Hint is simple and easy to use, it is not flexible enough because it provides a unified style, so you cannot specify a prompt as an error indication, or a prompt as a warning indication. Regarding this, we need to use an API to implement it. Find a comic-style tip online, and there are many articles available. I won't talk about it here.