• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

Minimize main form while non main forms remain visible

Hello expert,
  I have the following problem.
  My program has two forms, Form1 (main form) and Form2 (template form). On Form1 I have a button that assigns to a TForm2 type of variable, named MyForm, the template form using the code below:
I then assign the MyForm variable to a dynamic array of TForm2 type for later access. This works just fine. I click the button and I have a new "instance" of MyForm. Each form can from now on have it's own different settings. The problem is that when I minimize Form1 all Form2s that I've just created, minimize as well. What must I do to have them still visible and usable while Form1 is minimized? If a solution is found will it still work if I minimize Form1 to the windows tray?
                                     Thank you in advance.
1 Solution
The reason this happens is because your first form is the main application form.
So if you minimise the main form, you are minimising the entire application and not just the form.

You might be able to get round this if you created your second form from say a dll containing the code to create the second form.
This way you dont have to set the handle for the second form to be the main application. This though would open a new taskbar item for each second form unfortunately.

Other than that I cant think of another solution.
You may play with another setup: add a new empty form to the project, set position to desighed and left to -1000, and create it in the DPR file before your real main form. This will be the invisible main form for your application.

IMHO, windows requires that each application has the main form and is minimized when this form is minimized. Any attempt to bypass that creates features that some users will find strange/ugly. So, try to redesign you application: make a small main form, aother form with the controls that you currently have on your main one, and remove minimize buttons from all forms but the main one. This is how Delphi looks, right?
ZhaawZSoftware DeveloperCommented:
Do you minimize From1 or whole application?

This worked fine for me:
Form1.WindowState := wsMinimized;
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

ZhaawZSoftware DeveloperCommented:
mikelittlewood, minimizing "main form" will not minimize whole application. Application has it's own window. If you're minimizing application's window (not a 'main form') by using application.minimize, every form will dissapear, however, if you minimize just a first form, it won't affect other forms.

About minimizing to tray... This is not actually minimizing of a window. "Minimizing to tray" can be done by placing an icon to tray and hidding a window.
ZhaawZSoftware DeveloperCommented:
Octabun, your sollution with "left := -1000" never should be used!
If user has multiple monitors (like I do), these windows still can be visible.
If you want to hide a window, you should use ShowWindow(handle_of_the_window, sw_hide) instead of changing window's position.
Did you test that with DefaultMonitor=dmDesktop, position=poDesigned on multiple monitors? I do not have the second monitor, so I just read help. My understanding is that the form should not be visible regardless of the number of monitors.

If you have multiple monitors, please test top = -1000 too.

Where ShowWindow(handle_of_the_window, sw_hide) should be used? The form is created invisible anyway, Application shows it because it is the main form. How can I know that all application attempts to show the main form are handled? Same for the next VCL revision?
ZhaawZSoftware DeveloperCommented:
Octabun, changing position to hide window is so preposterous!
With multiple monitors, coordinates are calculated a bit different. For example, one of monitors can have coordinates from (-1280, -1024) to (0, 0) instead of (0, 0) to (1280x1024) - in such case changing top and left values to -1000 will not help. These coordinates can bet different on every PC.
Changing TForm.Position value will have no effect if you're manually changing left and top values. TForm.DefaultMonitor also won't have any effect.

ShowWindow() with SW_HIDE as nCmdShow parameter should be used whenever you want to hide some window. You may use it also to show, minimize window or do some other actions. Take a look at:
If you want to hide or show some form in your project (not external application), changing visibility property or using Hide and Show procedures also can be used.

If you want to check if some window is visible, you may use IsWindowVisible() function.
case boolean(IsWindowVisible(form1.Handle)) of
  true : ShowMessage('window IS visible');
  false : ShowMessage('window IS NOT visible');

>> How can I know that all application attempts to show the main form are handled?
As far as I know, application (by default) forces showing main form only when it starts.
If you want to avoid showing form when application starts, go to project --> view source and add this line:
  Application.ShowMainForm := false;
some where before application.run.
Wow! It is hard to believe in such things, but I guess I should since it is very much the Microsoft way...  Thanks for the warning!

So, it is necessary to try something better. If Form2 is created like

  Application.CreateForm(TForm1, Form1);
  Form2 := TForm2.CreateParented(GetDesktopWindow);
  SetWindowLong(Form2.Handle, GWL_EXSTYLE, GetWindowLong(Form2.Handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW);
  Form2.Visible := true;

it does not show up in the task bar and is not minimized when Form1, which is the main, is minimized.

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Accept: ZhaawZ {http:#13749321}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

EE Cleanup Volunteer

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now