BigHint.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:5k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit BigHint;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  5.   Dialogs, CoolTrayIcon, TextTrayIcon, Menus, ExtCtrls, StdCtrls;
  6. type
  7.   TTiledHintWindow = class(THintWindow)
  8.   private
  9.     Bmp: TBitmap;
  10.     procedure TileImage(Bitmap: TBitmap; R: TRect);
  11.   protected
  12.     procedure Paint; override;
  13.   public
  14.     constructor Create(AOwner: TComponent); override;
  15.     destructor Destroy; override;
  16.   end;
  17.   TForm1 = class(TForm)
  18.     TextTrayIcon1: TTextTrayIcon;
  19.     PopupMenu1: TPopupMenu;
  20.     Regular1: TMenuItem;
  21.     Custom1: TMenuItem;
  22.     N1: TMenuItem;
  23.     Exit1: TMenuItem;
  24.     Timer1: TTimer;
  25.     Timer2: TTimer;
  26.     Label1: TLabel;
  27.     Button1: TButton;
  28.     Label2: TLabel;
  29.     procedure FormCreate(Sender: TObject);
  30.     procedure FormDestroy(Sender: TObject);
  31.     procedure Timer1Timer(Sender: TObject);
  32.     procedure Timer2Timer(Sender: TObject);
  33.     procedure TextTrayIcon1MouseExit(Sender: TObject);
  34.     procedure TextTrayIcon1MouseEnter(Sender: TObject);
  35.     procedure Exit1Click(Sender: TObject);
  36.     procedure Button1Click(Sender: TObject);
  37.     procedure TextTrayIcon1MouseMove(Sender: TObject; Shift: TShiftState;
  38.       X, Y: Integer);
  39.     procedure Regular1Click(Sender: TObject);
  40.     procedure Custom1Click(Sender: TObject);
  41.   private
  42.     HintWindow1: THintWindow;
  43.     HintWindow2: TTiledHintWindow;
  44.     CurrentHintWindow: THintWindow;
  45.     LastMouse, LastHint: TPoint;
  46.     Hint: string;
  47.   end;
  48. var
  49.   Form1: TForm1;
  50. implementation
  51. {$R *.dfm}
  52. {$R 'images.res'}
  53. {------------------------ TForm1 ----------------------}
  54. procedure TForm1.FormCreate(Sender: TObject);
  55. var
  56.   I: Integer;
  57. begin
  58.   HintWindow1 := THintWindow.Create(Self);
  59.   HintWindow1.Color := clAqua;
  60.   HintWindow1.Canvas.Font.Style := [fsBold];
  61.   HintWindow1.Canvas.Font.Size := 10;
  62.   HintWindow2 := TTiledHintWindow.Create(Self);
  63.   HintWindow2.Canvas.Font.Color := clWhite;
  64.   Timer1.Interval := Application.HintPause;
  65.   Timer2.Interval := Application.HintHidePause;
  66.   Hint := Hint + 'This is a BIG hint!'+#13;
  67.   for I := 1 to 30 do
  68.   begin
  69.     Hint := Hint + 'abc - 0123456789 - 0123456789 - 0123456789 - 0123456789 - def';
  70.     if I <> 30 then
  71.       Hint := Hint + #13;
  72.   end;
  73.   Regular1Click(Self);
  74. end;
  75. procedure TForm1.FormDestroy(Sender: TObject);
  76. begin
  77.   HintWindow1.Free;
  78.   HintWindow2.Free;
  79. end;
  80. procedure TForm1.TextTrayIcon1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  81. begin
  82.   LastMouse.X := X;
  83.   LastMouse.Y := Y;
  84. end;
  85. procedure TForm1.Timer1Timer(Sender: TObject);
  86. var
  87.   HintRect: TRect;
  88. begin
  89.   Timer1.Enabled := False;
  90.   if (LastHint.X <> LastMouse.X) or (LastHint.Y <> LastMouse.Y) then
  91.   begin
  92.     if not Timer2.Enabled then
  93.     begin
  94.       HintRect := CurrentHintWindow.CalcHintRect(Screen.Width, Hint, nil);
  95.       CurrentHintWindow.ActivateHint(Rect(LastMouse.X - HintRect.Right,
  96.                LastMouse.Y - HintRect.Bottom, LastMouse.X, LastMouse.Y), Hint);
  97.     end;
  98.     LastHint.X := LastMouse.X;
  99.     LastHint.Y := LastMouse.Y;
  100.   end;
  101.   Timer2.Enabled := true;
  102. end;
  103. procedure TForm1.Timer2Timer(Sender: TObject);
  104. begin
  105.   CurrentHintWindow.ReleaseHandle;
  106.   Timer2.Enabled := False;
  107. end;
  108. procedure TForm1.Regular1Click(Sender: TObject);
  109. begin
  110.   Regular1.Checked := True;
  111.   CurrentHintWindow := HintWindow1;
  112. end;
  113. procedure TForm1.Custom1Click(Sender: TObject);
  114. begin
  115.   Custom1.Checked := True;
  116.   CurrentHintWindow := HintWindow2;
  117. end;
  118. procedure TForm1.Exit1Click(Sender: TObject);
  119. begin
  120.   Close;
  121. end;
  122. procedure TForm1.TextTrayIcon1MouseExit(Sender: TObject);
  123. begin
  124.   CurrentHintWindow.ReleaseHandle;
  125.   Timer1.Enabled := False;
  126.   Timer2.Enabled := False;
  127.   Timer1.Interval := Application.HintPause;
  128.   Timer2.Interval := 5000;        // Seems to be the time a tooltip is open
  129. end;
  130. procedure TForm1.TextTrayIcon1MouseEnter(Sender: TObject);
  131. begin
  132.   if not Timer1.Enabled then
  133.     Timer1.Enabled := True;
  134. end;
  135. procedure TForm1.Button1Click(Sender: TObject);
  136. begin
  137.   Exit1Click(Self);
  138. end;
  139. {------------------ TTiledHintWindow ------------------}
  140. constructor TTiledHintWindow.Create(AOwner: TComponent);
  141. var
  142.   H: HBITMAP;
  143. begin
  144.   inherited Create(AOwner);
  145.   Bmp := TBitmap.Create;
  146.   H := LoadBitmap(HINSTANCE, 'BACKGROUND');
  147.   Bmp.Handle := H;
  148. end;
  149. destructor TTiledHintWindow.Destroy;
  150. begin
  151.   Bmp.Free;
  152.   inherited Destroy;
  153. end;
  154. procedure TTiledHintWindow.Paint;
  155. var
  156.   R: TRect;
  157. begin
  158.   R := ClientRect;
  159.   Inc(R.Left, 2);
  160.   Inc(R.Top, 2);
  161.   TileImage(Bmp, R);
  162.   DrawText(Canvas.Handle, PChar(Caption), -1, R, DT_LEFT or DT_NOPREFIX or
  163.            DT_WORDBREAK or DrawTextBiDiModeFlagsReadingOnly);
  164. end;
  165. procedure TTiledHintWindow.TileImage(Bitmap: TBitmap; R: TRect);
  166. var
  167.   X, Y: Integer;
  168. begin
  169.   try
  170.     for X := 0 to (R.Right-R.Left) div Bitmap.Width do
  171.       for Y := 0 to (R.Bottom-R.Top) div Bitmap.Height do
  172.         Canvas.Draw(X * Bitmap.Width, Y * Bitmap.Height, Bitmap);
  173.   finally
  174.   end;
  175. end;
  176. end.