file://创建军于2004.6.15 file://作者:透明墨豆(昵称) file://QQ:33125083 file://说明:本程序是从《“磁性”窗口》--- wujian2的文章修改的,原因是原文章不全和 file://有错误,并且觉得有不完善的地方,如不能分辨出两窗口是否在同一个区域,不能 file://停靠屏幕边缘等。但此程序还有不足之处,如窗口Form不能跟随Winamp的窗口一起移动 file://希望大家多多指教,这是我第一篇文章。 file://详细的说明请看原文
unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure Magnetize(var nl,nt:integer); procedure Edgetize(var nL,nT:integer);//定义接近屏幕边缘时的过程 private { Private declarations } public { Public declarations } end;
const MagneticForce:integer=20;
var Form1: TForm1; file://把主窗口Form1适当改小些,并将BorderStyle设为bsNone LastX,LastY:integer; WinampRect:TRect; HWND_Winamp:HWND;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); begin Close; file://关闭窗口 end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); const classname=''''Winamp v1.x''''; file://如果改成ClassName=‘TAppBuilder’, // 你就会发现连Delphi也有引力啦 begin LastX:=X; file://保存指针坐标 LastY:=Y; HWND_Winamp:=FindWindow(classname,nil); file://获取Winamp的窗口句柄 if HWND_Winamp>0 then getwindowrect(HWND_Winamp,WinampRect); file://获取Winamp窗口的区域 end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var nLeft,nTop:integer; begin if HiWord(GetAsyncKeyState(VK_LBUTTON)) >0 then begin nLeft:=Left+X-LastX; file://计算窗口位置 nTop:=Top+Y-LastY; if HWND_Winamp>0 then file://这里进行了少许修改 Magnetize(nLeft,nTop) file://当窗口不存在时也可以进行屏幕边缘停靠 else Edgetize(nLeft,nTop); SetBounds(nLeft,nTop,width,height);//设定窗口位置 end; end; file://=============以上部分为抄录别人,下面为自已之写的======================///// procedure TForm1.Magnetize(var nL,nT:integer); var B_LR,B_TB,V_L,V_R,V_T,V_B:boolean; file://定义中间变量,B_LR,B_TB用来分别标识窗口 file://是否在要靠近的区域内,V_L,V_R,V_T,V_B分别用来标识窗口是否靠近 nR,nB,tL,tR,tT,tB:integer;//nR,nB分别用来保存Form1的右边位置和下边位置 begin nR:=nL+Width; file://计算Form1的Right,Bottom nB:=nT+Height; file://判断窗口是否在目标窗口的范围内(不知大家有没有更简单的算法呢) if Width<=(WinampRect.Right-WinampRect.Left) then file://如果Form的宽少于目标Form B_LR:=((nL>=WinampRect.Left)and(nL<=WinampRect.Right))or((nR>=WinampRect.Left) and(nR<=WinampRect.Right)) else B_LR:=((nL<=WinampRect.Right)and(nR>=WinampRect.Right))or((nL<=WinampRect.Left) and(nR>=WinampRect.Left)); if Height<=(WinampRect.Bottom-WinampRect.Top) then file://如果Form的高少于目标Form B_TB:=((nT>=WinampRect.Top)and(nT<=WinampRect.Bottom))or((nB>=WinampRect.Top) and(nB<=WinampRect.Bottom)) else B_TB:=((nT<=WinampRect.Top)and(nB>=WinampRect.Top))or((nT<=WinampRect.Bottom) and(nB>=WinampRect.Bottom)); //判断两窗口否足够接近 //计算边框的距离绝对值 tL:=abs(WinampRect.Right-nL); tR:=abs(WinampRect.Left-nR); tT:=abs(WinampRect.Bottom-nT); tB:=abs(WinampRect.Top-nB); V_L:=tL<MagneticForce; V_R:=tR<MagneticForce; V_T:=tT<MagneticForce; V_B:=tB<MagneticForce; //如果足够接近就修改坐标 if B_TB then if V_L then nL:=WinampRect.Right else if V_R then nL:=WinampRect.Left-Width; if B_LR then if V_T then nT:=WinampRect.Bottom else if V_B then nT:=WinampRect.Top-Height; //接近屏幕边缘时实现停靠 if (not V_L)and(not V_R)and(not V_T)and(not V_B) then Edgetize(nL,nT); end; //定义接近屏幕边缘时的过程 procedure TForm1.Edgetize(var nL,nT:integer); var nB,nR:integer; begin nR:=nL+Width; file://计算Form1的Right,Bottom nB:=nT+Height; file://Screen是用来获取屏幕分辨率的 if nT<=MagneticForce then nT:=0 else if abs(nB-Screen.Height)<=MagneticForce then nT:=Screen.Height-Height; if nL<=MagneticForce then nL:=0 else if abs(nR-Screen.Width)<=MagneticForce then nL:=Screen.Width-Width; end;
end.
没有相关教程
|