WPF 程序通訊、無標題欄的視窗移動、工作區尺寸
阿新 • • 發佈:2018-11-05
//獲取當前視窗控制代碼
IntPtr handle = new WindowInteropHelper(this).Handle;
//獲取當前工作區
SystemParameters.WorkArea
class Win32Api { #region msg public const int USER = 0x0400; public const int UM_1 = USER + 1; public const int WM_COPYDATA = 0x004A; public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; #endregion #region api /// <summary> /// 程序通訊,Post Message,不接收窗體的返回值 /// </summary> /// <param name="hWnd">要通訊的視窗控制代碼</param> /// <param name="msg"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> [DllImport("user32.dll", EntryPoint = "PostMessage")] public static extern void PostMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam); /// <summary> /// 程序通訊,Send Message,接收窗體的返回值 /// </summary> /// <param name="hWnd"></param> /// <param name="msg"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> [DllImport("user32.dll", EntryPoint = "SendMessage")] public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref CopyDataStruc lParam); /// <summary> /// 根據窗體title找到視窗控制代碼 /// </summary> /// <param name="lpClassName"></param> /// <param name="lpWindowName"></param> /// <returns></returns> [DllImport("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); /// <summary> /// 獲取當前滑鼠座標 /// </summary> /// <param name="pt"></param> /// <returns></returns> [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern bool GetCursorPos(out POINT pt); [DllImport("user32.dll")] public static extern bool ReleaseCapture(); #endregion }
[StructLayout(LayoutKind.Sequential)] public struct CopyDataStruc { public IntPtr dwData; public int cbData; // 字串長度 [MarshalAs(UnmanagedType.LPStr)] public string lpData; // 字串 } public struct POINT { public int X; public int Y; public POINT(int x, int y) { this.X = x; this.Y = y; } }
無標題欄的視窗移動:
void title_MouseDown(object sender, MouseButtonEventArgs e) { Win32Api.ReleaseCapture(); IntPtr handle = new WindowInteropHelper(this).Handle; Win32Api.PostMessage(handle, Win32Api.WM_SYSCOMMAND, Win32Api.SC_MOVE + Win32Api.HTCAPTION, IntPtr.Zero); }
程序通訊 之 傳送訊息:
hWnd = Win32Api.FindWindow(null, "window1");
if (hWnd != IntPtr.Zero)
{
CopyDataStruc data = new CopyDataStruc
{
lpData = "Show",
dwData = IntPtr.Zero
};
data.cbData = System.Text.Encoding.Default.GetByteCount(data.lpData) + 1;
Win32Api.SendMessage(hWnd, Win32Api.WM_COPYDATA, 0, ref data);
KillProcess(current.Id.ToString(), current.ProcessName);
}
程序通訊 之 接收訊息:
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
(PresentationSource.FromVisual(this) as HwndSource).AddHook(new HwndSourceHook(this.WndProc));
}
IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == Win32Api.WM_COPYDATA)
{
CopyDataStruc data = (CopyDataStruc)Marshal.PtrToStructure(lParam, typeof(CopyDataStruc));
if (data.lpData == "Show")
{ /*do something*/ }
}
return hwnd;
}