简体   繁体   中英

How to use WinAPI SendMessage (Or Equivalent) On a Process

My working environment: C#, .NET 4 and VS2012

I have a problem with an app. It runs by showing a NotifyIcon in the system tray. When users simply click on the icon, it pops up a new Window and shows important information.

Under normal circumstances users simply click on the icon and it brings up the new Window. However, we are looking to implement an alternative Windows shell that will not have a system tray area, and hence the app will have no NotifyIcon on which to click!

I've already tested when running the alternative shell. If I use WinSpy I can see the process running (with the same two listed Windows under it), even though there is no system tray.

I need to create an app to address this problem. Is there a way to connect to the process and simulate the user clicking on the app's system tray NotifyIcon, which should then cause the new Window to pop up...even while inside the alternative shell (which does not even have a system tray!?)

Or does anyone have an alternative solution?

Have a look at the RegisterWindowMessage (Defines a new window message that is guaranteed to be unique throughout the system. The message value can be used when sending or posting messages.)

The RegisterWindowMessage function is typically used to register messages for communicating between two cooperating applications.

If two different applications register the same message string, the applications return the same message value. The message remains registered until the session ends.

static public class WinApi
{
    [DllImport("user32")]
    public static extern int RegisterWindowMessage(string message);

    public static int RegisterWindowMessage(string format, params object[] args)
    {
        string message = String.Format(format, args);
        return RegisterWindowMessage(message);
    }
}

register the message before starting the application

public class Program
{
    public static readonly int WM_SHOWFIRSTINSTANCE =
        WinApi.RegisterWindowMessage("WM_SHOWFIRSTINSTANCE|{0}", "ANY_UNIQUE_STING");
    public static void Main()
    {

    }
}

In the main form of the application

protected override void WndProc(ref Message message)
{
    if (message.Msg == PROGRAM.WM_SHOWFIRSTINSTANCE) {
        //show the window
    }
    base.WndProc(ref message);
}   

To restore the window from other application

public class OtherProgram
{

    [DllImport("user32")]
    public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);            

    [DllImport("user32")]
    public static extern int RegisterWindowMessage(string message); 

    public static readonly int WM_SHOWFIRSTINSTANCE =
        WinApi.RegisterWindowMessage("WM_SHOWFIRSTINSTANCE|{0}", "ANY_UNIQUE_STING");


    public static void Main()
    {
        //public const int HWND_BROADCAST = 0xffff;
        PostMessage(
        (IntPtr)WinApi.HWND_BROADCAST, 
        WM_SHOWFIRSTINSTANCE,
        IntPtr.Zero,
        IntPtr.Zero);
    }
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM