如何在进程上使用 WinAPI SendMessage(或等价物) [英] How to use WinAPI SendMessage (Or Equivalent) On a Process

查看:23
本文介绍了如何在进程上使用 WinAPI SendMessage(或等价物)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作环境:C#、.NET 4 和 VS2012

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

我在使用某个应用时遇到问题.它通过在系统托盘中显示一个 NotifyIcon 来运行.当用户只需点击该图标时,它就会弹出一个新窗口并显示重要信息.

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.

在正常情况下,用户只需单击该图标即可打开新窗口.但是,我们正在寻求实现一个没有系统托盘区域的替代 Windows shell,因此该应用程序将没有可供点击的 NotifyIcon!

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!

我已经在运行替代 shell 时进行了测试.如果我使用 WinSpy,即使没有系统托盘,我也可以看到正在运行的进程(在它下面列出了相同的两个 Windows).

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.

我需要创建一个应用程序来解决这个问题.有没有办法连接到进程并模拟用户点击应用程序的系统托盘 NotifyIcon,这应该会导致新窗口弹出......即使在替代外壳内(甚至没有系统托盘!?)

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?

推荐答案

看看RegisterWindowMessage(定义一个新的窗口消息,保证在整个系统中是唯一的.消息值可以在发送或发布消息时使用.)

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.)

RegisterWindowMessage 函数通常用于注册用于在两个协作应用程序之间进行通信的消息.

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()
    {

    }
}

在应用程序的主要形式

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);
    }
}   

这篇关于如何在进程上使用 WinAPI SendMessage(或等价物)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆