Delphi的Hint入门

  Delphi中使用提示是如此简单,只需将欲使用Hint的控件作如下设置:

 ShowHint := True;
  Hint := ‘提示信息’;
  不必写一行代码,相当方便。
  但有时我们又想自己定制提示的效果,使其看起来更美观更具个人特色,没关系,Delphi完全有办法让你写出自己喜欢的Hint效果。
  Delphi的Hint功能实现归类在Application类中,所以我们可以在Application类中看到数个关于Hint的属性,这些属性可以设置Hint窗口的颜色,停留时间,出现时间等,设置了这些属性,将对整个工程的Hint功能起到影响。这样做的好处当然是统一了Hint的风格,并且让其他类不必去理会Hint的实现。
  我们可以建一个简单的工程,并放一个按钮,将按钮的ShowHint设为True,再对Hint设一个值。运行程序,当光标指到按钮上时,便会出现一个提示窗口。
  但如果我们在主窗口的创建事件中写下:

  procedure TForm1.FormCreate(Sender: TObject);
  begin
  Application.ShowHint := False;
  end;
  这些再运行程序,就不再有提示出现了,由此可知Application的ShowHint控制整个工程的Hint是否显示。
  如果你对于平常所见的Hint窗口的颜色感到厌烦,那么可以设Application的HintColor为其他颜色。但此时有一个问题,如果HintColor设为黑色,则提示字体也为黑色,就看不到提示信息了。为此,我们得了解另一个全局对象,事实上当程序运行时,会创建三个全局对象:Application,Screen,Mouse,三个对象的职责非常明显。Screen封闭了运行的工程在屏幕上的状态,它有一个HintFont的属性,允许你设置提示信息的字体。
  我们可以写如下的代码:

  procedure TForm1.Button1Click(Sender: TObject);
  begin
  Application.HintColor := clBlack;
  Screen.HintFont.Color := clWindow;
  Screen.HintFont.Size := 14;
  end;
  运行程序看看效果,提示字体变为白色,且变大了。
  另外Application有这三个属性:
  HintHidePause,HintPause,HintShortPause,控制着提示窗显示的时间等。HintHidePause指定提示窗口在屏幕上显示的时间,以毫秒为单位。HintPause则指定当你将光标移到有提示的控件上时,经过多长时间才会出现提示窗口,以毫秒为单位。而HintShortPause呢表示当你快速移动光标经过一组有Hint的控件时,显示Hint的间隔。比如有两个有Hint的控钮,当你的光标快速从Btn1移到Btn2时,Hint经过HintShortPause毫秒才会显示出来。
  Application中有一个比较特殊的属性Hint,我们不禁要奇怪,Hint指定的是那个控件的提示呢。其实Hint属性的一个很大的用途是给那些没有办法直接出现Hint窗口的控件一个机会,使它们能够通过别的方式出现提示。比如菜单,我们没有办法使菜单出现Hint窗口,但我们可以使菜单的Hint出现在状态栏上的。
  我们在上面的工程主窗口中加一个状态栏,并在加一个菜单控件,设置几个菜单项,并给每个菜单荐的Hint属性设置一些字符串。
  然后写下:

  procedure TForm1.FormCreate(Sender: TObject);
  begin
  Application.OnHint := WhenHint;
  end;
  procedure TForm1.WhenHint(sender: TObject);
  begin
  StatusBar1.SimpleText := Application.Hint;
  end;
  运行程序,当你指到菜单项时,看,状态栏上出现了提示了。
  上面可以看到,通过一些简单的代码,就可以使得提示别具特色。但人们是永远不会满足的,他们总想能不能做更好看的Hint呢,甚至对Hint的窗口风格提出了要求。Delphi的工程师们早想到了这一点,他们通过类的继承设定了一个提示窗口的父类,即我们看到的那个Hint窗口,我们可以通过继承它并覆盖它所提供的虚拟方法来写自己的提示窗口。
  去读一读HintWindow的源码吧,你只要覆盖几个虚拟方法,你就可以做出很漂亮的提示出来了。

Delphi的Hint虽然简单易用,但却不够灵活,因为它提供了统一的风格,所以你不能指定某个提示为错误指示,可某个提示为警告提示。关于这个,我们要用API来实现,在网上找一个漫画式提示,有很多文章可用。这里不再说述。
  下面将给出一个定制Hint窗口的例子。这个自定义Hint窗口的效果不错,以玻璃为边框,并且有阴影的效果。
  不过这之前,我们必须介绍一个如何定制,Hint的父类为THintWindow,在Controls单元中定义。我们看看几个虚拟方法,CreateParams设定窗口的风格,我们要覆盖掉它,使其没有边框。NCPaint画窗口的边框,我们也要覆盖它,因为我们不需要边框吗。Paint比较重要,为画Hint窗口客户区内容,当然要覆盖。不过最重要的当属ActivateHint,它会设定好窗口的大小,并显示它,我们就在这里定制一个类玻璃的窗口效果。下面给出该类的实现:

unit wdHintWnd;
interface
uses
Windows, Classes, Controls, Graphics, Forms, SysUtils, ExtCtrls;
type
TwdHintWnd = class(THintWindow)
private
FWndBmp: TBitmap; //窗口位图
FHintBmp: TBitmap; //提示信息位图
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure Paint; override;
procedure NCPaint(DC: HDC); override;
{画提示的图象}
procedure DrawHintImg(Bmp:TBitmap; AHint: string);
{取得提示窗口对应的桌面区域的图象}
procedure GetDesktopImg(Bmp: TBitmap; R: TRect);
{对桌面区域图象作处理,使其看起来像一块玻璃且带有一点阴影}
procedure EffectHandle(WndBmp, HintBmp: TBitmap);
public
constructor Create(Aowner: TComponent); override;
destructor Destroy; override;
procedure ActivateHint(Rect: TRect; const AHint: string); override;
end;
implementation
{ TwdHintWnd }
procedure TwdHintWnd.ActivateHint(Rect: TRect; const AHint: string);
var
P: TPoint;
begin
//在这里取得一个适当的尺寸显示文字
FHintBmp.Width := Rect.Right - Rect.Left;
FHintBmp.Height := Rect.Bottom - Rect.Top + 4;
DrawHintImg(FHintBmp, AHint);
FWndBmp.Width := Rect.Right - Rect.Left + 23;
FWndBmp.Height := Rect.Bottom - Rect.Top + 27;
Inc(Rect.Right, 23);
Inc(Rect.Bottom, 27);
BoundsRect := Rect;
if Left < Screen.DesktopLeft then
Left := Screen.DesktopLeft;
if Top < Screen.DesktopTop then
Top := Screen.DesktopTop;
if Left + Width > Screen.DesktopWidth then
Left := Screen.DesktopWidth - Width;
if Top + Height > Screen.DesktopHeight then
Top := Screen.DesktopHeight - Height;
GetDesktopImg(FWndBmp, BoundsRect);
EffectHandle(FWndBmp, FHintBmp);
P := ClientToScreen(Point(0, 0));
SetWindowPos(Handle, HWND_TOPMOST, P.X, P.Y, 0, 0,
SWP_SHOWWINDOW or SWP_NOACTIVATE or SWP_NOSIZE);
end;
constructor TwdHintWnd.Create(Aowner: TComponent);
begin
inherited;
FWndBmp := TBitmap.Create;
FWndBmp.PixelFormat := pf24bit;
FHintBmp := TBitmap.Create;
end;
procedure TwdHintWnd.CreateParams(var Params: TCreateParams);
begin
inherited;
//去掉窗口边框
Params.Style := Params.Style and not WS_BORDER;
end;
destructor TwdHintWnd.Destroy;
begin
FWndBmp.Free;
FHintBmp.Free;
inherited;
end;
procedure TwdHintWnd.GetDesktopImg(Bmp: TBitmap; R: TRect);
var
C: TCanvas;
begin
C:= TCanvas.Create;
try
C.Handle := GetDC(0);
Bmp.Canvas.CopyRect(Rect(0, 0, Bmp.Width, Bmp.Height), C, R);
finally
C.Free;
end;
end;
procedure TwdHintWnd.EffectHandle(WndBmp, HintBmp: TBitmap);
var
R: TRect;
i, j: Integer;

P: PByteArray;
Transt, TranstAngle: Integer;
begin
R := Rect(0, 0, WndBmp.Width - 4, WndBmp.Height - 4);
Frame3D(WndBmp.Canvas, R, clMedGray, clBtnShadow, 1);
//作窗口底下的阴影效果
Transt := 60;
for j:= WndBmp.Height - 4 to WndBmp.Height - 1 do
begin
P := WndBmp.ScanLine[j];
TranstAngle := Transt;
for i:= 3 to WndBmp.Width - 1 do
begin
//如果正处于右下角
if i > WndBmp.Width - 5 then
begin
P[3*i] := P[3*i] * TranstAngle div 100;
P[3*i + 1] := P[3*i + 1] * TranstAngle div 100;
P[3*i + 2] := P[3*i + 2] * TranstAngle div 100;
TranstAngle := TranstAngle + 10;
if TranstAngle > 90 then TranstAngle := 90;
end
else begin
P[3*i] := P[3*i] * Transt div 100;
P[3*i + 1] := P[3*i + 1] * Transt div 100;
P[3*i + 2] := P[3*i + 2] * Transt div 100;

end;
end;
Transt := Transt + 10;
end;
//作窗口右边的阴影效果
for j := 3 to WndBmp.Height - 5 do
begin
P := WndBmp.ScanLine[j];
Transt := 60;
for i:= WndBmp.Width - 4 to WndBmp.Width -1 do
begin
P[3*i] := P[3*i] * Transt div 100;
P[3*i + 1] := P[3*i + 1] * Transt div 100;
P[3*i + 2] := P[3*i + 2] * Transt div 100;
Transt := Transt + 10;
end;
end;
WndBmp.Canvas.Draw(10, 10, HintBmp);
end;
procedure TwdHintWnd.NCPaint;
begin

//重载不让画边框
end;
procedure TwdHintWnd.Paint;
begin
Canvas.CopyRect(ClientRect, FWndBmp.Canvas, ClientRect);
end;
procedure TwdHintWnd.DrawHintImg(Bmp: TBitmap; AHint: string);
var
R: TRect;
begin
Bmp.Canvas.Brush.Color := Application.HintColor;
Bmp.Canvas.Pen.Color := Application.HintColor;
Bmp.Canvas.Rectangle(0, 0, Bmp.Width, Bmp.Height);
Bmp.Canvas.Font.Color := Screen.HintFont.Color;
R := Rect(0, 0, Bmp.Width, Bmp.Height);
Inc(R.Left, 2);
Inc(R.Top, 2);
DrawText(Bmp.Canvas.Handle, PChar(AHint), -1, R, DT_LEFT or DT_NOPREFIX or
DT_WORDBREAK or DrawTextBiDiModeFlagsReadingOnly);
end;
initialization
Application.ShowHint := False;
HintWindowClass := TwdHintWnd;
Application.ShowHint := True;
end.

  只需将该单元加入你的工程当中,然后运行程序,便可看到效果了,试试看,漂亮吧。
  程序中重要部分已经作了注释,这里只说明几个重要的地方,首先是initialization部分,这里将Application的ShowHint设为False,看一下VCL源码,知道Application将一个HintWindow给消毁了,而HintWindowClass定义如下:

  THintWindowClass = class of THintWindow;
  它是THintWindow的类引用,在Forms单元中它初始化为THintWindow:

  HintWindowClass: THintWindowClass = THintWindow;
  在这里我们将其替换为TwdHintWnd,最后将ShowHint设为True,Application便用HintWindowClass创建一个Hint窗口,此时创建的便是我们定制的类了,以后的提示窗口就将用我们上面的窗口来显示。
  在ActivateHint方法,我们将作效果的处理,原理是取得提示窗口在桌面上的位置对应的位图,然后画到提示窗口上,再将提示信息的位置拷贝到提示窗口中间,这样就有了透明的效果了。其次画出玻璃的边,最后在窗口右边和下边作阴影效果。
  关于阴影效果的实现,用到的是图像的Alpha技术,可以到网上找一找,这里就不多说了,只给出图像透明度的公式:

  Dst.Red = Src.Red * alpha + (1-alpha) * Dst.Red;
  Dst.Green = Src.Green * alpha + (1-alpha) * Dst.Green;
  Dst.Blue = Src.Blue * alpha + (1-alpha) * Dst.Blue;
  Alpha的值为0到1之间,为1时表示完全不透明,不过我们将用于混合的颜色为黑色,即0,所以上面代码看到的是如下的样子:

P[3*i] := P[3*i] * TranstAngle div 100;
  玻璃提示窗口的原理大概如此,当然其透明效果是一个假象,遇到后有动的物体就暴露无疑了。不过作为一个提示窗口,我想已经足够了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值