How to minimize a window to the taskbar? (i.e. not iconify)

21,371

That icon on the taskbar is the icon of the Application (Handle) rather than that of the MainForm.

Use:

Application.Minimize;

Edit: But out of both your links, I understand you knew that already...duh ;)

This works for the MainForm:

TForm1 = class(TForm)
private
  procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
protected
  procedure CreateParams(var Params: TCreateParams); override;

...

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  with Params do
  begin
    ExStyle := ExStyle or WS_EX_APPWINDOW;
    WndParent := GetDesktopWindow;
  end;
end;

procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
begin
  if Msg.CmdType = SC_MINIMIZE then
    ShowWindow(Handle, SW_MINIMIZE)
  else
    inherited;
end;

And to hide Application.Handle from the taskbar (to only have a taskbar icon for the MainForm): set the Visible property of this Form to True and hide the Application in the project file:

Application.Initialize;
Application.CreateForm(TForm1, Form1);
ShowWindow(Application.Handle, SW_HIDE);
Application.Run;

For this form, ShowWindow(Handle, SW_MINIMIZE); shóuld work. It also provides for the default zooming-feature of Windows when minimizing or restoring.

(Tested with D5 & D7 on XP and W7)

Share:
21,371
mistertodd
Author by

mistertodd

Any code is public domain. No attribution required. జ్ఞా <sup>🕗</sup>🕗 Yes, i do write i with a lowercase i. The Meta Stackexchange answer that I am most proud of

Updated on March 19, 2020

Comments

  • mistertodd
    mistertodd about 4 years

    i have a window that i want to minimize (to the taskbar), so i call ShowWindow:

    ShowWindow(Handle, SW_MINIMIZE);
    

    Except that rather than minimizing itself (to the taskbar), the window is iconified:

    enter image description here

    The window is unparented:

    enter image description here

    How do i minimize a window to the taskbar?


    Update:

    Following some advice from 2002, i try setting the WS_EX_APPWINDOW window style and/or ensuring the window has no owner:

    enter image description here

    Unfortunately, that changes the behavior of my (Delphi) application because there is now two taskbar icons for my application, rather than one:

    enter image description here

    This, of course, is an artifact of Delphi (5); and because i was trying to solve another issue.

    But that shouldn't affect this question. i'm calling the ShowWindow(..., SW_MINIMIZE) API, and rather than minimize the window Windows is iconifying the application.

    How do i minimize a window to the taskbar?

  • mistertodd
    mistertodd almost 13 years
    Well holy, that works! Thank you! But i'm still curious what Windows was doing when it "minimized" my window - but not really.
  • NGLN
    NGLN almost 13 years
    WinSDK about WS_EX_APPWINDOW: Forces a top-level window onto the taskbar when the window is minimized. So I think, and know for pretty sure, that "normal" minimization (without this extended window style) is like minimizing a MDIChild in a MDI App: in this case the Windows Desktop is your MDIForm.
  • NGLN
    NGLN almost 13 years
    @Ian Boyd: You may also want to check the fully worked out version as added to your other question.
  • user1937012
    user1937012 over 3 years
    Works on Windows 10 as well, this is what I was looking for . Visually Word, Excel does the same thing, when I open multiple Documents.