这是鼠标跟随显示坐标的代码,坐标显示的位置随鼠标位置不同而不同,不会发生显示的坐标跑到屏幕外边去而看不到的情况。在这基础上增加判断停留的代码就可以了,楼上的思路跟我一样:判断间隔1秒后,鼠标位置是否发生变化。procedure TForm2.Timer1Timer(Sender: TObject); var pt: TPoint; k,l: LongInt; begin GetCursorPos(pt); //得到鼠标的坐标 Label1.Caption:= IntToStr(pt.X)+','+IntToStr(pt.Y); k:=GetSystemMetrics(SM_CXSCREEN); l:=GetSystemMetrics(SM_CYSCREEN); if (pt.X>k-200)and(pt.X<k) then Form2.Left:= pt.X-120 else Form2.Left:= pt.X+20; if (pt.Y>l-100)and(pt.Y<l) then Form2.Top:= pt.Y-60 else Form2.Top:= pt.Y+10; end;
如果是气泡,估计得用到GDI+或者D3D……
var pt : TPoint; begin case msg.LParam of WM_LBUTTONDOWN : begin SetForegroundWindow(Handle); GetCursorPos(pt);//得到坐标 PopupMenu1.Popup(pt.x, pt.y);//显示 end; end; end;
可以每隔一秒判断一下鼠标位置,然后比较位置是否变化,气泡提示用GDI+实现比较好。
{勉强实现的你的功能,但需要进步一修改} unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, commctrl, ExtCtrls; const TTS_BALLOON = $40; TTM_SETTITLE = (WM_USER + 32);type TForm1 = class(TForm) Timer1: TTimer; procedure Timer1Timer(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } m_lastPt:TPoint; public { Public declarations } end;var Form1: TForm1; hTooltip: Cardinal; ti: TToolInfo; buffer : array[0..255] of char;implementation{$R *.dfm}procedure CreateToolTips(hWnd: Cardinal); begin hToolTip := CreateWindowEx(0, 'Tooltips_Class32', nil, TTS_ALWAYSTIP or TTS_BALLOON, Integer(CW_USEDEFAULT), Integer(CW_USEDEFAULT), Integer(CW_USEDEFAULT), Integer(CW_USEDEFAULT), hWnd, 0, hInstance, nil); if hToolTip <> 0 then begin SetWindowPos(hToolTip, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE or SWP_NOACTIVATE); ti.cbSize := SizeOf(TToolInfo); ti.uFlags := TTF_SUBCLASS; ti.hInst := hInstance; end; end;procedure AddToolTip(hwnd: DWORD; lpti: PToolInfo; IconType: Integer;Text, Title: PChar); var Item: THandle; Rect: TRect; begin Item := hWnd; if (Item <> 0) and (GetClientRect(Item, Rect)) then begin lpti.hwnd := Item; lpti.Rect := Rect; lpti.lpszText := Text; SendMessage(hToolTip, TTM_ADDTOOL, 0, Integer(lpti)); FillChar(buffer, SizeOf(buffer), #0); lstrcpy(buffer, Title); if (IconType > 3) or (IconType < 0) then IconType := 0; SendMessage(hToolTip, TTM_SETTITLE, IconType, Integer(@buffer)); end; end;procedure TForm1.FormCreate(Sender: TObject); begin timer1.Enabled := True; Timer1.Interval := 1000; AlphaBlend := True; AlphaBlendValue := 10; //窗体的透明度 Align := alClient; BorderStyle := bsNone; Visible := False; GetCursorPos(m_lastPt); end;procedure TForm1.Timer1Timer(Sender: TObject); var pt: TPoint; str: string; begin GetCursorPos(pt); //得到鼠标的坐标 if (m_lastPt.X <> pt.X) and (m_lastPt.Y <> pt.Y) then begin m_lastPt.X := pt.X; m_lastPt.Y := pt.Y; Visible := False; Exit; end; str:= Format('[x:%d y:%d]', [pt.X, pt.Y]); Visible := True; CreateToolTips(Handle); AddToolTip(Handle, @ti, 1, PChar(str), '坐标');end;end.
最后一次坐标和当前操作的坐标的位置相同
var
pt: TPoint;
k,l: LongInt;
begin
GetCursorPos(pt); //得到鼠标的坐标
Label1.Caption:= IntToStr(pt.X)+','+IntToStr(pt.Y); k:=GetSystemMetrics(SM_CXSCREEN);
l:=GetSystemMetrics(SM_CYSCREEN);
if (pt.X>k-200)and(pt.X<k) then Form2.Left:= pt.X-120
else Form2.Left:= pt.X+20;
if (pt.Y>l-100)and(pt.Y<l) then Form2.Top:= pt.Y-60
else Form2.Top:= pt.Y+10;
end;
pt : TPoint;
begin
case msg.LParam of
WM_LBUTTONDOWN : begin
SetForegroundWindow(Handle);
GetCursorPos(pt);//得到坐标
PopupMenu1.Popup(pt.x, pt.y);//显示
end;
end;
end;
{勉强实现的你的功能,但需要进步一修改}
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, commctrl, ExtCtrls;
const
TTS_BALLOON = $40;
TTM_SETTITLE = (WM_USER + 32);type
TForm1 = class(TForm)
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
m_lastPt:TPoint;
public
{ Public declarations }
end;var
Form1: TForm1;
hTooltip: Cardinal;
ti: TToolInfo;
buffer : array[0..255] of char;implementation{$R *.dfm}procedure CreateToolTips(hWnd: Cardinal);
begin
hToolTip := CreateWindowEx(0, 'Tooltips_Class32', nil,
TTS_ALWAYSTIP or TTS_BALLOON,
Integer(CW_USEDEFAULT),
Integer(CW_USEDEFAULT),
Integer(CW_USEDEFAULT),
Integer(CW_USEDEFAULT),
hWnd, 0, hInstance, nil);
if hToolTip <> 0 then
begin
SetWindowPos(hToolTip, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE or SWP_NOSIZE or SWP_NOACTIVATE);
ti.cbSize := SizeOf(TToolInfo);
ti.uFlags := TTF_SUBCLASS;
ti.hInst := hInstance;
end;
end;procedure AddToolTip(hwnd: DWORD; lpti: PToolInfo; IconType: Integer;Text, Title: PChar);
var
Item: THandle;
Rect: TRect;
begin
Item := hWnd;
if (Item <> 0) and (GetClientRect(Item, Rect)) then
begin
lpti.hwnd := Item;
lpti.Rect := Rect;
lpti.lpszText := Text;
SendMessage(hToolTip, TTM_ADDTOOL, 0, Integer(lpti));
FillChar(buffer, SizeOf(buffer), #0);
lstrcpy(buffer, Title);
if (IconType > 3) or (IconType < 0) then IconType := 0;
SendMessage(hToolTip, TTM_SETTITLE, IconType, Integer(@buffer));
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
timer1.Enabled := True;
Timer1.Interval := 1000;
AlphaBlend := True;
AlphaBlendValue := 10; //窗体的透明度
Align := alClient;
BorderStyle := bsNone;
Visible := False; GetCursorPos(m_lastPt);
end;procedure TForm1.Timer1Timer(Sender: TObject);
var
pt: TPoint;
str: string;
begin
GetCursorPos(pt); //得到鼠标的坐标
if (m_lastPt.X <> pt.X) and (m_lastPt.Y <> pt.Y) then
begin
m_lastPt.X := pt.X;
m_lastPt.Y := pt.Y;
Visible := False;
Exit;
end; str:= Format('[x:%d y:%d]', [pt.X, pt.Y]);
Visible := True;
CreateToolTips(Handle);
AddToolTip(Handle, @ti, 1, PChar(str), '坐标');end;end.
有几个问题
1,当鼠标不动时,显示的坐标会不断变化?
2,程序的内存占用不断增加
3,打开任务管理器后,取不到任务管理器上面的坐标。
之前我说的效果,差不多像delphi提示框的效果,现在看来,一直显示,也行。
但如果只显示两秒或一秒消失会不会更好呢?前提是内存不要占用太大。
总一直消失,感觉屏幕有点乱。而且任务栏按钮和窗口会有闪烁。这个程序最好是用gdi+实现,会更漂亮一些,本人也对此想学。