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

2

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?

Pietrek answered 3/6, 2011 at 15:14 Comment(9)
Hey, Microsoft added the taskbar in Windows 95 - it fits! (Really it applies to all OS's from Windows 95 - Windows 7)Pietrek
Well okay, then that tag is there of historical reasons and not due to this app needing to be compatible with W95, right? :)Rosina
@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.)Peril
I know about corporate environments and still consider @Cobra_Fast's "Lol" appropriate ;)Mateusz
@Rosina 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 7Pietrek
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?Highup
@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!Tang
@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.Pietrek
@Ian There are two great pages on MSDN that I hope you know of: msdn.microsoft.com/en-us/library/ms632599(VS.85).aspx and msdn.microsoft.com/en-us/library/cc144179(VS.85).aspxHighup
P
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)

Preponderant answered 3/6, 2011 at 15:24 Comment(4)
Well holy, that works! Thank you! But i'm still curious what Windows was doing when it "minimized" my window - but not really.Pietrek
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.Preponderant
@Ian Boyd: You may also want to check the fully worked out version as added to your other question.Preponderant
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.Henka
D
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

Damnation answered 19/3, 2020 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.