如何做出先 金山词霸 一样的 提示窗? ( 积分: 50 )

  • 主题发起人 主题发起人 lt66
  • 开始时间 开始时间
我想应该是在获得鼠标位置后在该位置显示一个窗口,该窗口的宽度和高度根据显示的内容动态调整。鼠标离开后关闭窗口。
 
是啊, 但 如何做 呢?
 
用 hint不行吗
 
hint不能做按钮啊。金山词霸上有按钮。<br>窗口关闭:onmouseleave<br>窗口宽度调整: 应该可能根据一个autosize的label控件的宽度在oncreate重设窗口宽度。<br>窗口位置:getcusorpos获得光标坐标。如果是鼠标可以通过onmousemove来获得(x,y)坐标。
 
纠正一下楼上的,GetCursorPos获取的是鼠标在屏幕的绝对坐标而不是光标坐标,OnMouseMove获得的鼠标坐标只是相对于窗体的坐标,如果用来作为另一个窗体显示定位的坐标的话还必须加上当前窗体坐标的绝对坐标值(left和top),并且只有在当前窗体获得焦点的时候OnMouseLeave/OnMouseMove事件才能触发,所以通过OnMouseMove事件实现以上功能并不是很合适。
 
我简单写了个例子,基本能够实现:<br><br>工程文件<br>-----------------------------------------------------<br>program Project1;<br><br>uses<br> &nbsp;Forms,<br> &nbsp;Unit1 in 'Unit1.pas' {Form1},<br> &nbsp;Unit2 in 'Unit2.pas' {Form2};<br><br>{$R *.RES}<br><br>begin<br> &nbsp;Application.Initialize;<br> &nbsp;Application.CreateForm(TForm1, Form1);<br> &nbsp;Application.CreateForm(TForm2, Form2);<br> &nbsp;Application.Run;<br>end.<br><br><br>主窗体:*.pas<br>--------------------------------------------------<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;ExtCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Timer1: TTimer;<br> &nbsp; &nbsp;procedure FormMouseDown(Sender: TObject; Button: TMouseButton;<br> &nbsp; &nbsp; &nbsp;Shift: TShiftState; X, Y: Integer);<br> &nbsp; &nbsp;procedure Timer1Timer(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br> &nbsp;PShow:TPoint;<br><br>implementation<br><br>uses Unit2;<br><br>{$R *.DFM}<br><br>procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;<br> &nbsp;Shift: TShiftState; X, Y: Integer);<br>begin &nbsp;//点击鼠标显示提示窗体<br> &nbsp;//获取当前鼠标坐标<br> &nbsp;GetCursorPos(PShow);<br><br> &nbsp;//定位显示提示窗体<br> &nbsp;Form2.Left:=PShow.X+5; &nbsp;//这里取偏移5显示<br> &nbsp;Form2.Top:=PShow.Y+5; &nbsp; //这里取偏移5显示<br> &nbsp;Form2.Show;<br><br> &nbsp;//启动延时检测是否该关闭提示窗体<br> &nbsp;Timer1.Enabled:=false;<br> &nbsp;Timer1.Interval:=1000; &nbsp; //这里取1秒定时周期<br> &nbsp;Timer1.Enabled:=true;<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var P:TPoint;<br>begin<br> &nbsp;if not Form2.Visible then<br> &nbsp;begin //如果提示窗体已经关闭,则终止定时<br> &nbsp; &nbsp;Timer1.Enabled:=false;<br> &nbsp; &nbsp;exit;<br> &nbsp;end;<br><br> &nbsp;//获取当前鼠标坐标<br> &nbsp;GetCursorPos(P);<br> &nbsp;if ((Abs(PShow.X-P.X)&gt;5) or (Abs(PShow.Y-P.Y)&gt;5)) then<br> &nbsp;begin &nbsp;//如果鼠标的偏移量超过某一个限度(这里取5,可以根据实际情况调整)<br> &nbsp; &nbsp;if (P.X&lt;Form2.Left) or<br> &nbsp; &nbsp; &nbsp; (P.X&gt;Form2.Left+Form2.Width) or<br> &nbsp; &nbsp; &nbsp; (P.Y&lt;Form2.Top) or<br> &nbsp; &nbsp; &nbsp; (P.Y&gt;Form2.Top+Form2.Height) then<br> &nbsp; &nbsp;begin &nbsp;//鼠标位置不在提示窗体范围内<br> &nbsp; &nbsp; &nbsp;Timer1.Enabled:=false; &nbsp;//终止定时<br> &nbsp; &nbsp; &nbsp;Form2.Close; &nbsp;//关闭提示窗体<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br><br>end.<br><br>主窗体:*.dfm<br>------------------------------------------<br>object Form1: TForm1<br> &nbsp;Left = 272<br> &nbsp;Top = 237<br> &nbsp;Width = 450<br> &nbsp;Height = 361<br> &nbsp;Caption = 'Form1'<br> &nbsp;Color = clBtnFace<br> &nbsp;Font.Charset = DEFAULT_CHARSET<br> &nbsp;Font.Color = clWindowText<br> &nbsp;Font.Height = -11<br> &nbsp;Font.Name = 'MS Sans Serif'<br> &nbsp;Font.Style = []<br> &nbsp;OldCreateOrder = False<br> &nbsp;OnMouseDown = FormMouseDown<br> &nbsp;PixelsPerInch = 96<br> &nbsp;TextHeight = 13<br> &nbsp;object Timer1: TTimer<br> &nbsp; &nbsp;Enabled = False<br> &nbsp; &nbsp;OnTimer = Timer1Timer<br> &nbsp; &nbsp;Left = 100<br> &nbsp; &nbsp;Top = 16<br> &nbsp;end<br>end<br><br>提示窗体:*.pas<br>-----------------------------------------------<br>unit Unit2;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;StdCtrls, Buttons;<br><br>type<br> &nbsp;TForm2 = class(TForm)<br> &nbsp; &nbsp;SpeedButton1: TSpeedButton;<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;procedure SpeedButton1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form2: TForm2;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm2.SpeedButton1Click(Sender: TObject);<br>begin &nbsp;//关闭按钮<br> &nbsp;Close;<br>end;<br><br>end.<br><br>提示窗体:*.dfm &nbsp; <br>设为 AutoSize = True BorderStyle = bsNone <br>-------------------------------------------<br>object Form2: TForm2<br> &nbsp;Left = 516<br> &nbsp;Top = 164<br> &nbsp;AutoSize = True<br> &nbsp;BorderStyle = bsNone<br> &nbsp;BorderWidth = 2<br> &nbsp;Caption = 'Form2'<br> &nbsp;ClientHeight = 71<br> &nbsp;ClientWidth = 163<br> &nbsp;Color = 11599871<br> &nbsp;Font.Charset = DEFAULT_CHARSET<br> &nbsp;Font.Color = clWindowText<br> &nbsp;Font.Height = -11<br> &nbsp;Font.Name = 'MS Sans Serif'<br> &nbsp;Font.Style = []<br> &nbsp;OldCreateOrder = False<br> &nbsp;PixelsPerInch = 96<br> &nbsp;TextHeight = 13<br> &nbsp;object SpeedButton1: TSpeedButton<br> &nbsp; &nbsp;Left = 0<br> &nbsp; &nbsp;Top = 0<br> &nbsp; &nbsp;Width = 23<br> &nbsp; &nbsp;Height = 22<br> &nbsp; &nbsp;Flat = True<br> &nbsp; &nbsp;OnClick = SpeedButton1Click<br> &nbsp;end<br> &nbsp;object Label1: TLabel<br> &nbsp; &nbsp;Left = 1<br> &nbsp; &nbsp;Top = 32<br> &nbsp; &nbsp;Width = 162<br> &nbsp; &nbsp;Height = 39<br> &nbsp; &nbsp;Caption = <br> &nbsp; &nbsp; &nbsp;' &nbsp;演示数据 演示数据 演示数据 &nbsp;'#13#10' &nbsp;演示数据 演示数据 演示数据 &nbsp;'#13#10 +<br> &nbsp; &nbsp; &nbsp;' &nbsp;'<br> &nbsp;end<br>end
 
谢谢了,我先试试
 
后退
顶部