Minimize main form while non main forms remain visible

Posted on 2005-04-08
Last Modified: 2010-04-05
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.
Question by:DELPHIni_GR
    LVL 15

    Expert Comment

    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.
    LVL 3

    Expert Comment

    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?
    LVL 11

    Accepted Solution

    Do you minimize From1 or whole application?

    This worked fine for me:
    Form1.WindowState := wsMinimized;
    LVL 11

    Expert Comment

    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.
    LVL 11

    Expert Comment

    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.
    LVL 3

    Expert Comment

    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?
    LVL 11

    Expert Comment

    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
    LVL 3

    Expert Comment

    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.

    LVL 14

    Expert Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now