【问题标题】:firemonkey and global hot key?firemonkey 和全局热键?
【发布时间】:2013-03-12 10:53:41
【问题描述】:

我正在寻找一种在 firemonkey 应用程序中缓存全局热键的方法(仅限 Windows,至少目前如此)。 经过一些挫折和谷歌搜索后,这应该可以工作: 用winapi调用注册热键

RegisterHotKey(FmxHandleToHWND(form1.Handle), 0 , MOD_CONTROL, $41); 

它返回真。
然后在表单的过程中捕获热键

procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;

但是这个永远不会被调用。 我以前在 vcl 应用程序中这样做过,所以我的猜测是 firemonkey 以不同的方式处理消息。 那么问题来了:如何在 firemonkey 应用中捕捉全局热键?

edit:应用该解决方案的一些示例。我创建了一个班级很少的单元

unit fire_hotkey;

interface

uses windows, messages,allocatehwnd;

type
  TMsgHandler = procedure (var Msg: TMessage) of object;

  THotClass = class(TObject)
    fMsgHandlerHWND : HWND;
    proc:TMsgHandler;
    constructor Create;
    procedure init;
    destructor Destroy; override;
  end;

implementation

{ hotClass }

constructor THotClass.Create;
begin
  inherited;

end;

destructor THotClass.Destroy;
begin
  ThreadDeallocateHWnd(fMsgHandlerHWND);
  inherited;
end;

procedure THotClass.init;
begin
   fMsgHandlerHWND := ThreadAllocateHWnd(proc,true);
end;

end.

那么我的主窗体有一个处理热键事件的过程:

procedure TformEditor.WMHotKey(var Msg: TMessage);
begin
  if Msg.Msg = WM_HOTKEY then
  begin
    //call lua function or sth
    //...
  end
  else
    Msg.Result := DefWindowProc(hotkeyGrabber.fMsgHandlerHWND, Msg.Msg, Msg.wParam, Msg.lParam);
end;

并且有一个全局的 hotkeyGrabber:THotClass;在表单创建时初始化:

  hotkeyGrabber:=THotClass.Create;
  hotkeyGrabber.proc:=WMHotKey;
  hotkeyGrabber.init;

之后,您应该像在通常的 vcl 应用程序中一样注册热键,它们将被咳嗽 http://www.swissdelphicenter.ch/torry/showcode.php?id=147 希望有意义

【问题讨论】:

  • 有人私信我如何实际使用该解决方案,所以我将其添加到问题中

标签: delphi firemonkey firemonkey-fm2


【解决方案1】:

FMX 框架不会将消息路由到您的表单。因此,您的 WMHotKey 将永远不会被调用,因为 FMX 框架永远不会调用 Dispatch。通过检查FMX.Platform.Win 单元的实现部分中声明的WndProc 方法,您可以看到这种情况。

解决此问题的最简单方法是通过调用CreateWindow 创建您自己的窗口。然后为该窗口实现一个窗口过程来处理WM_HOTKEY 消息。

我已经像这样包装了那些低级 API 调用:

unit AllocateHWnd;

interface

uses
  System.SysUtils, System.Classes, System.SyncObjs, Winapi.Messages, Winapi.Windows;

function ThreadAllocateHWnd(AMethod: TWndMethod; MessageOnly: Boolean): HWND;
procedure ThreadDeallocateHWnd(Wnd: HWND);

implementation

const
  GWL_METHODCODE = SizeOf(Pointer)*0;
  GWL_METHODDATA = SizeOf(Pointer)*1;
  ThreadAllocateHWndClassName = 'MyCompanyName_ThreadAllocateHWnd';

var
  ThreadAllocateHWndLock: TCriticalSection;
  ThreadAllocateHWndClassRegistered: Boolean;

function ThreadAllocateHWndProc(Window: HWND; Message: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
  Proc: TMethod;
  Msg: TMessage;
begin
  Proc.Code := Pointer(GetWindowLongPtr(Window, GWL_METHODCODE));
  Proc.Data := Pointer(GetWindowLongPtr(Window, GWL_METHODDATA));
  if Assigned(TWndMethod(Proc)) then begin
    Msg.Msg := Message;
    Msg.wParam := wParam;
    Msg.lParam := lParam;
    Msg.Result := 0;
    TWndMethod(Proc)(Msg);
    Result := Msg.Result
  end else begin
    Result := DefWindowProc(Window, Message, wParam, lParam);
  end;
end;

function ThreadAllocateHWnd(AMethod: TWndMethod; MessageOnly: Boolean): HWND;

  procedure RegisterThreadAllocateHWndClass;
  var
    WndClass: TWndClass;
  begin
    if ThreadAllocateHWndClassRegistered then begin
      exit;
    end;
    ZeroMemory(@WndClass, SizeOf(WndClass));
    WndClass.lpszClassName := ThreadAllocateHWndClassName;
    WndClass.hInstance := HInstance;
    WndClass.lpfnWndProc := @ThreadAllocateHWndProc;
    WndClass.cbWndExtra := SizeOf(TMethod);
    Winapi.Windows.RegisterClass(WndClass);
    ThreadAllocateHWndClassRegistered := True;
  end;

begin
  ThreadAllocateHWndLock.Acquire;
  Try
    RegisterThreadAllocateHWndClass;
    if MessageOnly then begin
      Result := CreateWindow(ThreadAllocateHWndClassName, '', 0, 0, 0, 0, 0, HWND_MESSAGE, 0, HInstance, nil);
    end else begin
      Result := CreateWindowEx(WS_EX_TOOLWINDOW, ThreadAllocateHWndClassName, '', WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, nil);
    end;
    Win32Check(Result<>0);
    SetWindowLongPtr(Result, GWL_METHODDATA, NativeInt(TMethod(AMethod).Data));
    SetWindowLongPtr(Result, GWL_METHODCODE, NativeInt(TMethod(AMethod).Code));
  Finally
    ThreadAllocateHWndLock.Release;
  End;
end;

procedure ThreadDeallocateHWnd(Wnd: HWND);
begin
  Win32Check(DestroyWindow(Wnd));
end;

initialization
  ThreadAllocateHWndLock := TCriticalSection.Create;

finalization
  ThreadAllocateHWndLock.Free;

end.

这是 VCL AllocateHWnd 的线程安全版本,它因在主线程之外无法使用而臭名昭著。

您需要做的是创建一个带有窗口过程的类,即实现TWndMethod 的东西。它可以是实例方法或类方法。然后只需调用ThreadAllocateHWnd 来创建窗口,并将该窗口传递给RegisterHotKey。当需要解开这一切时,请取消注册您的热键,并通过调用 ThreadDeallocateHWnd 来销毁窗口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 2010-12-12
    • 2019-12-07
    • 2016-02-22
    • 2018-05-01
    • 2011-03-15
    • 2011-05-28
    相关资源
    最近更新 更多