2010-06-24 72 views
2

编辑:VCL没有问题左右拖动和下面的示例程序完美。鼠标手势实用程序导致该问题。 (也许它挂钩&拦截WM_RBUTTONUP事件...)如何检测TControl的右拖动结束?

我想检测端上的控制权,拖着

对于左侧拖动,我可以使用MouseUp事件,但右侧拖动后不会发生。

关于下面的测试程序(在表单的右侧放置一个备忘并拖动窗体), 我想在右拖后重置鼠标光标。

我该如何做到这一点? (WM_RBUTTONUP不来的。)

unit Unit1; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls; 

type 
    TForm1 = class(TForm) 
    Memo1: TMemo; 
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: 
     TShiftState; X, Y: Integer); 
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); 
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: 
     TShiftState; X, Y: Integer); 
    procedure WMRButtonUp(var Message: TWMRButtonUp); message WM_RBUTTONUP; 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

function ShiftStateToStr(Shift: TShiftState): string; 
begin 
    if ssShift in Shift then 
    Result := Result + 'S-'; 
    if ssCtrl in Shift then 
    Result := Result + 'C-'; 
    if ssAlt in Shift then 
    Result := Result + 'A-'; 
    if ssDouble in Shift then 
    Result := Result + 'D-'; 
    if ssLeft in Shift then 
    Result := Result + 'L'; 
    if ssRight in Shift then 
    Result := Result + 'R'; 
    if ssMiddle in Shift then 
    Result := Result + 'M'; 
end; 

function MouseButtonToStr(Btn: TMouseButton): string; 
begin 
    if Btn = mbLeft then 
    Result := 'Left' 
    else if Btn = mbRight then 
    Result := 'Right' 
    else if Btn = mbMiddle then 
    Result := 'Middle'; 
end; 


procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: 
    TShiftState; X, Y: Integer); 
begin 
    SetCapture(Handle); 
    Memo1.Lines.Add(Format('Down(Btn=%s, Shift=[%s])', [MouseButtonToStr(Button), ShiftStateToStr(Shift)])); 

    if Button = mbLeft then 
    Screen.Cursor := crDrag 
    else if Button = mbRight then 
    Screen.Cursor := crSize; 
end; 

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: 
    Integer); 
begin 
    Memo1.Lines.Add(Format('Move(Shift=[%s])', [ShiftStateToStr(Shift)])); 
end; 

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: 
    TShiftState; X, Y: Integer); 
begin 
    ReleaseCapture; 
    Memo1.Lines.Add(Format('Up(Btn=%s, Shift=[%s])', [MouseButtonToStr(Button), ShiftStateToStr(Shift)])); 

    Screen.Cursor := crDefault; 
end; 

procedure TForm1.WMRButtonUp(var Message: TWMRButtonUp); 
begin 
    Memo1.Lines.Add('WMRbuttonUp'); 
    inherited; 
end; 

end. 
+1

为什么不使用控件/表单的OnDrag ...事件? – 2010-06-24 07:48:29

+0

在真实情况下,我想旋转和翻译3D空间的视角。这只是一个简单的示例。 – benok 2010-06-25 01:49:09

回答

1

我用D2007试过了你的测试程序。一切工作如预期。当我释放鼠标右键时,触发FormMouseUpWMRButtonUp

你可以在另一台机器上测试它吗?我想你已经在Delphi中安装了“坏”的东西,或者你的系统上有某种钩子。但你的来源是正确的,应该工作。

+0

谢谢您的回复,我会检查其他一些环境。 – benok 2010-06-25 01:34:44

+0

在另一个环境中,一切都很完美。 我调查了麻烦的机器,发现了一个我忘记安装的鼠标手势util。 无论如何,非常感谢! – benok 2010-06-25 01:46:47

0

你的问题是,你需要直接检测鼠标事件,没有使用Delphi的解释。