如何限定鼠标在一定半径的圆内(100分)

  • 主题发起人 主题发起人 yanlei
  • 开始时间 开始时间
Y

yanlei

Unregistered / Unconfirmed
GUEST, unregistred user!
如何限定鼠标在一定半径的圆内
 
思考中、、、
 
其实你可以通过改变鼠标的指针来表示是否位于圆内。

创建一个圆形区域,如果鼠标坐标位于区域内,用一种指针来表示;否则显示另一种指针。
 
在Form上放TLabel1...5五个,TTimer一个
Timer1.Interval := 1;
代码如下,在D5+Win2000下调试通过:
unit Unit1;
interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Timer1: TTimer;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

const
iX0 = 200; //圆心X坐标
iY0 = 200; //圆心Y坐标
iRad = 100; //半径
var
Form1 : TForm1;
pMouse : TPoint; //鼠标

implementation

{$R *.DFM}

procedure TForm1.Timer1Timer(Sender: TObject);
var
iX, iY, iX1, iY1 : longint; //鼠标坐标
fX, fY : double; //圆周上的点坐标
fLength : double; //与圆心距离
begin
GetCursorPos(pMouse); //获得当前鼠标位置
iX := pMouse.x;
iY := pMouse.y;
if iX > Left then
iX := pMouse.x - Left
else
iX := pMouse.x;
if iY > Top then
iY := pMouse.y - Top
else
iY := pMouse.y;
Label1.Caption := inttostr(iX);
Label2.Caption := inttostr(iY);
fLength := sqrt((iX - iX0) * (iX - iX0) + (iY - iY0) * (iY - iY0));
Label5.Caption := floattostr(fLength);
fX := iRad * abs(iX - iX0) / flength;
fY := iRad * abs(iY - iY0) / flength;
Label3.Caption := floattostr(fX);
Label4.Caption := floattostr(fY);
if (iX > iX0) and (iY < iY0) then
begin
iX1 := Round(fX + iX0) + Left;
iY1 := abs(Round(fY - iY0)) + Top;
end;
if (iX > iX0) and (iY > iY0) then
begin
iX1 := Round(fX + iX0) + Left;
iY1 := Round(fY + iY0) + Top;
end;
if (iX < iX0) and (iY < iY0) then
begin
iX1 := abs(Round(iX0 - fX)) + Left;
iY1 := abs(Round(fY - iY0)) + Top;
end;
if (iX < iX0) and (iY > iY0) then
begin
iX1 := abs(Round(iX0 - fX)) + Left;
iY1 := Round(fY + iY0) + Top;
end;
if fLength > iRad then
SetCursorPos(iX1, iY1); //移动鼠标
end;

end.

 
哈哈,我刚解决了这个问题,一些工控控件中,像世纪飞扬出的虚拟旋钮(价不符实!)
中,存在一个问题,就是当鼠标按下并移动离开旋钮后,鼠标形状并没有改变,并且旋钮
仍然旋转,所以技术不好,在我做的几个工控控件中,便解决了这个问题,一开始,我也
费了两个小时搞,没搞通,一开始正如大虾“卷起千堆雪tyn”说的去做,我也想过大虾“
wind2000”的方法,但通过时钟来判断,确实是很苯的方法。但当鼠标按下
并移动离开旋钮时无效,现在我刚搞定,其实非常简单:对enable属性操作两次就行了
代码如下:
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
a:integer;
begin
a:=sqrt((X-center.x)*(X-center.x)+(Y-center.y)*(Y-center.y));
if a>r then //r为半径
begin
Mouse_down:=False;
enabled:=false;
enabled:=true;
cursor:=crarrow;
end else
begin
cursor:=crHandPoint;
end;
end;
哈哈,其中对enabled操作两次就行了,哈哈很简单吧,别忘了给分呀!
 
to :chen___ye
你的做法有时候会跑出窗体不在圆内,如果这个窗体不是全屏的话!
而且我上面的代码放到Form.OnMouseMove中也是可以的,还有一点就是请你看清楚人家出
的题:"如何限定鼠标在一定半径的圆内",不是说换个cursor就行了,那样的话还是可以运
行别的程序(如按别的按钮的),我想他是想做个类似锁屏幕之类的程序(如在网吧中有
使用网吧管理软件时刚启动时鼠标被限制在右下角一小区域内一样)
 
windows.ClipCursor(@rect);
rect为半圆的轮廓
 
呵呵,我看错题了,不过通过时钟来控制总觉得不爽
 
rect为半圆的轮廓
MapWindowpoints(handle,0.lppoint(&rect),2);
chipcursor(&rect);
取消 CHIPCURSOR(0);
 
我想他限制鼠标范围也只是一时限制,所以不用限制时就可以设Timer.Enabled := False;
也没什么不爽的!倒是你用MouseMove在不限制时还更麻烦!
 
后退
顶部