//获取当前窗口句柄
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;
}