C#操作移動其他程序窗口


在做項目時候,曾經遇到一個問題,就是用C#的WinForm,來打開一個使用C++編寫的軟件,並控制打開窗體位置和大小。

在這里使用了Win32 API來做的。可以使用C#根據窗體的路徑,啟動一個進程,然后使用Win32 API控制打開窗口的位置和大小。

主要代碼如下:

   public class A

{

        //調用Win32 API
         [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
         public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

        //打開窗體方法,fileName是C++的窗體名稱,包含路徑

        private void OpenAndSetWindow(String fileName)
        {
            Process p = new Process();//新建進程
            p.StartInfo.FileName = fileName;//設置進程名字
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            p.Start();
            MoveWindow(p.MainWindowHandle, 200, 300, 500, 400, true);

           //p.MainWindowHandle是你要移動的窗口的句柄;200,300是移動后窗口左上角的橫縱坐標;500,400是移動后窗口的寬度和高度;true表示移動后的窗口是需要重畫

      }

}

如果打開IE網頁,可以成下面語句一句

 p.StartInfo.FileName = "iexplore";
 p.StartInfo.Arguments = "www.baidu.com";//網頁


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM