Link to home
Start Free TrialLog in
Avatar of DELPHIni_GR
DELPHIni_GR

asked on

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:
  MyForm:=TForm2.Create(Application);
  MyForm.Show;
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.
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of Octabun
Octabun

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?
ASKER CERTIFIED SOLUTION
Avatar of ZhaawZ
ZhaawZ
Flag of Latvia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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?
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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/showwindow.asp
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');
end;

>> 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.Initialize;
  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;
  Application.Run;

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.

cwwkie
EE Cleanup Volunteer