How to detect windowstate changes?

How can I detect WindowState changes for a TCustomForm child? I would like to receive notifications at any time, the WindowState property has a different meaning.

I checked if there was an event or virtual method inside the installer, but I did not find anything to achieve my goal.

 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; 
+5
source share
1 answer

The notification that the OS sends to the window when its status has been changed is a WM_SIZE message. This is not obvious from the code quote you quoted, but VCL is already listening on the WM_SIZE class in TScrollingWinControl ( TCustomForm ) and invokes the Resizing virtual procedure during message processing.

Thus, you can override this method of your form to receive a notification.

 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; 


Please note that a notification can be sent several times for a given state, for example, when changing the size of the window or changing the visibility. You may need to keep track of the previous value to find out when the state is really changed.


Depending on your requirements, you can also use the OnResize event of the form. The difference is that this event is fired before the OS notifies the window of the change. VCL retrieves window status information by calling GetWindowPlacement , and TCustomForm processes WM_WINDOWPOSCHANGING .

The following is an example of using a flag to track the status of a previous window.

  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; 
+7
source

Source: https://habr.com/ru/post/1258405/


All Articles