1

I have created a WPF app. On closing Window (By Clicking on X at Title bar), I am hiding the App and we can relaunch or exit the App from tray icon.

        NotifyIcon notifyIcon = new NotifyIcon();
        string a = Properties.Resources.SuccessTitle;
        notifyIcon.Icon = Properties.Resources.pencil;
        notifyIcon.ContextMenu = new ContextMenu(new MenuItem[]
            {  exitMenuItem });
        notifyIcon.Visible = true;
        notifyIcon.Text = "My App Name";
        notifyIcon.DoubleClick += notifyIcon_DoubleClick;

Notify icon is working Fine for me, i.e when I double click on tray icon App is coming to for ground. Below is the code of Double Click Event

if (window.IsVisible)
        {
            if (window.WindowState == WindowState.Minimized)
            {
                window.WindowState = WindowState.Normal;
                window.Activate();
            }
            else
            {
                window.Activate();
            }
        }
        else
        {
            window.Visibility = Visibility.Visible;
        }

When app is in maximized or minimized state, clicking on exe is working fine too.

    Process current = Process.GetCurrentProcess();
    // Enumerate through all the process resources on the share
    // local computer that the specified process name.
    foreach (Process process in
                     Process.GetProcessesByName(current.ProcessName))
      {
         if (process.Id != current.Id)

           {
                  NativeMethods.SetForegroundWindow( process.MainWindowHandle);
                  NativeMethods.ShowWindow(process.MainWindowHandle,
                           WindowShowStyle.Restore);
                  break;
           }
      }

Now, I want the same effect when clicking in exe when windows is hidden. How can I achieve it. I tried to debug the code and found that when app is hidden

MainWindowHandle= 0X00000000

and when it is visible i.e. minimized or maximized then MainWindowHandle is having some valid value.

1
  • Above answer is for minimized windows. But my app is running in background and can be launched from tray icon. In above answer, they are fetching MainWindowHandle successfully as app is in minimized. But in my case mainwindowhandle I am getting is 0x00000000 because my app is in hidden state. Please reopen it
    – subham
    Commented Dec 17, 2018 at 3:13

0

Browse other questions tagged or ask your own question.