【问题标题】:How to detect WindowState changes?如何检测 WindowState 的变化?
【发布时间】:2017-02-27 18:22:37
【问题描述】:

如何检测WindowStateTCustomForm 后代的更改?我希望随时通知我 WindowState 属性设置为不同的值。

我检查了 setter 中是否有事件或虚拟方法,但我没有找到任何可以实现我的目标的东西。

function ShowWindow; external user32 name 'ShowWindow';

procedure TCustomForm.SetWindowState(Value: TWindowState);
const
  ShowCommands: array[TWindowState] of Integer =
    (SW_SHOWNORMAL, SW_MINIMIZE, SW_SHOWMAXIMIZED);
begin
  if FWindowState <> Value then
  begin
    FWindowState := Value;
    if not (csDesigning in ComponentState) and Showing then
      ShowWindow(Handle, ShowCommands[Value]);
  end;
end;

【问题讨论】:

    标签: forms delphi vcl


    【解决方案1】:

    当窗口状态改变时,操作系统向窗口发送的通知是WM_SIZE 消息。从您发布的代码引用中并不明显,但 VCL 已经在 TScrollingWinControl 类(TCustomForm 的后裔)中侦听 WM_SIZE,并在处理消息时调用虚拟 Resizing 过程。

    因此您可以覆盖表单的此方法以获得通知。

    type
      TForm1 = class(TForm)
        ..
      protected
        procedure Resizing(State: TWindowState); override;
    
    ....
    
    procedure TForm1.Resizing(State: TWindowState);
    begin
      inherited;
      case State of
        TWindowState.wsNormal: ;
        TWindowState.wsMinimized: ;
        TWindowState.wsMaximized: ;
      end;
    end;
    


    请注意,对于给定状态,可以多次发送通知,例如在调整窗口大小或更改可见性时。您可能需要跟踪先前的值以检测状态何时实际更改。


    根据您的要求,您还可以使用表单的OnResize 事件。不同之处在于,在操作系统通知窗口有关更改之前会触发此事件。 VCL 通过调用GetWindowPlacement 来检索窗口状态信息,而TCustomForm 正在处理WM_WINDOWPOSCHANGING

    下面是一个使用标志来跟踪先前窗口状态的示例。

      TForm1 = class(TForm)
        ..
      private
        FLastWindowState: TWindowState; // 0 -> wsNormal (initial value)
    
    ...
    
    procedure TForm1.FormResize(Sender: TObject);
    begin
      if WindowState <> FLastWindowState then
        case WindowState of
          TWindowState.wsNormal: ;
          TWindowState.wsMinimized: ;
          TWindowState.wsMaximized: ;
        end;
      FLastWindowState := WindowState;
    end;
    

    【讨论】:

      猜你喜欢
      • 2017-06-12
      • 2015-08-08
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 2011-02-03
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多