views:

3094

answers:

5

I need to hide a Windows form from the taskbar but I can't use WS_EX_TOOLWINDOW because I need the system menu and min/max buttons on the form's title bar.

If I switch the form to a tool window at runtime the form skinning is stuffed up. From searching on the Web I see that VB has a ShowInTaskbar property and I'm wondering if this would do what I want, and whether it can be implemented in Delphi 2006. Also this project is a COM server and has no MainForm, etc.

+1  A: 

With thanks to http://www.scalabium.com/faq/dct0096.htm.

procedure TForm1.FormCreate(Sender: TObject);
begin
  ShowWindow(Application.Handle, SW_HIDE);
  SetWindowLong(Application.Handle, GWL_EXSTYLE,
    GetWindowLong(Application.Handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW);
  ShowWindow(Application.Handle, SW_SHOW);
end;

I tested it and it worked with Delphi2006. And windows menu and min/max buttons are still visible.

Gamecat
Also works in D7
Jamie
Thanks, but I was already using this code. In my question I pointed out that I didn't want to use WS_EX_TOOLWINDOW. Great to have such a quick response though :)
+3  A: 

There's an interesting discussion of this exact problem here (from a VB6 persepective).

The most relevant bit from your question's perspective is:

"When you create a window, the taskbar examines the window's extended style to see if either the WS_EX_APPWINDOW (&H40000) or WS_EX_TOOLWINDOW (&H80) style is turned on. If WS_EX_APPWINDOW is turned on, the taskbar shows a button for the window, and if WS_EX_ TOOLWINDOW is turned on, the taskbar does not show a button for the window. A window should never have both of these extended styles. If the window doesn't have either of these styles, the taskbar decides to create a button if the window is unowned and does not create a button if the window is owned."

Incidentally, you use the GetWindow API function with the GW_OWNER flag to determine whether a window is owned.

Stu Mackellar
Thanks Stu. You have shown me the problem. Even though I'm turning off both those options, the taskbar button is still showing because the form doesn't have an owner from Windows point of view. This is complicated because it is a COM plugin.
+6  A: 

Thanks to Stu for putting me on to the answer so quickly. In my case I had to manually add the owning form's handle into the CreateParams, but that may not be necessary in other/normal cases.

procedure TfrmWord2Site.CreateParams(var Params:TCreateParams);
begin
  inherited CreateParams(Params);
  Params.WndParent := <your owner form>.Handle;
  Params.ExStyle := Params.ExStyle and not WS_EX_APPWINDOW;
end;
A: 

One way to do this in C# is:

        ShowWindow(_window, SWHide);

        int style = GetWindowLong(_window, GWL_EXSTYLE);
        style |= WS_EX_TOOLWINDOW;
        SetWindowLong(_window, GWL_EXSTYLE, style);

        ShowWindow(_window, SWShow);
The question clearly states that it is looking for a NON-toolwindow solution.
Synetech inc.
A: 

Solved my problems in this area by BordersStyle bsDialog/bsToolWindow (but then I did not need the min/max...). But I wonder why you should want to combine these attributes.. Won't it confuse the 'normal' user?