Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On Form Minimize 1

Status
Not open for further replies.

Jonah86

Programmer
Dec 11, 2003
152
US
I'm trying to get a form to do something upon minimizing. I'm able to use the applicationEvents for minimizing the main form, but this doesn't work for any sub forms, it seems.

Is there a way to tell a form to do something when it's minimized?
 
1) "Subforms" are child forms? If not: what are they?

2) What event are you intercepting?

3) What exactly is the problem?
3.a) The event does not fire.
3.b) The event fires but the code does not work.

If "3.b": what the code is trying to do?

buho (A).
 
I guess by subforms, I just mean forms that aren't the main form.

I want to hide the form when it's minimized. If I use the applicationEvents I can do this, but only when it's the main form being minimized.

All I want is a self.hide to run when the form I'm working on is minimized.

I'm not sure if I'm missing something or what.

I'm sorry if I'm being vague. I'm not really sure how best to word my problem. I'm not well versed on the proper lingo.
 
A programmer not versed in programmer jargon is a problem, he is not? :) :)

TApplication have an OnMinimize event, which is fired when the main form is minimized; TForm itself have not such an event (it have an OnResize, but it is not fired on minimization or restoring).

To catch the minimizations/maximizations you need to intercept the WM_SIZE message:

Code:
// In the form declaration
private
  procedure WMSize(var Msg : TMessage); message WM_SIZE;

// In the form implementation
procedure TMyForm.WMSize(var Msg : TMessage);
  begin
    // Do something
  end;

The method will be called in any size change; check the SDK to see how to detect the minimizing and restoring (basically, the WPARAM will have the values SIZE_MAXIMIZED, SIZE_MINIMIZED, SIZE_RESTORED or a couple others you are not interested in).

Dont forget to call "inherited" if you are not interested in the message or if you want the default processing before/after doing your work.

The approach will not work well in the main form (use the TApplication events in it).

buho (A).
 
Thank you for the response. That's exactly what I needed.
 
I found that this does what I wanted it to do, for the most part.
Code:
procedure TfrmDates.WMSize(var Msg : TWMSIZE);
begin
  if msg.SizeType=SIZE_MINIMIZED then
    self.hide;
end;

I'm now having a bit of trouble getting the form to be on top after restoring it though. This worked fine before as I would just use form.bringtofront. For some reason this has stopped working since implementing this code. Odd.
 
Well nevermind about that focus problem. It's a little goofy but it seems to work for the most part.

Thanks again for the help.
 
Try calling "inherited" in your message handler. Dunno if it will work but worth a try.

buho (A).
 
It's being very strange. I tried using inherited, but it didn't change much.

I have 2 ways of restoring the form once it's been hidden. I have a tray icon that I can either right click, and get a menu which has the option to restore the form and I'm using the left click to simply restore the form immediately. I use the same code for both occasions. I just use form.show, form.bringtofront. This works from the right click menu I've made, but not when using the left click. I can't imagine why they would be different. It seems to work fine when I'm not sure this code to hide the form on minimize.

Since adding inherited, I had to add form.windowstate:=wsNormal to get the form to restore to the screen. But other than that everything is behaving the same.
 
Can you explain a little more? Is your form a systray app main form? A child form in a systray app?

What left click are you talking about? Where you are left clicking? In the systray icon?

buho (A).
 
Sorry, you told me the form is a child form before.

Popping child forms in a systray app is a messy thing. Your best way is to "disconnect" the main form from the systray(using Shell_NotifyIcon(NIM_DELETE, @FIconData)) and connect the child one (using Shell_NotifyIcon(NIM_ADD, @FIconData)). When the child form ends, disconnect it and reconnect the main form.

Something like this:

Disconnect the main form from the systray
Launch the child form.
Child form connects itself to systray
Child form works.
Child form disconnects itself from systray and ends.
Main form connects to systray

As all your forms need to be systray aware, the better approach is to have a systray aware ancestor, able to connect itself on creation and disconnect itself on destroying.

Let the forms minimize/retore as Win see best. Mess with minimizes/restores only as a last resource. The only thing you actually need most of the time is to keep the application hidden:

1) Call ShowWindow(Application.Handle, SW_HIDE) before the Application.Run line in the DPR.
2) Call ShowWindow(Application.Handle, SW_HIDE) every time after showing or hidding you form.

buho (A).
 
Thank you for your help.

I think I'm going to adjust the way the program works a little. I was keeping the main form hidden because I didn't want the user to be able to accidentally close out of the program (it needs to be running to advise the user of when something becomes due). I have a funtion to close the application via the pop-up menu from the systray.

Can you see a problem with using the following to stop the closing of the main form from terminating the program?
Code:
procedure WMClose(var Msg : TWMCLOSE); message WM_CLOSE;

procedure TMainform.WMClose(var Msg : TWMCLOSE);
begin
  self.Hide;
end;

Thanks again for all of your help.



 
Can't say, I've never opened a non-modal child form in a systray app... and I'm not gonna make it if i can use any other approach. :) :)

Anyway, you have to options: you can use Hide or Visible := False. Depending on how you are managing the systray communication you may or may not need to call ShowWindow(Application.Handle, SW_HIDE) if the application taskbar button appears.

As a last resort, you can disable your main form close button while the child form is opened.

buho (A).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top