Bas Smit
Bas Smit

Reputation: 685

lauch c# app from c++ without taskbar button

I am looking for a way to launch a c# app from a c++ app without showing a taskbar button for the c# app until I choose to show it.

I have a class that can hide and show a taskbar button, however the taskbar button shows up very briefly when launching the c# app.

Is there a way to launch a c# app without the os creating a taskbar button, while still allowing me to show a taskbar button later on?

Here is the code Im currently using to hide and show a taskbar button.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program
{
    static void Main()
    {
        var tb = new Taskbar();
        tb.DeleteTab();
        bool hidden = true;

        while (true)
        {
            Console.WriteLine("Press any key to toggle taskbar button");
            Console.ReadKey();
            Console.Clear();

            if (hidden)
                tb.AddTab();
            else
                tb.DeleteTab();

            hidden = !hidden;
        }
    }
}

class Taskbar
{
    public void AddTab()
    {
        GetTaskbarList().AddTab(GetMainWindowHandle());
    }

    public void DeleteTab()
    {
        GetTaskbarList().DeleteTab(GetMainWindowHandle());
    }

    ITaskbarList GetTaskbarList()
    {
        var taskbarList = (ITaskbarList)new CoTaskbarList();
        taskbarList.HrInit();
        return taskbarList;
    }

    IntPtr GetMainWindowHandle()
    {
        return Process.GetCurrentProcess().MainWindowHandle;
    }

    [ComImport]
    [Guid("56fdf344-fd6d-11d0-958a-006097c9a090")]
    class CoTaskbarList
    {
    }

    [ComImport,
    Guid("56fdf342-fd6d-11d0-958a-006097c9a090"),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface ITaskbarList
    {
        /// <summary>
        /// Initializes the taskbar list object. This method must be called before any other ITaskbarList methods can be called.
        /// </summary>
        void HrInit();

        /// <summary>
        /// Adds an item to the taskbar.
        /// </summary>
        /// <param name="hWnd">A handle to the window to be added to the taskbar.</param>
        void AddTab([In] IntPtr hWnd);

        /// <summary>
        /// Deletes an item from the taskbar.
        /// </summary>
        /// <param name="hWnd">A handle to the window to be deleted from the taskbar.</param>
        void DeleteTab([In] IntPtr hWnd);

        /// <summary>
        /// Activates an item on the taskbar. The window is not actually activated; the window's item on the taskbar is merely displayed as active.
        /// </summary>
        /// <param name="hWnd">A handle to the window on the taskbar to be displayed as active.</param>
        void ActivateTab([In] IntPtr hWnd);

        /// <summary>
        /// Marks a taskbar item as active but does not visually activate it.
        /// </summary>
        /// <param name="hWnd">A handle to the window to be marked as active.</param>
        void SetActiveAlt([In] IntPtr hWnd);
    }
}

Upvotes: 0

Views: 449

Answers (2)

Kendall Frey
Kendall Frey

Reputation: 44316

Using:

this.ShowInTaskbar = false;

in your Form's constructor (or setting it in designer, if possible) should ensure that no taskbar button is initially created. I assume that you should still be able to create one when necessary.

Upvotes: 0

ChrisF
ChrisF

Reputation: 137108

Set the ShowInTaskbar (WinForms or WPF) property.

Set it to false initially and the application won't appear.

Set it to true when you want the application to appear in the taskbar.

Upvotes: 1

Related Questions