2008-12-18 171 views
6

在德爾福2007年,在鼠標移動事件,我試圖改變鼠標光標:如何在鼠標左鍵關閉時更改鼠標光標?

procedure TFr_Board_Display.PaintBox_Proxy_BoardMouseMove(Sender: TObject; 
    Shift: TShiftState; X, Y: Integer); 
begin 

    if left_mouse_button_down then begin 
    if some_condition then begin 
     Cursor := crDrag; 
    end 
    else begin 
     Cursor := crNoDrop; 
    end; 
    end 
    else begin 
    if some_other_condition then begin 
     Cursor := crHandPoint; 
    end 
    else begin 
     Cursor := crDefault; 
    end; 
    end; 
end; 

例如。但是,當鼠標左鍵關閉時,我移動鼠標,光標不會更改爲crDrag或crNoDrop。代碼被執行(例如Cursor:= crDrag;),但遊標不會改變。當鼠標左鍵彈起時,我移動鼠標,光標變化沒有問題。

(我最初試圖用一些拖放&丟棄事件和屬性,但不能得到的一切工作,我想要的方式。)

編輯:澄清所需的行爲,並格式化代碼。

編輯:謝謝Gamecat,但我希望光標在鼠標左鍵移動時改變,鼠標移動時光標應該在crDrag和crNoDrop之間來回切換。

+0

增加了更多信息,所以它在鼠標移動時可用。 – 2008-12-18 09:54:53

回答

11

如果您在onmousedown事件設置鼠標光標在OnMouseUp復位,任何正常工作:

procedure TForm4.FormMouseDown(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Integer); 
begin 
    Cursor := crCross; 
end; 

procedure TForm4.FormMouseUp(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Integer); 
begin 
    Cursor := crDefault; // Or you can restore a saved cursor. 
end; 

如果你想在mousecursor在鼠標移動作出反應,使用以下命令:

procedure TForm4.FormMouseMove(Sender: TObject; Shift: TShiftState; X, 
    Y: Integer); 
begin 
    if ssLeft in Shift then begin 
    if X<100 then 
     Screen.Cursor := crCross 
    else 
     Screen.Cursor := crHourGlass; 
    end else 
    Screen.Cursor := crDefault; // Or you can restore a saved cursor. 
end; 

procedure TForm4.FormMouseUp(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Integer); 
begin 
    Screen.Cursor := crDefault; // Or you can restore a saved cursor. 
end; 

需要MouseUp,否則如果光標懸停在控件上方,則光標不會變回。

一定要在任何地方使用Screen.Cursor。

5

稍有偏離主題,但可能對您有用。

我創建了一個全局堆棧以允許嵌套遊標更改。它允許任何代碼將鼠標指針設置爲他們想要的,而不必擔心它們的調用者或被調用者設置了它。

例如:

procedure AskUserWhatToDo; 
begin 
    PushMouseCursor(crArrow); 
    try 
    if MessageDlg('Abort?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then 
     SysUtils.Abort; 
    finally 
    PopMouseCursor; 
    end; 
end; 

procedure LongProcess; 
begin 
    PushMouseCursor(crHourglass); 
    try 
    // do something 
    if QuestionableState then 
     AskUserWhatToDo; 
    // do something 
    finally 
    PopMouseCursor; 
    end; 
end; 

無論過程有擔心什麼狀態等方面的需求或離開鼠標光標。

//=============================================================== 
// in a universal utility module (mine is called CraftWindows.pas) 

function SetMouseCursor(ACursor : TCursor) : TCursor; 
begin 
    Result := Screen.Cursor; 
    Screen.Cursor := ACursor; 
end; 

var 
    GlobalMouseCursorStack : TList = nil; 

procedure PushMouseCursor(ACursor : TCursor); 
begin 
    if GlobalMouseCursorStack = nil then 
     GlobalMouseCursorStack := TList.Create; 

    GlobalMouseCursorStack.Add(Pointer(SetMouseCursor(ACursor))); 
end; 

procedure PopMouseCursor; 
begin 
    if (GlobalMouseCursorStack <> nil) and (GlobalMouseCursorStack.Count > 0) then 
    begin 
     SetMouseCursor(TCursor(GlobalMouseCursorStack.Last)); 
     GlobalMouseCursorStack.Delete(GlobalMouseCursorStack.Count - 1); 
    end; 
end; 

... 

finalization 
    GlobalMouseCursorStack.Free;