C#WinForm窗體的管理
1.API申明
//將指定的應用程序窗口標題欄上的標題復制放入緩衝器
[DllImport("user32.dll")]
private static extern int GetWindowText(int hWnd, StringBuilder title, int size);
//列舉出屏幕上的窗口應用程序
[DllImport("user32.dll")]
private static extern int EnumWindows(EnumWindowsProc ewp, int lParam);
//返回應用程序窗口是否顯示
[DllImport("user32.dll")]
private static extern bool IsWindowVisible(int hWnd);
//設置應用程序窗口是否顯示
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
//判斷應用程序窗口是否最小化
[DllImport("user32.dll")]
private static extern bool IsIconic(int hwnd);
//判斷應用程序窗口是否最大化
[DllImport("user32.dll")]
private static extern bool IsZoomed(int hwnd);
//將消息傳給指定窗口
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd,uint Msg,int wParam,int lParam);
[DllImport("user32.dll")]
private static extern int GetWindowText(int hWnd, StringBuilder title, int size);
//列舉出屏幕上的窗口應用程序
[DllImport("user32.dll")]
private static extern int EnumWindows(EnumWindowsProc ewp, int lParam);
//返回應用程序窗口是否顯示
[DllImport("user32.dll")]
private static extern bool IsWindowVisible(int hWnd);
//設置應用程序窗口是否顯示
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
//判斷應用程序窗口是否最小化
[DllImport("user32.dll")]
private static extern bool IsIconic(int hwnd);
//判斷應用程序窗口是否最大化
[DllImport("user32.dll")]
private static extern bool IsZoomed(int hwnd);
//將消息傳給指定窗口
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd,uint Msg,int wParam,int lParam);
2.遍歷窗體
private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
private bool EvalWindow(int hWnd, int lParam)
{//檢測指定窗體
if (!IsWindowVisible(hWnd))
return true;
StringBuilder strTitle = new StringBuilder(256);
GetWindowText(hWnd, strTitle, 256);
if (strTitle.Length == 0)
return true;
if ((strTitle.ToString() != "ScrnClear"))
{
if (IsIconic(hWnd))
//窗體已最小化時的操作
else if (IsZoomed(hWnd))
//窗體已最大化時的操作
else
//窗體原始大小時的操作
}
return true;
}
private void GetWindows()
{
EnumWindowsProc ewp = new EnumWindowsProc(EvalWindow);
EnumWindows(ewp, 0);
}
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
private bool EvalWindow(int hWnd, int lParam)
{//檢測指定窗體
if (!IsWindowVisible(hWnd))
return true;
StringBuilder strTitle = new StringBuilder(256);
GetWindowText(hWnd, strTitle, 256);
if (strTitle.Length == 0)
return true;
if ((strTitle.ToString() != "ScrnClear"))
{
if (IsIconic(hWnd))
//窗體已最小化時的操作
else if (IsZoomed(hWnd))
//窗體已最大化時的操作
else
//窗體原始大小時的操作
}
return true;
}
private void GetWindows()
{
EnumWindowsProc ewp = new EnumWindowsProc(EvalWindow);
EnumWindows(ewp, 0);
}
3.操作窗體 hWnd.ToString()="888"
//取得窗體裝態
if (IsIconic(Convert.ToInt32("888")))
//窗體最小化
else if (IsZoomed(Convert.ToInt32("888")))
//窗體最大化
else
//原始大小
//關閉程序
SendMessage("888", WM_SYSCOMMAND, SC_CLOSE, 0);
//隱藏程序窗體
if (IsWindowVisible("888"))
ShowWindowAsync((IntPtr)Convert.ToInt32("888"), SW_HIDE);
//顯示並最大化窗體
ShowWindowAsync((IntPtr)Convert.ToInt32("888"), SW_SHOWMAXIMIZED);
//顯示並最小化窗體
ShowWindowAsync((IntPtr)Convert.ToInt32("888"), SW_SHOWMINIMIZED);
//設定窗體為原來大小顯示
ShowWindowAsync((IntPtr)Convert.ToInt32("888"), SW_RESTORE);
if (IsIconic(Convert.ToInt32("888")))
//窗體最小化
else if (IsZoomed(Convert.ToInt32("888")))
//窗體最大化
else
//原始大小
//關閉程序
SendMessage("888", WM_SYSCOMMAND, SC_CLOSE, 0);
//隱藏程序窗體
if (IsWindowVisible("888"))
ShowWindowAsync((IntPtr)Convert.ToInt32("888"), SW_HIDE);
//顯示並最大化窗體
ShowWindowAsync((IntPtr)Convert.ToInt32("888"), SW_SHOWMAXIMIZED);
//顯示並最小化窗體
ShowWindowAsync((IntPtr)Convert.ToInt32("888"), SW_SHOWMINIMIZED);
//設定窗體為原來大小顯示
ShowWindowAsync((IntPtr)Convert.ToInt32("888"), SW_RESTORE);