日期:2014-05-18  浏览次数:21236 次

C# API
C# winform,调用一个外部exe程序,请问有什么办法,控制这个exe程序窗体显示大小和显示位置,谢谢!

------解决方案--------------------
MSDN 查查这个Process类,看看Demo就知道了
------解决方案--------------------
启动:
shellexecute()
createprocess()
winexec()
大小和位置:
setwindowpos()
查找窗口:
findwindow()
enumwindows()
------解决方案--------------------
Process.start
------解决方案--------------------
You can choose to start the process with a minimized, maximized or normal window (by setting the ProcessStartInfo.WindowStyle and using the Process.Start(ProcessStartInfo) method). However, you don't really have control over the size of the window of the outside process when you start it. 

Once you launch the the program, you can get it's main window handle using Process.MainWindowHandle.

Given the window handle, you should be able to P/Invoke SetWindowPos to position or size that window

------解决方案--------------------
帮顶,可以在程序中启动,控制大小。。。待研究!!
------解决方案--------------------
Process~~
------解决方案--------------------
自己慢慢琢磨,记得给分哦
C# code

KeyboardHook keyboardHook = new KeyboardHook();//全局按键

        //查找窗口
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        //可将最小化窗口还原
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        //将指定窗口激活
        [DllImport("user32.dll ")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        //设置鼠标位置
        [DllImport("user32.dll")]
        private static extern bool SetCursorPos(int X, int Y);
        //设置窗口位置和大小 
        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
        //模拟鼠标
        [DllImport("user32.dll")]
        private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        private const int SW_RESTORE = 9; //将窗口还原的参数
        private const uint SWP_SHOWWINDOW = 0x0040; //设置窗口位置的参数

        private const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下 
        private const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起 
        void keyboardHook_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                if (lvInfo.SelectedItems.Count > 0)
                {
                    IntPtr findPtr = FindWindow(null, "World of Warcraft");//World of Warcraft为游戏在任务栏的名称
                    ShowWindow(findPtr, SW_RESTORE); //将窗口还原
                    SetForegroundWindow(findPtr);//将指定窗口激活
                    if (findPtr.ToInt32()!=0)
                    {
                        SymmetricMethod sm = new SymmetricMethod();
                        //设置窗口位置和大小
                        SetWindowPos(findPtr, (IntPtr)(-1), 0, 0, 700, 550, SWP_SHOWWINDOW);
                        System.Threading.Thread.Sleep(500);
                        SetCursorPos(300, 300); //设置鼠标位置
                        mouse_event(0x0002, 0, 0, 0, 0); //模拟鼠标按下操作
                        mouse_event(0x0004, 0, 0, 0, 0); //模拟鼠标放开操作
                        System.Threading.Thread.Sleep(1000);
                        SendKeys.Send(lvInfo.SelectedItems[0].SubItems[1].Text);   //模拟键盘输入游戏ID
                        SendKeys.Send("{TAB}"); //模拟键盘输入TAB
                        SendKeys.Send(sm.Decrypto(lvInfo.SelectedItems[0].SubItems[2].Text)); //模拟键盘输入游戏密码
                        SendKeys.Send("{ENTER}"); //模拟键盘输入ENTER
                    }
                    else
                    {