0

I use Hardcodet.Wpf.TaskbarNotification to create tray menu, but i have some trouble with it, how can i open my app(process) from other process if it was hide to tray

I used this methods, but had not luck

    [DllImport("user32.dll")]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll")]
    public static extern IntPtr SetFocus(HandleRef hWnd);

    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int cmd);

I have been add events Activated to MainWindow, and GotFocus, Loaded to TaskbarIcon(TaskBar WPF Element), but those methods(from user32) didn't trigger any events (it works when app is minimized, but not when is hide to tray)

Any minds what i need to do ?

Update:

It's not duplicate, there are described how to use Mutex in the original post, i already done it. I need "bring to front", Activate or Trigger any event from other instance of my app, when the first instance is in tray There are just show MessageBox when the second instance created, and the first instance minimized, that post is easy to do, i have more harder task than described in the original post

Vadym Buhaiov
  • 167
  • 1
  • 15
  • Applications don't hide in the tray. The tray icon is a resource just like the form itself. The application still runs but its form is hidden or there may not be a form at all. Just display the form you want in response to the menu click, the same way you would in response to a button or menu click, eg with `Show()` or `ShowDialog()`. – Panagiotis Kanavos Jun 13 '18 at 10:42
  • If you created a form and *minimized* it, restore it. No need to call any interop methods or handle any events – Panagiotis Kanavos Jun 13 '18 at 10:42
  • @PanagiotisKanavos i need bring main window of process(1) to front from other process(2), when process (1) is moved to tray by `Hardcodet.Wpf.TaskbarNotification` library – Vadym Buhaiov Jun 13 '18 at 10:46
  • @PanagiotisKanavos ok, i need trigger any event of my app from other process, how to do it ? – Vadym Buhaiov Jun 13 '18 at 10:50
  • Nothing is "moved" to the tray. Hardcodet.Wpf.TaskbarNotification *creates* a tray icon and menu for an application. No other process is needed. The **same** process should contain the taskbar component and the forms you want to use – Panagiotis Kanavos Jun 13 '18 at 10:50
  • @PanagiotisKanavos i have two processes, both are wpf, one has only icon in a tray(not in taskbar), how my the second process can trigger any event of first process ? – Vadym Buhaiov Jun 13 '18 at 10:54
  • Don't use two processes. There's no such thing as events anyway, it's a programming construct. Processes communicate with each other through an interprocess communication mechanism like sockets. The windowing system sends window messages to *windows* (not the applications themselves). – Panagiotis Kanavos Jun 13 '18 at 10:58
  • BTW the control's [tutorial](https://www.codeproject.com/Articles/36468/WPF-NotifyIcon) already shows how to use it. And yes, that WPF application uses the *tray icon* as it's main form. Nothing prevents it from opening other forms in response to menu events – Panagiotis Kanavos Jun 13 '18 at 11:00
  • @PanagiotisKanavos ok, I describe all problem. I can run one app(executable file), twice(more), it's create two(more) instance of app, i use mutex to detect, is any instance already running, and when the second instance called, i need to bring first instance to front – Vadym Buhaiov Jun 13 '18 at 11:05
  • This has nothing to do with tray icons then. What you described in the question has nothing to do with processes though. I *have* used that particular component with WPF applications to open up dialog boxes and menus in response to events, in a *single* application. I also had to communicate with a service - that required IPC – Panagiotis Kanavos Jun 13 '18 at 12:00
  • You **CAN'T** raise UI events if there is no window. There are no events in reality. *Window messages* are translated into events by .NET. No window, no target for those events – Panagiotis Kanavos Jun 13 '18 at 12:03
  • What are you *really* trying to achieve? Don't describe how you think it would work. In Windows 10 for example the tray icons themselves aren't used that much. There are many ways for [cross-app communication](https://blogs.windows.com/buildingapps/2015/09/22/using-cross-app-communication-to-make-apps-work-together-10-by-10/) that don't have the security requirements of the [IPC mechanisms](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365574%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396). – Panagiotis Kanavos Jun 13 '18 at 12:12
  • If you check the cross-app options, you can have the OS *start* another app using deep linking. You can pass files to it. Or you can exchange data between apps using app services. – Panagiotis Kanavos Jun 13 '18 at 12:13

1 Answers1

0

From another process? Without elevation you will probably have a hard time bringing your app to the foreground. Read the "Remarks" section here.

l33t
  • 18,692
  • 16
  • 103
  • 180
  • I need not exactly make my app as foreground, i need trigger any event or method of my app, from another instance of app (read comments to post) – Vadym Buhaiov Jun 13 '18 at 11:39
  • @VadimBugaiov you keep repeating how you assume the solution will look like. There are no events to begin with. If you want IPC, use an IPC mechanism. If you want to display a form though, *don't* use two processes. If you really-really want the tray process to call another process, build and use an IPC mechanism - WCF, sockets, TcpListener, shared memory. – Panagiotis Kanavos Jun 13 '18 at 12:01
  • Indeed, IPC is the key here. Perhaps `HWND_BROADCAST` is a good start. https://stackoverflow.com/questions/14506406/wpf-single-instance-best-practices – l33t Jun 13 '18 at 12:03
  • @l33t if the target app doesn't have a window there's nothing to *receive* that message. That's why [this isn't included in the list of IPC mechanisms](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365574%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396). Other IPC mechanisms are better although they may have security restrictions. Besides, Windows 10 allows applications to communicate in other, [far easier](https://blogs.windows.com/buildingapps/2015/09/22/using-cross-app-communication-to-make-apps-work-together-10-by-10/) ways – Panagiotis Kanavos Jun 13 '18 at 12:08
  • @l33t i already done it with `HWND_BROADCAST` is possible to do like i describe above via processes ? – Vadym Buhaiov Jun 13 '18 at 12:18