全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1323
推到 Plurk!
推到 Facebook!

磁性”視窗

 
jackkcg
站務副站長


發表:891
回覆:1050
積分:848
註冊:2002-03-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-10-26 02:20:33 IP:61.64.xxx.xxx 未訂閱
“磁性”視窗 http://delphi.cxc.cc/jiqiao/005.htm “磁性”視窗 Winamp的用戶都知道,Winamp的播放列表或等化器在被移動的時候,仿佛會受到一股磁力,每當靠近主窗口時就一下子被“吸附”過去,自動沿邊對齊。我想讓我的Winamp插件也具備這種奇妙特性,於是琢磨出了一種“磁化”窗口的方法。該法適用於Delphi的各個版本。爲了演示這種技術,請隨我來製作一個會被Winamp“吸引”的樣板程式。 先新建一應用程式專案,把主視窗Form1適當改小些,並將BorderStyle設爲bsNone。放一個按鈕元件,雙擊它並在OnClick事件中寫“Close;”。待會兒就按它來結束程式。現在切換到代碼編輯區,定義幾個總體變數。 var Form1: TForm1; //“磁性”視窗 LastX, LastY: Integer; //記錄前一次的座標 WinampRect:Trect; //保存Winamp視窗的矩形區域 hwnd_Winamp:HWND; //Winamp視窗的控制控制碼 接著編寫Form1的OnMouseDown和OnMouseMove事件。 procedure TForm1.FormMouseDown(Sender: Tobject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); const ClassName=‘Winamp v1.x’; //Winamp主窗口的類名 //如果改成ClassName=‘TAppBuilder’,你就會發現連Delphi也有引力啦! begin //記錄當前座標 LastX := X; LastY := Y; //查找Winamp hwnd_Winamp := FindWindow(ClassName,nil); if hwnd_Winamp>0 then //找到的話,記錄其視窗區域 GetWindowRect(hwnd_Winamp, WinampRect); 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; nTop := Top + Y - LastY; //如果找到Winamp,就修正以上座標,産生“磁化”效果 if hwnd_Winamp>0 then Magnetize(nleft,ntop); //重設窗口位置 SetBounds(nLeft,nTop,width,height); end; end; 別急著,看Magnetize()過程,先來瞭解一下修正座標的原理。根據對Winamp實現效果的觀察,我斗膽給所謂“磁化”下一個簡單的定義,就是“在原視窗與目標視窗接近到某種預定程度,通過修正原窗口的座標,使兩窗口處於同一平面且具有公共邊的過程”。依此定義,我設計了以下的“磁化”步驟。第一步,判斷目標視窗(即Winamp)和我們的Form1在水平及垂直方向上的投影線是否重疊。“某方向投影線有重疊”是“需要進行座標修正”的必要非充分條件。判斷依據是兩投影線段最右與最左邊界的差減去它們寬度和的值的正負。第二步,判斷兩窗口對應邊界是否靠得足夠近了。肯定的話就讓它們合攏。 好了,下面便是“神秘”的Magnetize過程了…… procedure TForm1.Magnetize(var nl,nt:integer); //內嵌兩個比大小的函數 function Min(a,b:integer):integer; begin if a>b then result:=b else result:=a; end; function Max(a,b:integer):integer; begin if a end; var H_Overlapped,V_Overlapped:boolean; //記錄投影線是否重疊 tw,ww,wh:integer; //臨時變數 const MagneticForce:integer=50; //“磁力”的大小。 //準確的說,就是控制視窗邊緣至多相距多少圖元時需要修正座標 //爲了演示,這裏用一個比較誇張的數位——50。 //一般可以用20左右,那樣比較接近Winamp的效果 begin //判斷水平方向是否有重疊投影 ww := WinampRect.Right-WinampRect.Left; tw := Max(WinampRect.Right,nl+Width)-Min(WinampRect.Left,nl); H_Overlapped := tw<=(Width+ww); //再判斷垂直方向 wh := WinampRect.Bottom-WinampRect.Top; tw := Max(WinampRect.Bottom,nt+Height)-Min(WinampRect.Top,nt); V_Overlapped := tw<=(Height+wh); //足夠接近的話就調整座標 if H_Overlapped then begin if Abs(WinampRect.Bottom-nt) else if Abs(nt+Height-WinampRect.Top) end; if V_Overlapped then begin if Abs(WinampRect.Right-nl) else if Abs(nl+Width-WinampRect.Left) end; end; ********************************************************* 哈哈&兵燹 最會的2大絕招 這個不會與那個也不會 哈哈哈 粉好 Delphi K.Top的K.Top分兩個字解釋Top代表尖端的意思,希望本討論區能提供Delphi的尖端新知 K.表Knowlege 知識,就是本站的標語:Open our mind to make knowledge together! 希望能大家敞開心胸,將知識寶庫結合一起
------
**********************************************************
哈哈&兵燹
最會的2大絕招 這個不會與那個也不會 哈哈哈 粉好

Delphi K.Top的K.Top分兩個字解釋Top代表尖端的意思,希望本討論區能提供Delphi的尖端新知
K.表Knowlege 知識,就是本站的標語:Open our mind
chih
版主


發表:48
回覆:1186
積分:639
註冊:2002-04-02

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-06-25 14:34:32 IP:218.165.xxx.xxx 未訂閱
原文章連結不見了..因為程式有部份Loss..找了好久..找到了.. http://www.undu.com/Articles/020329b.html
var
     Form1: TForm1;                   //yeah, the magnetic window... 
     LastX, LastY: Integer;           //keep the coordinates of cursor 
     WinampRect:TRect;              //stores the region of target window 
     hwnd_Winamp:HWND;        //the handle of Winamp's main window     Then fill the OnMouseDown and OnMouseMove events of Form1 as shown below.    procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;  
      Shift: TShiftState; X, Y: Integer); 
const 
      ClassName='Winamp v1.x';   //the classname of Winamp's main window 
      { Replace the line with "ClassName='TAppBuilder'" if your Delphi itself hungers for some magnetic power. } 
begin     //save the coordinates 
LastX := X;
LastY := Y; 
//try to locate Winamp 
hwnd_Winamp := FindWindow(ClassName,nil); 
if hwnd_Winamp>0 then 
      GetWindowRect(hwnd_Winamp, WinampRect);     end;     procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); 
var 
     nLeft,nTop:integer; 
begin 
//check whether the form is being dragged 
     if HiWord(GetAsyncKeyState(VK_LBUTTON)) > 0 then 
     begin
           //calculate new position 
           nleft := Left   X - LastX; 
           nTop := Top   Y - LastY;
//adjust the position if Winamp is found 
ifhwnd_Winamp>0 then
        Magnetize(nleft,ntop); 
     SetBounds(nLeft,nTop,width,height); 
   end;    end;    Have a pause here. Before unveiling the codes of "magnetize()", I wanna give an unofficial definition of "window-magnetization". That is, "a process of adjusting the position of source window to align it and target window on one plane surface when they are close to some predefined extent". A little bit confusing huh? Well, sample codes are sometimes better than dull explanation. Let's continue...          procedure TForm1.Magnetize(var nl,nt:integer);
     //two simple embedded functions
     function Min(a,b:integer):integer;
     begin
     ifa>b then result:=b else result:=a;
     end;
     function Max(a,b:integer):integer;
     begin
     if a < b then result:=b else result:=a;
     end;
var
     H_Overlapped,V_Overlapped:boolean;
     tw,ww,wh:integer;
const
     MagneticForce:integer=50;
     { in other words, when two windows are nearer than 50 pixels, they will be aligned automatically.  Remember, 50 is used for demonstration purposes only. 
        It's quite exaggerative if you want to simulate the effect of Winamp. }
begin    ww := WinampRect.Right-WinampRect.Left;
tw := Max(WinampRect.Right,nl Width)-Min(WinampRect.Left,nl);
H_Overlapped := tw<=(Width ww);    wh := WinampRect.Bottom-WinampRect.Top;
tw := Max(WinampRect.Bottom,nt Height)-Min(WinampRect.Top,nt);
V_Overlapped := tw<=(Height wh);    //change the position when the two windows are close enough
if H_Overlapped then
     begin
          if Abs(WinampRect.Bottom-nt)< MagneticForce then nt := WinampRect.Bottom
          else if Abs(nt Height-WinampRect.Top)< MagneticForce then nt := WinampRect.Top-Height;
     end;    if V_Overlapped then
     begin
          if Abs(WinampRect.Right-nl)< MagneticForce then nl := WinampRect.Right
          else if Abs(nl Width-WinampRect.Left)< MagneticForce then nl := WinampRect.Left-Width;
     end;
end;
發表人 - chih 於 2004/06/25 14:36:49
系統時間:2024-06-24 21:36:39
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!