2010-12-14 97 views
-2

此代碼是不是爲我工作的德爾福XE:鼠標懸停在窗體上嗎?

http://delphi.about.com/cs/adptips2000/a/bltip0800_5.htm

procedure TForm1.FormDeactivate(Sender: TObject) ; 
begin 
    ReleaseCapture; 
end; 

procedure TForm1.FormMouseMove 
(Sender: TObject; Shift: TShiftState; X,Y: Integer) ; 
begin 
    If GetCapture = 0 then 
    SetCapture(Form1.Handle) ; 
    if PtInRect(Rect(Form1.Left, 
        Form1.Top, 
        Form1.Left + Form1.Width, 
        Form1.Top + Form1.Height), 
        ClientToScreen(Point(x, y))) then 
    Form1.Caption := 'Mouse is over form' else 
    Form1.Caption := 'Mouse is outside of form'; 
end; 

沒有錯誤 - 它只是沒有任何效果。

請幫忙。

編輯1

原來,這個問題是不是與代碼,甚至是鼠標FormMouseEnter和FormMouseLeave不工作,因爲我傳遞的形式我這樣的功能創造了一個單位:

程序Slide(Form:TForm; Show:Boolean);

我從這個過程中調用Show方法。我怎樣才能克服這個問題?

謝謝。

EDIT 2

我不想用我現在張貼的功能。我想使用下面提到的人們(FormMouseEnter和FormMouseLeave),但它在我的情況下也不起作用。

+0

什麼問題? – miku 2010-12-14 10:37:45

+3

面向未來:嘗試從鏈接中包含儘可能多的信息到您的問題中,以便其他人不必通過鏈接瀏覽以便能夠回答問題。 – jpfollenius 2010-12-14 10:44:47

+0

是不是形式標題改變,同時移動鼠標進出窗體,其工作正常對我來說 – Bharat 2010-12-14 10:51:35

回答

5

您可以使用OnMouseEnterOnMouseLeave事件來跟蹤鼠標是否在表單上,​​而不捕獲鼠標光標。

1

據我所知,使用SetCapture這是一個壞主意。如果它像你希望的那樣運作,那麼只會因爲你不知道追蹤鼠標的更好方法而搶劫其他人的鼠標消息。

但MSDN說(即使使用SetCapture,外部的鼠標消息也不會被重定向到您的窗口,除非)(http://msdn.microsoft.com/en-us/library/ms646262(VS.85).aspx)鼠標按鈕關閉(可能是一種措施,以防止你到底想要達到什麼目的:在沒有正當理由的情況下盜取鼠標)。

無論你從哪裏調用Show(),所以你的問題不在於此。

3

這只是在相應表單的OnMouseEnter和OnMouseLeave事件中輸入必要的代碼。就我而言,我所做的就是:

創建德爾福
一個新的項目選擇 切換到Object Inspector中的事件標籤如有必要
你與
去工作,在Object Inspector
形式向下滾動到OnMouseEnter事件,雙擊它旁邊的空白處,它將生成一個EventHandler。請確保你結束了在事件處理程序下面的代碼:

procedure TForm1.FormMouseEnter(Sender: TObject); 
begin 
    Self.Caption := 'Mouse in form'; 
end; 

轉到Object Inspector中再次
發現在白色區域OnMouseLeave在事件並雙擊它來生成新的活動的權利處理程序,並將下面的代碼添加到它

procedure TForm1.FormMouseLeave(Sender: TObject); 
begin 
    Self.Caption := 'Mouse outside form'; 
end; 

運行程序...將鼠標移到表格,標題將更改爲「鼠標內部形式」,將它的形式之外,字幕會說'鼠標外形'

作爲一個魅力工作(在德爾福2010年測試)

0

我需要一個窗體(frmTasks)與重大修改的標題。所以我創建了一個帶有隱藏標題的表單。我使用TImage(imgRedLogo)模擬Caption,在該圖像上繪製我需要的東西。 此代碼讓用戶點擊假標題(圖片)並移動表格。像沒有捕獲鼠標的魅力一樣工作。它甚至可以用鼠標右鍵來操作(如果你想禁用這個'功能',你必須在imgRedLogoMouseDown中測試'Button'參數)。

FULL工作代碼:

VAR 
    Dragged : Boolean= FALSE; 
    IsOverImg: Boolean= FALSE; { True if mouse if over the image } 
    OldPos : TPoint; 

procedure TfrmTasks.imgRedLogoMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); 
begin 
Dragged:= True; 
GetCursorPos(OldPos); 
end; 

procedure TfrmTasks.imgRedLogoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); 
VAR NewPos: TPoint; 
begin 
if Dragged AND IsOverImg then 
    begin 
    GetCursorPos(NewPos); 
    frmTasks.Left:= frmTasks.Left- OldPos.X + NewPos.X; 
    frmTasks.Top := frmTasks.Top - OldPos.Y + NewPos.Y; 
    OldPos:= NewPos; 
    end; 
end; 

procedure TfrmTasks.imgRedLogoMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); 
begin 
if Dragged 
then Dragged:= False; 
end; 

procedure TfrmTasks.imgRedLogoMouseEnter(Sender: TObject); 
begin 
IsOverImg:= TRUE; 
end; 

procedure TfrmTasks.imgRedLogoMouseLeave(Sender: TObject); 
begin 
IsOverImg:= FALSE; 
end; 

享受。