以下程式碼,透過 button1 控制項 Click 事件執行 ShowWindow() 方法,控制外部程式視窗狀態:包含最大化、最小化、縮到工具列、復原 等動作。
控制外部程序,請參考 Process 類別。
程式碼:
-
using System;
-
using System.Diagnostics;
-
using System.Runtime.InteropServices; // to import Dll
-
using System.Windows.Forms;
-
-
namespace WordTest
-
{
-
public partial class Form1 : Form
-
{
-
public Form1( )
-
{
-
InitializeComponent( );
-
}
-
[DllImport( "User32" ) ]
-
private static extern int ShowWindow( int hwnd, int nCmdShow);
-
private enum CommandShow : int
-
{
-
SW_HIDE = 0,
-
SW_SHOWNORMAL = 1,
-
SW_NORMAL = 1,
-
SW_SHOWMINIMIZED = 2,
-
SW_SHOWMAXIMIZED = 3,
-
SW_MAXIMIZE = 3,
-
SW_SHOWNOACTIVATE = 4,
-
SW_SHOW = 5,
-
SW_MINIMIZE = 6,
-
SW_SHOWMINNOACTIVE = 7,
-
SW_SHOWNA = 8,
-
SW_RESTORE = 9,
-
SW_SHOWDEFAULT = 10,
-
SW_FORCEMINIMIZE = 11,
-
SW_MAX = 11
-
} ;
-
private static void button1_Click( object sender, EventArgs e)
-
{
-
Process[ ] p = Process.GetProcessesByName ( "notepad" );
-
if (p.Length > 0 )
-
{
-
int hwnd;
-
hwnd = p[ 0 ].MainWindowHandle.ToInt32 ( );
-
ShowWindow(hwnd, ( int ) CommandShow.SW_RESTORE );
-
}
-
}
-
}
-
}
|