加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将消息发送到Windows进程(而不是其主窗口)

发布时间:2020-12-15 04:20:15 所属栏目:百科 来源:网络整理
导读:我有一个应用程序,在随后的一个检测是否有一个进程的同名已经运行,如果是,激活正在运行的应用程序的窗口,然后退出. 问题是主窗口可能被隐藏(只有一个通知区域图标可见),从而让我没有窗口句柄. 在启动时,上一个实例的MainWindowHandle属性为0,所以我不能发送S
我有一个应用程序,在随后的一个检测是否有一个进程的同名已经运行,如果是,激活正在运行的应用程序的窗口,然后退出.

问题是主窗口可能被隐藏(只有一个通知区域图标可见),从而让我没有窗口句柄.

在启动时,上一个实例的MainWindowHandle属性为0,所以我不能发送ShowWindow或PostMessage.

有什么办法可以发送可以被运行的应用程序拦截的消息,从而允许它显示其主窗口?

该应用程序是用C#编写的,我正在使用的代码来实现这一点.

[STAThread]
static void Main()
{
    bool createdNew = true;
    using (Mutex mutex = new Mutex(true,"MyMutexName",out createdNew))
    {
        if (createdNew)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
        else
        {
            Process current = Process.GetCurrentProcess();
            foreach (Process process in Process.GetProcessesByName(current.ProcessName))
            {
                if (process.Id != current.Id)
                {
                    Interop.WINDOWINFO pwi = new Interop.WINDOWINFO();
                    IntPtr handle = process.MainWindowHandle;
                    var isVisible = Interop.GetWindowInfo(handle,ref pwi);
                    if (!isVisible)
                    {
                        MessageBox.Show(Constants.APP_NAME + " is already running,check the notification area (near the clock).",Constants.APP_NAME,MessageBoxButtons.OK,MessageBoxIcon.Information);//temporary message,until I find the solution
                        //Interop.ShowWindow(handle,Interop.WindowShowStyle.ShowNormal);
                        //Interop.PostMessage(handle,Interop.WM_CUSTOM_ACTIVATEAPP,IntPtr.Zero,IntPtr.Zero);
                    }
                    else
                        Interop.SetForegroundWindow(handle);//this works when the window is visible
                        break;
                    }
                }
            }
        }
    }
}

解决方法

这是我如何做到这一点:
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
public partial class MainForm : Form
{
    #region Dll Imports
    private const int HWND_BROADCAST = 0xFFFF;

    private static readonly int WM_MY_MSG = RegisterWindowMessage( "WM_MY_MSG" );

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

    [DllImport( "user32" )]
    private static extern int RegisterWindowMessage(string message);
    #endregion Dll Imports
    static Mutex _single = new Mutex(true,"{4EABFF23-A35E-F0AB-3189-C81203BCAFF1}");
    [STAThread]
    static void Main()
    {
        // See if an instance is already running...
        if (_single.WaitOne(TimeSpan.Zero,true)) {
            // No...start up normally.
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try {
                Application.Run(new MainForm());
            } catch (Exception ex) {
                // handle exception accordingly
            } finally {
                _single.ReleaseMutex();
            }
        } else {
            // Yes...Bring existing instance to top and activate it.
            PostMessage(
                (IntPtr) HWND_BROADCAST,WM_MY_MSG,new IntPtr(0xCDCD),new IntPtr(0xEFEF));
        }
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_MY_MSG) {
            if ((m.WParam.ToInt32() == 0xCDCD) && (m.LParam.ToInt32() == 0xEFEF)) {
                if (WindowState == FormWindowState.Minimized) {
                    WindowState = FormWindowState.Normal;
                }
                // Bring window to front.
                bool temp = TopMost;
                TopMost = true;
                TopMost = temp;
                // Set focus to the window.
                Activate();
            }
        } else {
            base.WndProc(ref m);
        }
    }
}

我希望我已经正确地转录了.我不得不放弃很多其他的东西,但我认为我有必要的.我为我工作,没有失败.如果你有问题,让我知道,我会看看我错过了什么.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读