file://創(chuàng)建軍于2004.6.15
file://作者:透明墨豆(昵稱)
file://QQ:33125083
file://說明:本程序是從《“磁性”窗口》--- wujian2的文章修改的,原因是原文章不全和
file://有錯(cuò)誤,并且覺得有不完善的地方,如不能分辨出兩窗口是否在同一個(gè)區(qū)域,不能
file://停靠屏幕邊緣等。但此程序還有不足之處,如窗口Form不能跟隨Winamp的窗口一起移動(dòng)
file://希望大家多多指教,這是我第一篇文章。
file://詳細(xì)的說明請(qǐng)看原文
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);//定義接近屏幕邊緣時(shí)的過程
  private
    { Private declarations }
  public
    { Public declarations }
  end;
const MagneticForce:integer=20;
var
  Form1: TForm1;      file://把主窗口Form1適當(dāng)改小些,并將BorderStyle設(shè)為bsNone
  LastX,LastY:integer;
  WinampRect:TRect;
  HWND_Winamp:HWND;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
  Close;   file://關(guān)閉窗口
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
const classname='Winamp v1.x';  file://如果改成ClassName=‘TAppBuilder’,
                                //  你就會(huì)發(fā)現(xiàn)連Delphi也有引力啦
begin
  LastX:=X;     file://保存指針坐標(biāo)
  LastY:=Y;
  HWND_Winamp:=FindWindow(classname,nil);  file://獲取Winamp的窗口句柄
  if HWND_Winamp>0 then
    getwindowrect(HWND_Winamp,WinampRect);  file://獲取Winamp窗口的區(qū)域
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://計(jì)算窗口位置
      nTop:=Top+Y-LastY;
      if HWND_Winamp>0 then     file://這里進(jìn)行了少許修改
        Magnetize(nLeft,nTop)     file://當(dāng)窗口不存在時(shí)也可以進(jìn)行屏幕邊緣停靠
      else Edgetize(nLeft,nTop);
      SetBounds(nLeft,nTop,width,height);//設(shè)定窗口位置
    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用來分別標(biāo)識(shí)窗口
    file://是否在要靠近的區(qū)域內(nèi),V_L,V_R,V_T,V_B分別用來標(biāo)識(shí)窗口是否靠近
    nR,nB,tL,tR,tT,tB:integer;//nR,nB分別用來保存Form1的右邊位置和下邊位置
  begin
    nR:=nL+Width; file://計(jì)算Form1的Right,Bottom
    nB:=nT+Height;
    file://判斷窗口是否在目標(biāo)窗口的范圍內(nèi)(不知大家有沒有更簡(jiǎn)單的算法呢)
    if Width<=(WinampRect.Right-WinampRect.Left) then   file://如果Form的寬少于目標(biāo)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的高少于目標(biāo)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));
    //判斷兩窗口否足夠接近
    //計(jì)算邊框的距離絕對(duì)值
    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;
    //如果足夠接近就修改坐標(biāo)
    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;
   //接近屏幕邊緣時(shí)實(shí)現(xiàn)停靠
    if (not V_L)and(not V_R)and(not V_T)and(not V_B) then Edgetize(nL,nT);
   end;
  //定義接近屏幕邊緣時(shí)的過程
  procedure TForm1.Edgetize(var nL,nT:integer);
    var nB,nR:integer;
    begin
      nR:=nL+Width; file://計(jì)算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.
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注