C#启用外部程序,同时隐藏外部程序窗体

           /// <summary>
          /// 0-关闭窗口 1-正常大小显示 2最小化窗口 3-最大化窗口
          /// </summary>
          /// <param name="hwnd"></param>
          /// <param name="nCmdShow"></param>
         /// <returns></returns>
          [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
         public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
  
         /// <summary>
         /// 显示程序
         /// </summary>
         /// <param name="hwnd">窗口句柄</param>
        /// <param name="nCmdshow">0-关闭窗口 1-正常大小显示 2最小化窗口 3-最大化窗口                       
</param>
         /// <returns></returns>
        public int DisPlayWindow(IntPtr hwnd, int nCmdshow)
        {
             return ShowWindow(hwnd, nCmdshow);
         }
 
         public Process Run(string exeName)
         {
             return Process.Start(exeName);
         }
        
         
 
         IntPtr hWnd = IntPtr.Zero;
         Process process=null;
         bool IsMini = true;
         int nCmdshow = 1;
         private void button1_Click(object sender, EventArgs e)
         {
             //获取句柄
             if (process == null)
             {
                 string exeName = Application.StartupPath + @"\CCD\CameraVision.exe";
                 //创建启动进程信息
                 process = Run(exeName);
             }
             else
             {
                 if (IsMini)
                 {
                     DisPlayWindow(process.MainWindowHandle, 1);
                     IsMini = false;
                 }
                 else
                 {
                     DisPlayWindow(process.MainWindowHandle, 2);
                    IsMini = true;
                 }
             }
             if(process!=null && !process.HasExisted)
             {
                process.kill();
             }            
         }

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要去除外部程序窗体激活状态监听,可以使用`SetWindowLong`函数来修改窗口样式。具体步骤如下: 1. 首先,需要引入Win32 API函数: ```csharp [DllImport("user32.dll")] public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); ``` 2. 然后,在启动外部程序前,需要获取该程序的窗口句柄: ```csharp Process process = new Process(); process.StartInfo.FileName = "path/to/your/program.exe"; process.Start(); process.WaitForInputIdle(); IntPtr hWnd = process.MainWindowHandle; ``` 这里使用`process.MainWindowHandle`获取外部程序的主窗口句柄。 3. 最后,使用`SetWindowLong`函数来修改窗口样式: ```csharp const int GWL_EXSTYLE = -20; const int WS_EX_NOACTIVATE = 0x08000000; int style = GetWindowLong(hWnd, GWL_EXSTYLE); SetWindowLong(hWnd, GWL_EXSTYLE, style | WS_EX_NOACTIVATE); ``` 在这个示例中,我们使用`SetWindowLong`函数将窗口样式修改为`WS_EX_NOACTIVATE`,这样程序窗口就不会被激活。 完整代码如下: ```csharp using System.Diagnostics; using System.Runtime.InteropServices; [DllImport("user32.dll")] public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll")] public static extern int GetWindowLong(IntPtr hWnd, int nIndex); const int GWL_EXSTYLE = -20; const int WS_EX_NOACTIVATE = 0x08000000; Process process = new Process(); process.StartInfo.FileName = "path/to/your/program.exe"; process.Start(); process.WaitForInputIdle(); IntPtr hWnd = process.MainWindowHandle; int style = GetWindowLong(hWnd, GWL_EXSTYLE); SetWindowLong(hWnd, GWL_EXSTYLE, style | WS_EX_NOACTIVATE); ``` 在这个示例中,我们使用了`GetWindowLong`函数来获取窗口原有的样式,然后使用`SetWindowLong`函数将其修改为`WS_EX_NOACTIVATE`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值