//自定義函數GetFormNameAt,獲得鼠標針指下窗體的Name
function GetFormNameAt ( X, Y : integer ) : string;
var
   P:TPoint;
   W:TWinControl;
begin
   P.X := X;
   P.Y := Y;
   W := FindVCLWindow(P); //得到鼠標指針下的VCL可視組件
   if ( nil <> W ) then
   begin
      while w.Parent<>nil do //當W的上級Parent不為空時就繼續往上找
        w:=w.Parent; 
      Result := W.Name;//最后返回窗體的名稱Name
   end
   else
   begin
    Result := '';
   end;
end;
PRocedure TForm1.Timer1Timer(Sender: TObject);
var
   winPos:TPoint;
begin
   GetCursorPos(winpos);  //得到當前鼠標指針的在屏幕上的坐標
  
   //當鼠標指針下的窗體的Name等于form1.name時
   if form1.name=GetFormNameAt(winpos.X,winpos.Y) then
   {在此我們可以為form1取一個特別的名稱,以防有別的窗體名稱與它相同}
   begin
    form1.Timer2.Enabled:=false; //停用Timer2
    form1.Top:=0;  //form1的Top與屏幕對齊
   end
   else
    form1.Timer2.Enabled:=true; //開啟Timer2
end;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
   if form1.Top<=20 then
      form1.Top:=-(form1.Height-10);//將form1向上移,在屏幕上方露出10像素
end;
新聞熱點
疑難解答