2014-03-04 69 views
3

当调整窗口大小时,我想在调整大小时处理OnResize事件,因为更新图形需要几秒钟。这很棘手,因为调整窗口大小会生成大量调整大小事件。由于更新窗口需要一段时间,我不希望每个事件都更新窗口。我试图检测一个鼠标,将其标记为完成调整大小的事件,但从未检测到mouseup。在FMX中处理OnResize的最佳方式是什么?

TLama有一个nice solution但唉,这是VCL,我需要它为Firemonkey。任何关于FMX的建议?

回答

1

以下是在Windows上的FMX中处理它的一种方法,您需要更改您的表单以从TResizeForm继承并分配OnResizeEnd属性。不是很干净,因为它取决于FMX内部,但应该工作。

unit UResizeForm; 

interface 

uses 
    Winapi.Windows, 
    System.SysUtils, System.Classes, 
    FMX.Types, FMX.Forms, FMX.Platform.Win; 

type 
    TResizeForm = class(TForm) 
    strict private 
    class var FHook: HHook; 
    strict private 
    FOnResizeEnd: TNotifyEvent; 
    public 
    property OnResizeEnd: TNotifyEvent read FOnResizeEnd write FOnResizeEnd; 
    class constructor Create; 
    class destructor Destroy; 
    end; 

implementation 

uses Winapi.Messages; 

var 
    WindowAtom: TAtom; 

function Hook(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall; 
var 
    cwp: PCWPSTRUCT; 
    Form: TForm; 
    ResizeForm: TResizeForm; 
begin 
    try 
    cwp := PCWPSTRUCT(lparam); 
    if cwp.message = WM_EXITSIZEMOVE then 
    begin 
     if WindowAtom <> 0 then 
     begin 
     Form := TForm(GetProp(cwp.hwnd, MakeIntAtom(WindowAtom))); 
     if Form is TResizeForm then 
     begin 
      ResizeForm := Form as TResizeForm; 
      if Assigned(ResizeForm.OnResizeEnd) then 
      begin 
      ResizeForm.OnResizeEnd(ResizeForm); 
      end; 
     end; 
     end; 
    end; 
    except 
    // eat exception 
    end; 
    Result := CallNextHookEx(0, code, wparam, lparam); 
end; 

class constructor TResizeForm.Create; 
var 
    WindowAtomString: string; 
begin 
    WindowAtomString := Format('FIREMONKEY%.8X', [GetCurrentProcessID]); 
    WindowAtom := GlobalFindAtom(PChar(WindowAtomString)); 
    FHook := SetWindowsHookEx(WH_CALLWNDPROC, Hook, 0, GetCurrentThreadId); 
end; 

class destructor TResizeForm.Destroy; 
begin 
    UnhookWindowsHookEx(FHook); 
end; 

end. 
0

作为一种变通方法添加一个TTimer到窗体,设定它的初始状态为禁用和Interval属性100毫秒。为了最大限度地减少的时候你的应用程序反作用于OnResize事件的量使用下面的代码:

procedure TForm1.FormResize(Sender: TObject); 
begin 
    with ResizeTimer do 
    begin 
     Enabled := false; 
     Enabled := true; 
    end; 
end; 

procedure TForm1.ResizeTimerTimer(Sender: TObject); 
begin 
    ResizeTimer.Enabled := false; 

    // Do stuff after resize 
end; 

的最小延迟不应该被用户察觉。

相关问题