不過這之前,我們必須介紹一個如何定制,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;
玻璃提示窗口的原理大概如此,當然其透明效果是一個假象,遇到後有動的物體就暴露無疑了。不過作為一個提示窗口,我想已經足夠了。