首页  编辑  

THintWindow 这么用

Tags: /超级猛料/VCL/Form,窗体/   Date Created:

THintWindow 的 ActivateHint 和 ActivateHintData 怎么用?

type

 TGraphicHintWindow = class(THintWindow)

   constructor Create(AOwner: TComponent); override;

 private

   FActivating: Boolean;

 public

   procedure ActivateHint(Rect: TRect; const AHint: string); override;

 protected

   procedure Paint; override;

 published

   property Caption;

 end;

 {...}

constructor TGraphicHintWindow.Create(AOwner: TComponent);

begin

 inherited Create(AOwner);

 {

  Hier kbeliebige Schrift Eigenschaften gesetzt

  werden.

  Here you can set custom Font Properties:

  }

 with Canvas.Font do

 begin

   Name := 'Arial';

   Style := Style + [fsBold];

   Color := clBlack;

 end;

end;

procedure TGraphicHintWindow.Paint;

var

 R: TRect;

 bmp: TBitmap;

begin

 R := ClientRect;

 Inc(R.Left, 2);

 Inc(R.Top, 2);

 {*******************************************************

  Der folgende Code ist ein Beispiel wie man die Paint

  Prozedur nutzen kann um einen benutzerdefinierten Hint

  zu erzeugen.

  The folowing Code ist an example how to create a custom

  Hint Object. :

  }

 bmp := TBitmap.Create;

 bmp.LoadfromFile('D:\hint.bmp');

 with Canvas do

 begin

   Brush.Style := bsSolid;

   Brush.Color := clsilver;

   Pen.Color   := clgray;

   Rectangle(0, 0, 18, R.Bottom + 1);

   Draw(2,(R.Bottom div 2) - (bmp.Height div 2), bmp);

 end;

 bmp.Free;

 //Beliebige HintFarbe

 //custom Hint Color

 Color := clWhite;

 Canvas.Brush.Style := bsClear;

 Canvas.TextOut(20, (R.Bottom div 2) - (Canvas.Textheight(Caption) div 2), Caption);

 {********************************************************}

end;

procedure TGraphicHintWindow.ActivateHint(Rect: TRect; const AHint: string);

begin

 FActivating := True;

 try

   Caption := AHint;

   //Hdes Hints setzen setzen

   //Set the "Height" Property of the Hint

   Inc(Rect.Bottom, 14);

   //Breite des Hints setzen

   //Set the "Width" Property of the Hint

   Rect.Right := Rect.Right + 20;

   UpdateBoundsRect(Rect);

   if Rect.Top + Height > Screen.DesktopHeight then

     Rect.Top := Screen.DesktopHeight - Height;

   if Rect.Left + Width > Screen.DesktopWidth then

     Rect.Left := Screen.DesktopWidth - Width;

   if Rect.Left < Screen.DesktopLeft then Rect.Left := Screen.DesktopLeft;

   if Rect.Bottom < Screen.DesktopTop then Rect.Bottom := Screen.DesktopTop;

   SetWindowPos(Handle, HWND_TOPMOST, Rect.Left, Rect.Top, Width, Height,

     SWP_SHOWWINDOW or SWP_NOACTIVATE);

   Invalidate;

 finally

   FActivating := False;

 end;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

 HintWindowClass := TGraphicHintWindow;

 Application.ShowHint := False;

 Application.ShowHint := True;

end;

********************************************************************

procedure ActivateHintData(Rect: TRect; const AHint: string; AData: Pointer); virtual;

AData 指的是什么?

能否给个具体例子?

多谢!

var

 h : THintWindow;

 r : TRect;

begin

 with r do

 begin

   // set the position and size

   // of the hint window

   left   :=  10;

   top    :=  50;

   right  := 200;

   bottom := 100;

 end;

 h := THintWindow.Create( Self );

 with h do

 begin

   // set the background color

   Color := clRed;

   ActivateHint( r, 'hi there!' );

   // perform your tasks here

   // before closing the hint window

   MessageBox( 0,'Press any key to close the ' + 'hint window',

              'THintWindow', MB_OK );

   ReleaseHandle;

   Free;

 end;

end;

--------------------------------------------------------------------------------

来自:笑傲江湖 时间:01-1-20 8:57:10 ID:443232  

AData 指的是什么?

能否给个具体例子?

----AData是一个(关联的)指针,

   目前来说, ActivateHintData和ActivateHint等效, 因为AData参数被忽略.

   在THintWindow类中, ActivateHintData被定义成Virtual的,

   后代类可以重载这个方法, 把AHint 和AData所关联的信息

   重新组合成自己需要的HINT方式.

   以下是THintWindow的源码部分:  

procedure THintWindow.ActivateHint(Rect: TRect; const AHint: string);

begin

 FActivating := True;

 try

   Caption := AHint;

   Inc(Rect.Bottom, 4);

   UpdateBoundsRect(Rect);

   if Rect.Top + Height > Screen.DesktopHeight then

     Rect.Top := Screen.DesktopHeight - Height;

   if Rect.Left + Width > Screen.DesktopWidth then

     Rect.Left := Screen.DesktopWidth - Width;

   if Rect.Left < Screen.DesktopLeft then Rect.Left := Screen.DesktopLeft;

   if Rect.Bottom < Screen.DesktopTop then Rect.Bottom := Screen.DesktopTop;

   SetWindowPos(Handle, HWND_TOPMOST, Rect.Left, Rect.Top, Width, Height,

     SWP_SHOWWINDOW or SWP_NOACTIVATE);

   Invalidate;

 finally

   FActivating := False;

 end;

end;

procedure THintWindow.ActivateHintData(Rect: TRect; const AHint: string; AData: Pointer);

begin

 ActivateHint(Rect, AHint);//AData参数被忽略, 你可以传个nil, 或任意一个指针

end;

Application.HintHidePause:=99999999; //防止闪烁

   getcursorpos(cursorpos);

   LvwGrp.Hint:='XXX';

   application.activatehint(cursorpos);

img_27733.bmp (106.0KB)