实现思路
获取需要被置顶的窗体Handle,将其置顶
int handle = FindWindow(null, "排号窗口"); QueueHandle = (int)mystr.dwData; //通过句柄 ShowWindowAsync((IntPtr)handle, 1);
//方式二:或使用如下方式:
//设置窗体状态为默认大小
this.WindowState = FormWindowState.Normal;
//不显示在任务栏
this.ShowInTaskbar = false;
//方式三
[DllImport("user32 ")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
SetParent(this.Handle, (IntPtr)handle);
方式四
this.TopMost = true;
注意:具体使用需根据实际场景决定(可能需要配合使用iiiii)...
具体实现如下:
#region 获取消息 /// <summary> /// windowapi 通过句柄显示或隐藏窗体函数 /// </summary> /// <param name="hWnd">窗体句柄</param> /// <param name="cmdShow">显示类型(0:隐藏窗体,1:默认大小窗体,2:最小化窗体,3:最大化窗体)</param> /// <returns>返回成功或失败</returns> [DllImport("user32.dll", EntryPoint = "ShowWindowAsync", SetLastError = true)] public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
/// <summary> /// 0x004A /// </summary> const int WM_COPYDATA = 0x004A; //[StructLayout(LayoutKind.Sequential)] public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } protected override void DefWndProc(ref System.Windows.Forms.Message m) { try { switch (m.Msg) { // 接收自定义消息 USER,并显示其参数 case WM_COPYDATA: COPYDATASTRUCT mystr = new COPYDATASTRUCT(); Type mytype = mystr.GetType(); mystr = (COPYDATASTRUCT)m.GetLParam(mytype); int handle = FindWindow(null, "排号窗口"); QueueHandle = (int)mystr.dwData; //通过句柄,最大化窗体 ShowWindowAsync((IntPtr)handle, 1); ReceiveMessage = mystr.lpData; ExecuteQueueFlowEvent(); break; default: base.DefWndProc(ref m); break; } } catch { return; } } #endregion
参考:https://www.cnblogs.com/xielong/p/6705243.html?utm_source=itdadao&utm_medium=referral ——内容很全面谢谢分享