Bringing wpf App to Foreground which is running in background?

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.

wpf
process
asked on Stack Overflow Dec 14, 2018 by subham

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0