简体   繁体   English

如何检测 Delphi FMX Windows 表单中的鼠标后退和前进按钮?

[英]How detect the mouse back and forward buttons in a Delphi FMX Windows form?

Has anyone found a way to detect the mouse back and forward buttons in a Delphi FMX form in Windows (and only Windows)?有没有人找到一种方法来检测 Windows(仅 Windows)中 Delphi FMX 形式的鼠标后退和前进按钮?

I understand this works fine in a VCL application, using我知道这在 VCL 应用程序中工作正常,使用

procedure WMAppCommand(var Msg: Winapi.Messages.TMessage); message WM_APPCOMMAND;

but this has no effect in an FMX application.但这对 FMX 应用程序没有影响。

If anyone already has worked out a solution to this, would very much appreciate a hint (or the code they used, of course).如果有人已经为此制定了解决方案,将非常感谢提示(或他们使用的代码,当然)。

FMX heavily filters window messages, only dispatching the few messages it actually uses for itself. FMX 对 window 消息进行大量过滤,只发送它实际用于自身的少数消息。 WM_APPCOMMAND is not one of them, which is why a simple message handler does not work in FMX, like it does in VCL. WM_APPCOMMAND不是其中之一,这就是为什么简单的message处理程序在 FMX 中不起作用的原因,就像在 VCL 中那样。

So, you are going to have to manually subclass the TForm 's Win32 HWND directly, via SetWindowLongPtr(GWLP_WNDPROC) or SetWindowSubclass() , in order to intercept window messages before FMX sees them.因此,您将不得不通过SetWindowLongPtr(GWLP_WNDPROC)SetWindowSubclass()直接手动对TForm的 Win32 HWND进行子类化,以便在 FMX 看到它们之前拦截 window 消息。 See Subclassing controls .请参阅子类化控件

An ideal place to do that subclassing is to override the TForm.CreateHandle() method.进行子类化的理想位置是覆盖TForm.CreateHandle()方法。 You can use FMX's FormToHWND() function to get the TForm 's HWND after it has been created.创建 TForm 后,您可以使用 FMX 的FormToHWND() function 获取TFormHWND

protected
  procedure CreateHandle; override;

...

uses
  FMX.Platform.Win, Winapi.Windows, Winapi.CommCtrl;

function MySubclassProc(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM;
  uIdSubclass: UINT_PTR; dwRefData: DWORD_PTR): LRESULT; stdcall;
begin
  case uMsg of
    WM_APPCOMMAND: begin
     // use TMyForm(dwRefData) as needed...
    end;
    WM_NCDESTROY:
      RemoveWindowSubclass(hWnd, @MySubclassProc, uIdSubclass);
  end;
  Result := DefSubclassProc(hWnd, uMsg, wParam, lParam);
end;

procedure TMyForm.CreateHandle;
begin
  inherited;
  SetWindowSubclass(FormToHWND(Self), @MySubclassProc, 1, DWORD_PTR(Self));
end;

procedure InitStandardClasses;
var 
  ICC: TInitCommonControlsEx;
begin
  ICC.dwSize := SizeOf(TInitCommonControlsEx);
  ICC.dwICC := ICC_STANDARD_CLASSES;
  InitCommonControlsEx(ICC);
end;

initialization
  InitStandardClasses;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM