異世界


2012年5月20日 星期日

綁架外部程式視窗

WIN32定義區:
   1: #region WIN32定義區
   2:        public enum ShowWindowStyles : short
   3:        {
   4:            SW_HIDE = 0,
   5:            SW_SHOWNORMAL = 1,
   6:            SW_NORMAL = 1,
   7:            SW_SHOWMINIMIZED = 2,
   8:            SW_SHOWMAXIMIZED = 3,
   9:            SW_MAXIMIZE = 3,
  10:            SW_SHOWNOACTIVATE = 4,
  11:            SW_SHOW = 5,
  12:            SW_MINIMIZE = 6,
  13:            SW_SHOWMINNOACTIVE = 7,
  14:            SW_SHOWNA = 8,
  15:            SW_RESTORE = 9,
  16:            SW_SHOWDEFAULT = 10,
  17:            SW_FORCEMINIMIZE = 11,
  18:            SW_MAX = 11
  19:        }
  20:  
  21:        public const int SWP_NOOWNERZORDER = 0x200;
  22:        public const int SWP_NOREDRAW = 0x8;
  23:        public const int SWP_NOZORDER = 0x4;
  24:        public const int SWP_SHOWWINDOW = 0x0040;
  25:        public const int WS_EX_MDICHILD = 0x40;
  26:        public const int SWP_FRAMECHANGED = 0x20;
  27:        public const int SWP_NOACTIVATE = 0x10;
  28:        public const int SWP_ASYNCWINDOWPOS = 0x4000;
  29:        public const int SWP_NOMOVE = 0x2;
  30:        public const int SWP_NOSIZE = 0x1;
  31:        public const int GWL_STYLE = (-16);
  32:        public const int WS_VISIBLE = 0x10000000;
  33:        public const int WM_CLOSE = 0x10;
  34:        public const int WS_CHILD = 0x40000000;
  35:  
  36:        public const int HWND_TOP = 0x0;
  37:        public const int WM_COMMAND = 0x0112;
  38:        public const int WM_QT_PAINT = 0xC2DC;
  39:        public const int WM_PAINT = 0x000F;
  40:        public const int WM_SIZE = 0x0005;
  41:        public const int WM_USER = 0x0400;
  42:  
  43:        
  44:        #region User_Message(自定義訊息)         
  45:        public const int SbMsgOffset = 100;
  46:  
  47:        //.....................................................................
  48:        //  WM_USER_MESSAGE_1 => 通知 OWner, __ADAM6050UDP.exe 的 HWND
  49:        //     Lparam: HWnd
  50:        //     WParam: nonuse
  51:        public const int WM_USER_MESSAGE_1 = WM_USER + SbMsgOffset;
  52:  
  53:        //.....................................................................
  54:        //  WM_USER_MESSAGE_2 => 通知 OWner, DI_Value
  55:        //     Lparam: 此 ADAM6050 的IP 在 INI檔裡的Index
  56:        //     WParam: DI_Value (16 bits)
  57:        public const int WM_USER_MESSAGE_2 = WM_USER + SbMsgOffset +1; //..通知 OWner DI_Value
  58:  
  59:  
  60:        //.....................................................................
  61:        //  WM_USER_MESSAGE_3 => 通知 OWner __IP_Scand.exe    的 HWND
  62:        //     Lparam: HWnd
  63:        //     WParam: nonuse
  64:        public const int WM_USER_MESSAGE_3 = WM_USER + SbMsgOffset +2;
  65:  
  66:  
  67:        //.....................................................................
  68:        //  WM_USER_MESSAGE_4 => 通知 OWner, IP_Ping 的結果
  69:        //     Lparam: 此 IP_Scan 的IP 在 INI檔裡的Index
  70:        //     WParam:  0:OK / 非0: ping不通
  71:        public const int WM_USER_MESSAGE_4 = WM_USER + SbMsgOffset +3;
  72:  
  73:        //.....................................................................
  74:        //  WM_USER_MESSAGE_5 => 保留未用
  75:        public const int WM_USER_MESSAGE_5 = WM_USER + SbMsgOffset +4;
  76:  
  77:        //.....................................................................
  78:        //  WM_USER_MESSAGE_6 => 通知 ADAM & IP_Scan 結束程式
  79:        public const int WM_USER_MESSAGE_6 = WM_USER + SbMsgOffset + 5;
  80:        #endregion
  81:  
  82:  
  83:        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
  84:             CharSet = CharSet.Unicode, ExactSpelling = true,
  85:             CallingConvention = CallingConvention.StdCall)]
  86:        public static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);
  87:  
  88:        [DllImport("user32.dll", SetLastError = true)]
  89:        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  90:  
  91:        [DllImport("user32.dll", SetLastError = true)]
  92:        public static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
  93:  
  94:        [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
  95:        public static extern long GetWindowLong(IntPtr hwnd, int nIndex);
  96:  
  97:        [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
  98:        public static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);
  99:        //public static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
 100:  
 101:        [DllImport("user32.dll", SetLastError = true)]
 102:        public static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);
 103:  
 104:        [DllImport("user32.dll", SetLastError = true)]
 105:        public static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
 106:  
 107:        [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
 108:        public static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam);
 109:  
 110:        [DllImport("user32.dll")]
 111:        public static extern void SetForegroundWindow(IntPtr hwnd);
 112:  
 113:        //..
 114:        [DllImport("user32.dll")]
 115:        public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
 116:  
 117:        [DllImport("user32.dll", SetLastError = true)]
 118:        public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
 119:  
 120:        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
 121:        public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
 122:  
 123:        [DllImport("user32.dll")]
 124:        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
 125:  
 126:        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
 127:        public static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong);
 128:  
 129:        [DllImport("user32.dll", CharSet = CharSet.Auto)]
 130:        public static extern bool ShowWindow(IntPtr hWnd, short State);
 131:  
 132:        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
 133:        internal static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
 134:  
 135:        [DllImport("user32.dll")]
 136:        [return: MarshalAs(UnmanagedType.Bool)]
 137:        static extern bool IsWindow(IntPtr hWnd);
 138:  
 139:        [DllImport("kernel32.dll", SetLastError = true)]
 140:        static extern int GetProcessId(IntPtr hWnd);
 141:  
 142:        internal struct RECT  //Win32彙編數據結構不用自己定義
 143:        {
 144:            public int left;
 145:            public int top;
 146:            public int right;
 147:            public int bottom;
 148:        }
 149:  
 150:  
 151:  
 152:        #endregion

綁架外部程式視窗:


   1: #region 綁架外部程式視窗
   2:        //..以名稱檢察 Processes 是否存在
   3:        public static bool isProcesses(string ProName)
   4:        {
   5:            return (Process.GetProcessesByName(ProName).Length > 1);
   6:        }
   7:        public static Process RunExe(String ExeFileName, String Arg)
   8:        {
   9:            Process p = new Process();
  10:            // 需要啟動的程序            
  11:            //p.StartInfo.mResFile = @"C:\Windows\System32\notepad.exe";            
  12:            p.StartInfo.FileName = ExeFileName;
  13:            p.StartInfo.Arguments = Arg;
  14:  
  15:  
  16:            // 最小化程序            
  17:            p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
  18:  
  19:            // 啟動           
  20:            p.Start();
  21:  
  22:            return (p);
  23:        }
  24:        public static Process RunExeAndToChid(string ExeFileName, IntPtr ParentHandle)
  25:        {
  26:            Process p = new Process();
  27:            // 需要啟動的程序            
  28:            //p.StartInfo.mResFile = @"C:\Windows\System32\notepad.exe";            
  29:            p.StartInfo.FileName = ExeFileName;
  30:  
  31:  
  32:            // 最小化程序            
  33:            p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
  34:  
  35:            // 啟動           
  36:            p.Start();
  37:  
  38:            // 必须等待,啟動程序的 Handol 未没有創建,不能控制程序             
  39:            Thread.Sleep(5000);
  40:  
  41:            // 最大化啟動程序            
  42:            ShowWindow(p.MainWindowHandle, (short)ShowWindowStyles.SW_MAXIMIZE);
  43:  
  44:            // 設置被绑架程序的父窗口            
  45:            //SetParent(p.MainWindowHandle, this.Handle);
  46:            SetParent(p.MainWindowHandle, ParentHandle);
  47:  
  48:            // 改變尺寸            
  49:            ResizeControl(p, ParentHandle);
  50:  
  51:            return (p);
  52:        }
  53:  
  54:  
  55:        // 控制嵌入程序的位置和尺寸
  56:        public static void ResizeControlEMS(Process p, IntPtr ParentHandle)
  57:        {
  58:            int TopOffset = -54;//-28;
  59:            int LeftOffset = -8;
  60:  
  61:            SendMessage(p.MainWindowHandle, WM_COMMAND, WM_PAINT, 0);
  62:            PostMessage(p.MainWindowHandle, WM_QT_PAINT, 0, 0);
  63:  
  64:            //.. 取得視窗大小
  65:            RECT tempRect = new RECT();
  66:            GetWindowRect(ParentHandle, ref tempRect);
  67:  
  68:            SetWindowPos(p.MainWindowHandle,
  69:                          HWND_TOP,
  70:                          LeftOffset,           // 0,  // 设置偏移量,把原来窗口的菜单遮住                
  71:                          TopOffset,
  72:                          tempRect.right - tempRect.left - LeftOffset + 10,// (int)this.Width,
  73:                          tempRect.bottom - tempRect.top - TopOffset + 6, // (int)this.Height,
  74:                          SWP_FRAMECHANGED);
  75:            SendMessage(p.MainWindowHandle, WM_COMMAND, WM_SIZE, 0);
  76:        }
  77:        public static void ResizeControl(Process p, IntPtr ParentHandle)
  78:        {
  79:            SendMessage(p.MainWindowHandle, WM_COMMAND, WM_PAINT, 0);
  80:            PostMessage(p.MainWindowHandle, WM_QT_PAINT, 0, 0);
  81:  
  82:            //.. 取得視窗大小
  83:            RECT tempRect = new RECT();
  84:            GetWindowRect(ParentHandle, ref tempRect);
  85:  
  86:            SetWindowPos(p.MainWindowHandle,
  87:                          HWND_TOP,
  88:                          0,  // 设置偏移量,把原来窗口的菜单遮住                
  89:                          0,
  90:                          tempRect.right - tempRect.left,// (int)this.Width,
  91:                          tempRect.bottom - tempRect.top, // (int)this.Height,
  92:                          SWP_FRAMECHANGED);
  93:            SendMessage(p.MainWindowHandle, WM_COMMAND, WM_SIZE, 0);
  94:        }
  95:        public static void ResizeControlHwnd(IntPtr pHWND, IntPtr ParentHandle)
  96:        {
  97:            //.. 取得視窗大小
  98:            RECT tempRect = new RECT();
  99:            GetWindowRect(ParentHandle, ref tempRect);
 100:  
 101:            SetWindowPos(pHWND,
 102:                          HWND_TOP,
 103:                          0,  // 设置偏移量,把原来窗口的菜单遮住                
 104:                          0,
 105:                          tempRect.right - tempRect.left,// (int)this.Width,
 106:                          tempRect.bottom - tempRect.top, // (int)this.Height,
 107:                          SWP_FRAMECHANGED);
 108:        }
 109:  
 110:        private void KillProcessByName(string ProcName)
 111:        {
 112:            Process[] myProcesses = Process.GetProcessesByName(ProcName);
 113:            foreach (Process myProcess in myProcesses)
 114:            {
 115:                //if (myProcess.MainWindowTitle.Length > 0)
 116:                {
 117:                    try { myProcess.Kill(); }
 118:                    catch { }
 119:                }
 120:            }
 121:        }
 122:        #endregion

沒有留言:

張貼留言