using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace MyDb { /// /// Win32 API /// public class RyWin32 { /// /// /// /// /// /// [DllImport("USER32.DLL")] public static extern int GetSystemMenu(IntPtr hwnd, int bRevert); /// /// /// /// /// /// /// [DllImport("USER32.DLL")] public static extern int RemoveMenu(int hMenu, int nPosition, int wFlags); /// /// /// /// /// [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); /// /// 获取当前前台窗口句柄 /// /// [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetForegroundWindow(); /// /// 获得当前活动窗体 /// /// [DllImport("user32.dll")] public static extern IntPtr GetActiveWindow();//获得当前活动窗体 /// /// 设置活动窗体 /// /// /// [DllImport("user32.dll")] public static extern IntPtr SetActiveWindow(IntPtr hwnd);//设置活动窗体 /// /// 获取类名 /// /// /// /// /// [DllImport("user32", EntryPoint = "GetClassName", SetLastError = false, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] public static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount); /// /// 获取当前线程对应的进程ID /// /// /// /// [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID); /// /// 判断指定句柄是否是一个窗口 /// /// /// [DllImport("user32.dll", EntryPoint = "IsWindow")] public static extern bool IsWindow(IntPtr hWnd); /// /// 获取窗口标题 /// /// /// /// /// [DllImport("user32", SetLastError = true)] public static extern int GetWindowText( int hWnd,//窗口句柄 StringBuilder lpString,//标题 int nMaxCount //最大值 ); /// /// 查找窗口 /// /// /// /// [DllImport("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); /// /// 设置父窗口 /// /// /// /// [DllImport("user32.dll")] public static extern int SetParent(IntPtr hWndChild, IntPtr hWndParent); /// /// 移动窗口 /// /// /// /// /// /// /// /// [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint); /// /// /// /// /// /// /// /// /// [DllImport("User32.dll")] public extern static IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam); /// /// /// /// /// /// /// [DllImport("User32.dll")] public static extern IntPtr SetWindowLong(IntPtr hwnd, int nIndex, IntPtr dwNewLong); /// /// /// public const int GWL_WNDPROC = (-4); /// /// 结束进程 /// /// /// /// [DllImport("kernel32")] public static extern long TerminateProcess(int handle, int exitCode); /// /// 发送消息 /// /// /// /// /// /// [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage(IntPtr hwnd, int msg, int wParam, ref COPYDATASTRUCT IParam); /// /// 发送消息 /// /// /// /// /// [DllImport("user32.dll")] public static extern void PostMessage(IntPtr hWnd, int msg, int wParam, ref COPYDATASTRUCT IParam); /// /// 获取窗口位置和大小 /// /// /// /// [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); /// /// 矩形 /// [StructLayout(LayoutKind.Sequential)] public struct RECT { /// /// 最左坐标 /// public int Left; /// /// 最上坐标 /// public int Top; /// /// 最右坐标 /// public int Right; /// /// 最下坐标 /// public int Bottom; } /// /// WM_COPYDATA消息的主要目的是允许在进程间传递只读数据。 /// public const int WM_COPYDATA = 0x004A; /// /// 系统通道ID /// public const int Sys_chanel_id = 1000; /// /// 用户通道ID /// public const int User_chanel_id = 1001; /// /// /// public struct COPYDATASTRUCT { /// /// /// public IntPtr dwData; /// /// /// public int cData; /// /// /// [MarshalAs(UnmanagedType.LPStr)] public string lpData; } /// /// 获取消息 /// /// /// /// public static string GetMsg(Message m,out IntPtr handle) { COPYDATASTRUCT cdata = new COPYDATASTRUCT(); Type mytype = cdata.GetType(); cdata = (COPYDATASTRUCT)m.GetLParam(mytype); handle = cdata.dwData; return cdata.lpData; } /// /// 发送消息 /// /// /// /// /// /// public static int SendMsg(IntPtr from_handle, IntPtr to_handle, int wParam, string str) { byte[] arr = System.Text.Encoding.Default.GetBytes(str); int len = arr.Length; COPYDATASTRUCT cdata; cdata.dwData = from_handle; cdata.lpData = str; cdata.cData = len + 1; return SendMessage(to_handle, WM_COPYDATA, wParam, ref cdata); } /// /// 设置父窗口 /// /// /// /// /// public static bool SetParentWin(IntPtr sub_form, IntPtr parent_form,Size size) { SetParent(sub_form, parent_form); MoveWindow(sub_form, 0, 0, size.Width, size.Height, false); return true; } } }