【问题标题】:How to use WinAPI SendMessage (Or Equivalent) On a Process如何在进程上使用 WinAPI SendMessage(或等效)
【发布时间】:2013-06-27 02:42:11
【问题描述】:

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

我的应用有问题。它通过在系统托盘中显示 NotifyIcon 来运行。当用户只需单击图标时,它会弹出一个新窗口并显示重要信息。

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

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

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

或者有人有其他解决方案吗?

【问题讨论】:

  • 如果用户看不到图标,他们怎么知道点击哪里?

标签: c# winapi sendmessage


【解决方案1】:

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

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

如果两个不同的应用程序注册相同的消息字符串,则 应用程序返回相同的消息值。消息还在 注册到会话结束。

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

在启动应用程序之前注册消息

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

从其他应用程序恢复窗口

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多