7

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?

Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • Hey, Microsoft added the taskbar in Windows 95 - it fits! (Really it applies to all OS's from Windows 95 - Windows 7) – Ian Boyd Jun 03 '11 at 15:18
  • 1
    Well okay, then that tag is there of historical reasons and not due to this app needing to be compatible with W95, right? :) – Cobra_Fast Jun 03 '11 at 15:20
  • 2
    @Cobra_Fast: Believe it or not, there are some corporations still using Win95 because they have internal software that won't run on later versions. Not everyone upgrades immediately - you should learn more about corporate environments before ridiculing what you don't understand. (We just got rid of our last Win2K system about a year ago (everything XP SP3), and got our first Win7 machines this week.) – Ken White Jun 03 '11 at 15:42
  • 2
    I know about corporate environments and still consider @Cobra_Fast's "Lol" appropriate ;) – Jemes Jun 03 '11 at 16:00
  • @Cobra_Fast It also wouldn't be correct to just tag it with `Windows` or with just `Windows-7`, since the question only applies to Windows operating systems with a taskbar: **Windows 95**-**Windows 7** – Ian Boyd Jun 03 '11 at 16:36
  • oh please, this should have windows tag and that's all. It would be better if the question had more detail of what the app is doing that means it behaves oddly. I've never seen an app do this. The default delphi app doesn't do this. What's special about your app? Why are you calling raw Windows api? What's wrong with vcl methods? – David Heffernan Jun 03 '11 at 17:02
  • @Ian Boyd: That's correct. I love Win95, I still use it every day. :P See my questions tagged 'windows-95'. @Cobra_Fast: Indeed, make this program Win95 compatible please! – Midas Jun 04 '11 at 16:19
  • @David Heffernan: In truth i *am* calling `form.WindowState := wsMinimized`, which in turn is calls `ShowWindow(..., SW_MINIMIZE)`. But i didn't want anyone blaming some 3rd party wrapper, or confuse the issue. The question is what is causing Windows to *iconify* a window, rather than *minimize it*. The answer is: "because it has an owner, and doesn't have `WS_EX_APPWINDOW` style". If you want an *owned* window to minimize to the taskbar you have to create it with `WS_EX_APPWINDOW` - that's what the style is there for. As for your "*oh please*": oh please. – Ian Boyd Jun 09 '11 at 12:29
  • 1
    @Ian There are two great pages on MSDN that I hope you know of: http://msdn.microsoft.com/en-us/library/ms632599(VS.85).aspx and http://msdn.microsoft.com/en-us/library/cc144179(VS.85).aspx – David Heffernan Jun 09 '11 at 12:33

2 Answers2

12

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)

NGLN
  • 43,011
  • 8
  • 105
  • 200
  • Well holy, that works! Thank you! But i'm still curious what Windows was doing when it "minimized" my window - but not really. – Ian Boyd Jun 03 '11 at 17:07
  • 1
    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 Jun 03 '11 at 17:17
  • 1
    @Ian Boyd: You may also want to check the [fully worked out version](http://stackoverflow.com/questions/3988151/how-to-not-have-a-mainform-in-delphi/6230024#6230024) as added to your other question. – NGLN Jun 03 '11 at 21:04
  • 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. – user1937012 Dec 01 '20 at 14:19
0

A super simple solution is to disable the minimize icon on the FORM
[Object inspector]-[Form properties]-[Border icons]-[biMinimize]
The application can still be minimized and restore by clicking on the APPLICATION icon at the taskbar

enter image description here

Community
  • 1
  • 1
Erez Amir
  • 131
  • 1
  • 6