C#窗口判断,模拟按键,设置窗口最大化

这篇博客介绍了如何使用C#进行窗口操作,包括调用user32.dll库来实现模拟按键、查找和设置窗口焦点以及最大化窗口的方法。通过DllImport导入相关函数,如FindWindow、GetForegroundWindow、SetForegroundWindow和ShowWindow,可以实现对窗口的控制。

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

[DllImport(“user32.dll”)]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
[DllImport(“user32.dll”)]
static extern byte MapVirtualKey(byte wCode, int wMap);
[DllImport(“user32.dll”)]
static extern IntPtr FindWindow(string strClass, string strWindow);
[DllImport(“user32.dll”)]
private static extern IntPtr GetForegroundWindow();
[DllImport(“user32.dll”, EntryPoint = “SetForegroundWindow”, SetLastError = true)]
private static extern void SetForegroundWindow(IntPtr hwnd);
[DllImport(“user32.dll”, CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

    private void ShowVideo()
    {
        //keybd_event(18, MapVirtualKey(18, 0), 0, 0); //按下CTRL鍵。  

        
      

        while (true)
        {
            Thread.Sleep(100);
             //获取目标窗口句柄
             //可以通过工具中Spy++获得
            IntPtr ptrTaskbar = FindWindow("ZPContentViewWndClass", "Zoom");//"ZPContentViewWndClass"
            if (ptrTaskbar != IntPtr.Zero)
            {
            //获取当前窗口句柄
                IntPtr ptrCc =  GetForegroundWindow();
                if (ptrCc == ptrTaskbar)
                {
					//模拟按键
                    keybd_event(18, MapVirtualKey(18, 0), 0, 0); //按下alt鍵。   
                    keybd_event(86, MapVirtualKey(70, 0), 0, 0);//鍵下v鍵。   
                    keybd_event(86, MapVirtualKey(70, 0), 0x2, 0);//放開v鍵。  0x35 
                                                                  // Thread.Sleep(Convert.ToInt32(10 * 1000));//开起程序后等待
                    keybd_event(18, MapVirtualKey(18, 0), 0x2, 0);//放開alt鍵。

                    //MessageBox.Show("as");
                    //this.WindowState = WindowState.Maximized;

                    //SetForegroundWindow(ptrTaskbar);
                    //SendMessage(mainHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0);

                    ShowWindow(ptrTaskbar, 3);//窗口最大化
                    return;
                }
                //MessageBox.Show("1111");
               
            }
            else
            {
                //MessageBox.Show("没有");
            }
            ptrTaskbar = IntPtr.Zero;


        }
        
            Thread.Sleep(20000);
        Console.WriteLine("222222");
            keybd_event(18, MapVirtualKey(18, 0), 0, 0); //按下alt鍵。   
            keybd_event(86, MapVirtualKey(70, 0), 0, 0);//鍵下v鍵。   
            keybd_event(86, MapVirtualKey(70, 0), 0x2, 0);//放開v鍵。  0x35 
                                                          //Thread.Sleep(Convert.ToInt32(10 * 1000));//开起程序后等待
        keybd_event(18, MapVirtualKey(18, 0), 0x2, 0);//放開alt鍵。




        //MessageBox.Show("111111111111");
        //Process cur = Process.GetCurrentProcess();
        ////当前进程的id
        //Console.WriteLine(cur.Id);
        ////获取关联的进程的终端服务会话标识符。
        //Console.WriteLine(cur.SessionId);
        ////当前进程的名称
        //Console.WriteLine(cur.ProcessName);
        ////当前进程的启动时间
        //Console.WriteLine(cur.StartTime);
        ////获取关联进程终止时指定的值,在退出事件中使用
        ////Console.WriteLine(cur.ExitCode);
        ////获取进程的当前机器名称
        //Console.WriteLine(cur.MachineName); //.代表本地
        //                                    //获取进程的主窗口标题。
        //Console.WriteLine(cur.MainWindowTitle);




    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值