unit wdHintWnd2;
interface
uses
Windows, Classes, Controls, Graphics, Forms, SysUtils, ExtCtrls;
type
TwdHintWnd = class(THintWindow)
private
FHintBmp: TBitmap; //提示信息位图
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure Paint; override;
procedure NCPaint(DC: HDC); override;
{画提示的图象}
procedure DrawHintImg(Bmp: TBitmap; AHint: string);
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);
Inc(Rect.Right, 23);
Inc(Rect.Bottom, 27);}
if length(AHint) > 100 then //如果 AHint地长度大于100,则将AHint显示在400*300的矩形框中!!!
begin
Rect.Right := Rect.Left + 386;
Rect.Bottom := Rect.Top + 168;
end;
FHintBmp.Width := Rect.Right - Rect.Left;
FHintBmp.Height := Rect.Bottom - Rect.Top;
DrawHintImg(FHintBmp, AHint);
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;
Frame3D(FHintBmp.Canvas, Rect, clMedGray, clBtnShadow, 1);
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;
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
FHintBmp.Free;
inherited;
end;
procedure TwdHintWnd.NCPaint;
begin
//重载不让画边框
end;
procedure TwdHintWnd.Paint;
begin
Canvas.CopyRect(ClientRect, FHintBmp.Canvas, ClientRect);
end;
procedure TwdHintWnd.DrawHintImg(Bmp: TBitmap; AHint: string);
var
R: TRect;
slLine: TStrings;
iLineNo, iLineH: Integer;
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;
//Bmp.Canvas.Font.
R := Rect(0, 0, Bmp.Width, Bmp.Height);
Inc(R.Left, 2);
Inc(R.Top, 2);
slLine := TStringList.Create;
slLine.Text := StringReplace(AHint, #$0A#$0D, #$0D#$0A, [rfReplaceAll]);
iLineH := 0;
for iLineNo := 0 to slLine.Count - 1 do
begin
if (iLineNo mod 3) = 0 then Bmp.Canvas.Font.Style := [fsBold]; //标题
if (iLineNo mod 3) = 1 then Bmp.Canvas.Font.Style := []; //正文
if (iLineNo mod 3) = 2 then ; //分段(换行符)
//DrawText(Bmp.Canvas.Handle, PChar(AHint), -1, R, DT_LEFT or DT_NOPREFIX or DT_WORDBREAK or DrawTextBiDiModeFlagsReadingOnly);
R.Top := R.Top + iLineH;
iLineH := DrawText(Bmp.Canvas.Handle, PChar(slLine.Strings[iLineNo]), -1, R, DT_LEFT or DT_NOPREFIX or DT_WORDBREAK or DrawTextBiDiModeFlagsReadingOnly);
if iLineH < 10 then iLineH := 13;
end;
slLine.Free;
end;
initialization
Application.ShowHint := False;
HintWindowClass := TwdHintWnd; //THintWindow
Application.ShowHint := True;
Application.HintHidePause := -1;
end.