歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C#操作移動其他程序窗口

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

日期:2017/3/1 10:17:48   编辑:Linux編程

在做項目時候,曾經遇到一個問題,就是用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表示移動後的窗口是需要重畫

}

}

Copyright © Linux教程網 All Rights Reserved