{
修改者:ghs
日期:20071218
功能:在原版本的基础上增加。
RegisterControl:注册需要提示的控件。
BeginHelp:设置光标状态为帮助crHelp;
鼠标弹起后,显示注册的提示信息,同时光标进行还原。 原版本
作者:thaoqi
出处:http://www.2ccc.com/article.asp?articleid=4389
功能:首先谢谢xsherry大大,来盒子很长一段时间了,老是下东西,没有为盒子做什么贡献。
前段时间xsherry大大抛砖引玉的文章,给我启发很大,最近一个项目提出要求人
机交互界面更加有好,尽量少用MessageBox,所以在他的基础上,我试图模仿XP
登录时候的那个ToolTip提示功能,用API摸索出一个符合要求的ToolTip提示框出
来,最后我把实现的函数封装成了一个VCL的控件,希望大家能多提意见!
}
unit TooltipUtil; interface uses Messages, Windows, SysUtils, Classes, Contnrs, Controls, CommCtrl,
StdCtrls, ExtCtrls, Consts, Forms, Dialogs, AppEvnts; type
TTipTool_ICON = (ttNoneIcon, ttInformationIcon, ttWarningIcon, ttStopIcon);
TTipAlignment = (taLeft, taCenter, taRight); PTipInfo = ^TTipInfo; TTipInfo = packed record
WinControl: TWinControl;
Handle: THandle;
Caption: string;
Msg: string;
TipICON: TTipTool_ICON;
TipAlg: TTipAlignment;
Cursor: TCursor;
end; TToolTip = class(TComponent)
private
fTitle: string;
fText: string;
fEnabled: Boolean;
fWindowHandle: HWND;
fTipHandle: HWND;
fInterval: Cardinal;
fToolInfo: TToolInfo;
fAlignment: TTipAlignment;
fTipIcon: TTipTool_ICON;
fControl: TWinControl;
//
Flist: TList;
ApplicationEvents: TApplicationEvents;
FLastHandle: THandle; procedure SetText(AText: string); //设置气泡提示信息
procedure SetTitle(ATitle: string); //设置气泡提示的标题 procedure UpdateTime; //更新计时器状态
procedure WndProc(var Msg: TMessage); //接收windows消息
protected
//拦截消息=处理左键弹起
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
//结束帮助=设置光标为控件本来状态
procedure EndHelp;
public
constructor Create(AOwner: TComponent); override; //构造函数,创建实例
destructor Destroy; override; //析构函数,销毁实例
//注册控件信息
procedure RegisterControl(WinControl: TWinControl; aCaption, aMsg: string;
TipICON: TTipTool_ICON = ttInformationIcon; TipAlignment: TTipAlignment = taLeft);
//开始帮助=设置光标状态
procedure BeginHelp;
procedure Popup(Handle: HWND); overload; //在指定的句柄中弹出气泡(重载)
procedure Popup(Handle: HWND; IconType: TTipTool_ICON; Title,
Text: string); overload; //在指定的句柄中弹出气泡(重载) published
//气泡窗体的窗体句柄
property Handle: HWND read fTipHandle;
//气泡窗体的提示信息
property Text: string read fText write SetText;
//气泡窗体的标题信息
property Title: string read fTitle write SetTitle;
//气泡窗体的信息图标
property ICON: TTipTool_ICON read fTipIcon write fTipIcon;
//气泡窗体弹出时对齐位置
property Alignment: TTipAlignment read fAlignment write fAlignment default taLeft;
//气泡窗体的显示时间
property Interval: Cardinal read fInterval write fInterval default ;
end; procedure Register; implementation const
TTS_BALLOON = $; //ToolTip提示窗口的外形,指定为气球型
TTS_CLOSE = $; //关闭按钮
TTF_PARSELINKS = $; //可使用超链接
TTM_SETTITLE = WM_USER + ; //社这提示标题信息的消息 constructor TToolTip.Create(AOwner: TComponent);
begin
inherited Create(AOwner); if not (AOwner is TWinControl) then
begin
raise exception.Create('TToolTip''s owner must be a ''TWinControl'' type.');
Destroy;
end; fWindowHandle := Classes.AllocateHWnd(WndProc); fEnabled := False;
fInterval := ; //创建气泡提示窗口
fTipHandle := CreateWindow(TOOLTIPS_CLASS, nil,
WS_POPUP or TTS_NOPREFIX or
TTS_BALLOON or TTS_ALWAYSTIP, // or TTS_CLOSE,
, , , , fWindowHandle,
, HInstance, nil); if fTipHandle <> then
begin
//设置ToolInfo的大小
fToolInfo.cbSize := SizeOf(fToolInfo);
//设置基本风格
fToolInfo.uFlags := TTF_PARSELINKS or TTF_IDISHWND or TTF_TRACK;
//设置所有者的句柄
fToolInfo.uId := fWindowHandle;
end;
Flist := TList.Create;
ApplicationEvents := TApplicationEvents.Create(nil);
ApplicationEvents.OnMessage := ApplicationEvents1Message;
end; destructor TToolTip.Destroy;
var
I: Integer;
tmpTipInfo: PTipInfo;
begin
if fTipHandle <> then
CloseWindow(fTipHandle);
for I := Flist.Count - downto do // Iterate
begin
tmpTipInfo := PTipInfo(FList.Items[i]);
Dispose(tmpTipInfo);
end; // for
Flist.Free;
ApplicationEvents.Free;
inherited Destroy;
end; procedure TToolTip.SetText(AText: string);
begin
fText := AText; if fTipHandle <> then
begin
//设置标题信息
fToolInfo.lpszText := PAnsiChar(fText);
//向气泡窗体发送消息,将ToolInfo的信息设置到气泡窗体中
SendMessage(fTipHandle, TTM_ADDTOOL, , Integer(@fToolInfo));
SendMessage(fTipHandle, TTM_SETTOOLINFO, , Integer(@fToolInfo));
end;
end; procedure TToolTip.SetTitle(ATitle: string);
begin
fTitle := ATitle; if fTipHandle <> then
//设置气泡窗体的提示图标和标题信息
SendMessage(fTipHandle, TTM_SETTITLE, Integer(fTipIcon), Integer(fTitle));
end; procedure TToolTip.Popup(Handle: HWND);
var
tmpRect: TRect;
x, y: word;
begin
x := ; fControl := FindControl(Handle);
if fControl.Hint <> '' then
fControl.ShowHint := False; //得到需要显示窗体所在的屏幕区域
GetWindowRect(Handle, tmpRect); //计算显示区域位置的坐标
with tmpRect do
begin
y := (Bottom - Top) div + Top; case fAlignment of
taLeft: x := Left;
taCenter: x := (Right - Left) div + Left;
taRight: x := Right;
end;
end; //设置气泡窗体弹出的坐标
SendMessage(fTipHandle, TTM_TRACKPOSITION, , MAKELONG(x, y));
//激活气泡窗体,并显示出来
SendMessage(fTipHandle, TTM_TRACKACTIVATE, Integer(True), Integer(@fToolInfo)); fEnabled := True;
//更新计时器状态
UpdateTime;
end; procedure TToolTip.WndProc(var Msg: TMessage);
begin
fEnabled := False; with Msg do
begin
case Msg of
WM_TIMER:
try
SendMessage(fTipHandle, TTM_TRACKACTIVATE,
Integer(False), Integer(@fToolInfo));
if fControl.Hint <> '' then
fControl.ShowHint := True;
except
Application.HandleException(Self);
end;
else
Result := DefWindowProc(fWindowHandle, Msg, wParam, lParam);
end;
end;
//更新计时器状态
UpdateTime;
end; procedure TToolTip.Popup(Handle: HWND; IconType: TTipTool_ICON;
Title: string; Text: string);
begin
fTipIcon := IconType; SetTitle(Title);
SetText(Text); Popup(Handle);
end; procedure TToolTip.UpdateTime;
begin
KillTimer(fWindowHandle, );
if (FInterval <> ) and FEnabled then
if SetTimer(fWindowHandle, , FInterval, nil) = then
raise EOutOfResources.Create(SNoTimers);
end; procedure Register;
begin
RegisterComponents('ToolTip', [TToolTip]);
end; procedure TToolTip.RegisterControl(WinControl: TWinControl; aCaption, aMsg: string;
TipICON: TTipTool_ICON = ttInformationIcon; TipAlignment: TTipAlignment = taLeft);
var
TipInfo: PTipInfo;
begin
New(TipInfo);
TipInfo.WinControl := WinControl;
TipInfo.Handle := WinControl.Handle;
TipInfo.Caption := aCaption;
Tipinfo.Msg := aMsg;
TipInfo.TipICON := TipICON;
TIpInfo.TipAlg := TipAlignment;
TipInfo.Cursor := WinControl.Cursor; Flist.Add(TipInfo);
end; procedure TToolTip.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
var
I: Integer;
tmpTipInfo: PTipInfo;
tmpPoint: TPoint;
tmpHandle: THandle;
begin
if Msg.message = WM_LBUTTONUP then
begin
GetCurSorPos(tmpPoint);
tmpHandle := WindowFromPoint(tmpPoint);
if FLastHandle <> tmpHandle then //防止不停触发
begin
FLastHandle := tmpHandle;
for I := to FList.Count - do // Iterate
begin
tmpTipInfo := PTipInfo(FList.Items[i]);
//只有调用了BeginHelp,才会弹出提示窗口
if (tmpTipInfo.Handle = tmpHandle) and (tmpTipInfo.WinControl.Cursor = crHelp) then
begin
Popup(tmpHandle, tmpTipInfo.TipICON, tmpTipInfo.Caption, tmpTipInfo.Msg);
break;
end;
end; // for
EndHelp;
DefWindowProc(Msg.hwnd, Msg.message, Msg.wParam, Msg.lParam);
end;
end; end; procedure TToolTip.BeginHelp;
var
i: Integer;
tmpTipInfo: PTipInfo;
begin
for I := to FList.Count - do // Iterate
begin
tmpTipInfo := PTipInfo(FList.Items[i]);
tmpTipInfo.WinControl.Cursor := crHelp;
end; // for
end; procedure TToolTip.EndHelp;
var
i: Integer;
tmpTipInfo: PTipInfo;
begin
for I := to FList.Count - do // Iterate
begin
tmpTipInfo := PTipInfo(FList.Items[i]);
tmpTipInfo.WinControl.Cursor := tmpTipInfo.Cursor;
end; // for
end; end. 调用一: if edt3.Text='' then
begin
tltp1.Popup(TWinControl(edt3).Handle, ttStopIcon,'提示','请输入产地');
Exit;
end; 调用二: ToolTip1.RegisterControl(LabeledEdit1, '提示', '请输入用户名');
ToolTip1.BeginHelp;

定时显示提示信息(TToolTip)的更多相关文章

  1. 基于JQuery的浮动DIV显示提示信息并自动隐藏

    /** * 浮动DIV定时显示提示信息,如操作成功, 失败等 * @param string tips (提示的内容) * @param int height 显示的信息距离浏览器顶部的高度 * @p ...

  2. JQuery 浮动DIV显示提示信息并自动隐藏

    /** * 浮动DIV定时显示提示信息,如操作成功, 失败等 * @param string tips (提示的内容) * @param int height 显示的信息距离浏览器顶部的高度 * @p ...

  3. div显示提示信息

    div显示提示信息 <body> <style type="text/css"> a.link{position:relative;} a.link div ...

  4. MFC 学习之 鼠标移动到Toolbar按钮上显示提示信息(tooltip),状态栏也随之改变

    1.在ResourceView里加入Toolbar资源,命名IDR_TOOLBAR1 2.在主程序的.h文件中加入变量:           CToolBar m_toolbar;CImageList ...

  5. js定时显示广告代码

    这是一则定时显示广告的javascript代码,当然也可以定时显示某一区块内容,很实用. 代码如下: function strToDate(str, ext) { if (str == null || ...

  6. VC/MFC 当鼠标移到控件上时显示提示信息

    VC/MFC 当鼠标移到控件上时显示提示信息 ToolTip是Win32中一个通用控件,MFC中为其生成了一个类CToolTipCtrl,总的说来其使用方法是较简单的,下面讲一下它的一般用法和高级用法 ...

  7. 重写TextBox实现显示提示信息

    /// <summary> /// TextBox提示信息 /// </summary> /// <author>Tim_et</author> /// ...

  8. div显示提示信息【转】

    div显示提示信息 <body> <style type="text/css"> a.link{position:relative;} a.link div ...

  9. ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等--绘制点、线、圆,显示提示信息

    ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等--绘制点.线.圆,显示提示信息 /// <summary> /// 绘制界面上的点和线 ///  ...

随机推荐

  1. (转载) 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)

    这一篇是从0开始搭建SQL Server AlwaysOn 的第四篇,这一篇开始搭建异地机房节点 注意点1 注意异地节点最好至少有2个AG节点,否则在本地节点进行手动故障转移的时候会出现仲裁警告,提示 ...

  2. learning java AWT 布局管理器CardLayout

    import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; public class CardLayo ...

  3. PHP 鸟哥:我也曾经是“不适合”编程的人

    网名:雪候鸟,大家尊称鸟哥,惠新宸 @Laruence, 是国内最有影响力的 PHP 技术专家,PHP 开发组核心成员,PECL 开发者,Zend 公司外聘顾问.他曾供职于雅虎,百度,现在新浪微博任平 ...

  4. Js验证正则表达式

    //验证是否手机 var base = Objcet();base.isPhone = function(num) { var preg = /^1[3-7,8]{1}[0-9]{9}$/; retu ...

  5. 细说 call、apply 以及 bind 的区别和用法

    call 和 apply 的共同点 它们的共同点是,都能够改变函数执行时的上下文,将一个对象的方法交给另一个对象来执行,并且是立即执行的. 为何要改变执行上下文?举一个生活中的小例子:平时没时间做饭的 ...

  6. redis系列(五):搭建redis-cluster集群

    1.为什么要用redis-cluster a.并发要求 redis官方声称可以达到10万每秒,但是如果业务需要每秒100万条呢?b.数据量太大 一台服务器的内存正常是16-256G,如果业务需要500 ...

  7. CODE FESTIVAL 2016 qual A题解

    传送门 不知道为什么\(AGC\)系列的题里突然多了这些--那就做吧-- \(A\) 什么玩意儿-- upd:因为没看到最后要加换行居然没有\(1A\)好气哦-- const int N=15; ch ...

  8. 安装pdo_dblib扩展连接SQLserver

    1.先得安装freetdswget ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.1.5.tar.gztar zxvf freetds-1.1. ...

  9. 谈下python的GIL

    GIL 是python的全局解释器锁,同一进程中假如有多个线程运行,一个线程在运行python程序的时候会霸占python解释器(加了一把锁即GIL),使该进程内的其他线程无法运行,等该线程运行完后其 ...

  10. [代码审计]php弱类型总结

    0x01 前言 php是世界上最好的语言,所以php自身的安全问题也是web安全的一个方面.由于其自身弱类型语言的特性以及内置函数对于传入参数的松散处理,所以会带来很多的问题,这里将进行简要介绍. 弱 ...