Search code examples
windowslazarus

Hiding Taskbar Button works, but not when second form is shown


I managed to hide my winforms application taskbar button using

ShowWindow(GetParent(Form1.Handle),SW_HIDE); 

This i call on timer 1 second after the form is created. The taskbar button remain hidden through out the application usage, but until I click a button on the form to show another form, with the Form1 as the owner.

I try to use the same code to hide the second form but not able to work.

Edit: Adding more codes:

Codes in Form1:

// this fires every 1 second and works well.
procedure TForm1.scanTimerTimer(Sender: TObject);
begin
   ShowWindow(GetParent(Form1.Handle),SW_HIDE);
end;   


// when a user press Settings button on the Form1
// I open another form.
procedure TForm1.SettingsBtnClick(Sender: TObject);
    var
      settings: TSettingsForm;
    begin
      settings := TSettingsForm.Create(Form1);
      settings.Show;
    end;    

Codes in SettingsForm

// this fires every 1 second and DOESNT WORK!
procedure TSettingsForm.scanTimerTimer(Sender: TObject);
begin
   ShowWindow(GetParent(SettingsForm.Handle),SW_HIDE);
end;  

That's all there is for the codes. So when I open SettingsForm, immediately the taskbar button reappears and never disappears anymore. I want taskbar to remain hidden no matter how many other forms I open from the main form.


Solution

  • I tried a "OS specific API" for windows which is

    ShowWindow(GetParent(Form1.Handle),SW_HIDE);
    

    Which works after FormCreate, but after the main window opens a secondary window, the taskbar button reappears. So if your app just have one window, you can use this. But for multiple windows/forms app, it will not work!

    Also I tried Non OS Specific API:

    SettingsForm.ShowInTaskBar := stNever;
    

    Tried putting this in FormCreate, and also just before Show in Caller form, but still it doesn't work. The taskbar button still appears.

    Finally I found in lazarus forum the answer using a OS Specific API:

    You need to add 2 imports:

    InterfaceBase, Win32Int
    

    And put this is FormCreate:

    procedure TForm1.FormCreate(Sender: TObject);
    var
     i: integer;
     EXStyle: Long;
     AppHandle: THandle;
    begin
        AppHandle := TWin32WidgetSet(WidgetSet).AppHandle;
        EXStyle:= GetWindowLong(AppHandle, GWL_EXSTYLE);
        SetWindowLong(AppHandle, GWL_EXSTYLE, EXStyle or WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW);
    end;