控件加提示

来源:互联网 发布:淘宝装修在线辅助工具 编辑:程序博客网 时间:2024/06/18 07:28
你可以利用THintWindow,创建一个自己的Hint。

一、窗口的Type 加入  THintControl = class(TComponent)
二、窗口的implementation加入THintControl的实现部分 
三、Buuton等控件的MouseUp或Click事件中加入相应代码
        此处,可以重新设置显示长度和背景色,体现个性化。

具体代码如下:
Delphi/Pascal code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;
 
type
  THintControl = class(TComponent)
  private
    FHint: string;
    HintWindow: THintWindow;
    HintTimer: TTimer;
    FComponent: TControl;
  protected
    procedure HideHint(Sender:TObject); virtual;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Hint: string read FHint write FHint;
    property Component: TControl read FComponent write FComponent;
    procedure ShowHint(AHint: string); overload;
    procedure ShowHint(AHint: string; AComponent: TControl); overload;
  end;
 
  TForm1 = class(TForm)
    Button3: TButton;
    Edit2: TEdit;
    Memo1: TMemo;
    GroupBox1: TGroupBox;
    procedure Button3Click(Sender: TObject);
    procedure Memo1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure GroupBox1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Edit2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
{$R *.dfm}
 
procedure THintControl.ShowHint(AHint: string);
begin
  ShowHint(AHint, FComponent);
end;
  
constructor THintControl.Create(AOwner: TComponent);
begin
  inherited;
  HintWindow := HintWindowClass.Create(nil);
  HintWindow.Color := Application.HintColor;
  HintTimer:=TTimer.Create(nil);
  HintTimer.OnTimer:=HideHint;
  HintTimer.Interval:=Application.HintHidePause;    
end;
  
destructor THintControl.Destroy;
begin
  HintTimer.Free;
  HintWindow.Free;
  inherited;
end;
  
procedure THintControl.HideHint(Sender: TObject);
begin
  TTimer(Sender).Enabled:=false;
  ShowWindow(HintWindow.Handle,SW_HIDE);
end;
  
procedure THintControl.ShowHint(AHint: string; AComponent: TControl);
var
  vPoint:TPoint;
  vRect:TRect;
begin
  if AHint<>'' then
  begin
    if not Windows.IsWindowVisible(HintWindow.Handle) or (HintWindow.Caption<>AHint) then
    begin
      vRect:=HintWindow.CalcHintRect(Screen.Width,AHint,nil);
      vPoint.X := AComponent.Left+3;
      vPoint.Y := AComponent.Top + AComponent.Height +3;
      vPoint := AComponent.Parent.ClientToScreen(vPoint);
      Inc(vRect.Left,vPoint.X);
      Inc(vRect.Right,vPoint.X);
      Inc(vRect.Top,vPoint.Y);
      Inc(vRect.Bottom,vPoint.Y);
      HintWindow.ActivateHint(vRect,AHint);
      HintTimer.Enabled:=true;
    end;
  end
  else
    ShowWindow(HintWindow.Handle,SW_HIDE);
end;
 
procedure TForm1.Button3Click(Sender: TObject);
var
  vHint: THintControl;
begin
  vHint := THintControl.Create(self);
  try
    vHint.Component := Button3;
    vHint.ShowHint('Button的Hint,3秒,保存数据! ');
    vHint.HintTimer.Interval:=3000;
  finally
  end;
end;
 
procedure TForm1.Memo1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  vHint: THintControl;
begin
  vHint := THintControl.Create(self);
  try
    vHint.Component := Memo1;
    vHint.ShowHint('Memo的Hint, 6秒,文本数据的显示!');
    vHint.HintTimer.Interval:=6000;
  finally
  end;
end;
 
procedure TForm1.GroupBox1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  vHint: THintControl;
begin
  vHint := THintControl.Create(self);
  try
    vHint.Component := GroupBox1;
    vHint.ShowHint('GroupBox的Hint,8秒 '+#10#13+'选择级别,实现分类操作!');
    vHint.HintTimer.Interval:=8000;
  finally
  end;
end;
 
procedure TForm1.Edit2Click(Sender: TObject);
var vHint: THintControl;
begin
  vHint := THintControl.Create(self);
  try
    vHint.Component := Edit2;
    vHint.ShowHint('Edit的Hint,5称红色 '+#10#13+'录入相关信息! ');
    vHint.HintTimer.Interval:=5000;
    vHint.HintWindow.Color:=clRed;
  finally
  end;
end;
 
end.

运行效果:
原创粉丝点击