00 Votes

Delphi: React to minimizing a form, OnMinimize

Question by Chematik | 2012-09-10 at 19:22

I would like to perform an action in Delphi when the user minimizes the form. In the Object Inspector, although, I can find all kinds of events, such as OnClose and so on, but I can not find OnMinimize there, and that is, would I need.

How can I set this event? Is that possible, anyway?

ReplyPositiveNegativeDateVotes
33 Votes

Just search for the component named TApplicationEvents. This component provides all kinds of events, your application provides, including your sought OnMinimize.

Simply drag the component on your form and write under OnMinimize the code, that should be executed.
2012-09-11 at 21:42

ReplyPositive Negative
1Best Answer5 Votes

The solution suggested by Computer Expert certainly works quite well, but there are cases in which the solution is not practical, because TApplicationEvents always applies to the entire application. But if you want to capture only the events of a particular form (window), you should better use the following code:

// declare in private section
private
  procedure WMSysCommand(var msg: TWMSysCommand);
     message WM_SYSCOMMAND;
 
 
// and here is the procedure
procedure TForm1.WMSysCommand(var msg: TWmSysCommand);
begin
  if msg.CmdType = SC_MINIMIZE then
  begin
    // here is the code that should be executed
    exit;
  end;
 
  inherited;
end;

With this procedure, you are able to catch all Windows messages that are sent to the window. When minimizing, the type of the Windows Message is "SC_MINIMIZE" and you can react to it and write down your corresponding code.

If you omit the "exit", after your code also the default action of the minimizing is triggered. With "exit", it will not be triggered.
2012-09-20 at 11:52

ReplyPositive Negative
Reply

Related Topics

Delphi: System-Wide HotKey

Tutorial | 1 Comment

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.