1 自定义Windows消息
internal class WindowMessageSendService
{
pravite const string WindowsTitle="灵犀助手";
/// <summary>
/// 发送显示灵犀助手消息
/// </summary>
public static void SendShowLinseerCopilot()
{
var handle = FindWindow(null, WindowsTitle);
if (handle != IntPtr.Zero)
{
SendMessage(handle, ShowLinseerCopilotMessage, new IntPtr(0), new IntPtr(0));
CommService.Logger.Info($"发送激活显示灵犀助手消息: {ShowLinseerCopilotMessage}");
}
}
/// <summary>
/// 发送显示Ai会议
/// </summary>
public static void SendShowAiMeetingMessage()
{
var handle = FindWindow(null,WindowsTitle);
if (handle != IntPtr.Zero)
{
SendMessage(handle, ShowAiMeetingMessage, new IntPtr(0), new IntPtr(0));
CommService.Logger.Info($"发送激活显示AI会议: {ShowAiMeetingMessage}");
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern uint RegisterWindowMessage(string lpString);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
public static readonly int ShowLinseerCopilotMessage = (int)RegisterWindowMessage("ShowLinseerCopilotMessage");
//显示AI会议
public static readonly int ShowAiMeetingMessage = (int)RegisterWindowMessage("ShowAiMeetingMessage");
}
2 应用订阅windows消息
public class WindowsManager
{
public void InitHook()
{
var hwnd = new WindowInteropHelper(Application.Current.MainWindow).EnsureHandle();
var hwndSource = HwndSource.FromHwnd(hwnd);
if (hwndSource != null)
{
hwndSource.AddHook(new HwndSourceHook(WndProc));//挂钩
}
else
{
CommService.Logger.Error("获取不到窗口句柄");
}
}
/// <summary>
/// 钩子函数
/// </summary>
/// <param name="hwnd"></param>
/// <param name="msg"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <param name="handled"></param>
/// <returns></returns>
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WindowMessageSendService.ShowLinseerCopilotMessage)
{
}
else if (msg == WindowMessageSendService.ShowAiMeetingMessage)
{
}
return hwnd;
}
}
3 发送消息
/// <summary>
/// 激活上一个灵犀应用
/// </summary>
/// <returns></returns>
public void NotifyExistLinseerCopilotApp(StartupEventArgs e)
{
var existLinSeerCopilots = Process.GetProcessesByName(“LinseerCopilot”);
CommService.Logger.Info($"获取到{CustomText.ProjectName}进程有{existLinSeerCopilots.Length}个");
var current = Process.GetCurrentProcess();
foreach (var linSeerCopilot in existLinSeerCopilots)
{
try
{
if (linSeerCopilot.Id != current.Id)
{
_logger.Info($"已有灵犀助手进程,激活:{linSeerCopilot.Id}");
if (e.Args.Length > 0 && !string.IsNullOrWhiteSpace(e.Args[0]))
{
if (e.Args[0].Equals(ExternalStartup.ControlCenterAiMeeting, StringComparison.CurrentCultureIgnoreCase))
{
WindowMessageSendService.SendShowAiMeetingMessage();
break;
}
}
WindowMessageSendService.SendShowLinseerCopilot();
}
}
catch (Exception ex)
{
_logger.Error(ex);
}
}
if (existLinSeerCopilots.Length > 1)
{
_logger.Info($"已有灵犀助手进程,杀掉当前进程:{current.Id}");
Application.Current.Shutdown();
}
}