C#WinForm窗體的管理

本文介紹了如何通過C#使用API對WinForm窗體進行管理與操作,包括獲取窗體標題、檢測窗體狀態、遍歷屏幕上的所有應用程序窗體及對特定窗體執行顯示、最小化、最大化等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

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);
 }

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值