栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

C# 控制台程序检测程序窗体最大最小化

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C# 控制台程序检测程序窗体最大最小化

```csharp
using System;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace PCListener
{
    static class Program
    {
        static bool _IsExit = false;
        //展示窗口API
        [DllImport("User32.dll", EntryPoint = "ShowWindow")]   //
        private static extern bool ShowWindow(IntPtr hWnd, int type);
        
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        
        //获取窗体位置 最小化 最大化 隐藏 api
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

        static  UInt32 SW_HIDE = 0;
        static  UInt32 SW_SHOWNORMAL = 1;
        static  UInt32 SW_NORMAL = 1;
        static  UInt32 SW_SHOWMINIMIZED = 2;
        const   UInt32 SW_SHOWMAXIMIZED = 3;
        static  UInt32 SW_MAXIMIZE = 3;
        static  UInt32 SW_SHOWNOACTIVATE = 4;
        static  UInt32 SW_SHOW = 5;
        static  UInt32 SW_MINIMIZE = 6;
        static  UInt32 SW_SHOWMINNOACTIVE = 7;
        static  UInt32 SW_SHOWNA = 8;
        static  UInt32 SW_RESTORE = 9;

        static IntPtr ParenthWnd = new IntPtr(0);
        static void Main()
        {   
            Console.Title("窗体标题")
            ParenthWnd = FindWindow(null, "窗体标题");
            //ShowWindow(ParenthWnd, 0);//隐藏本dos窗体, 0: 后台执行;1:正常启动;2:最小化到任务栏;3:最大化
            Thread checkMiniMinimizedThread=new Thread(new ThreadStart(checkMiniMinimized));
            checkMiniMinimizedThread.Start();

            while (true)
            {
                Application.DoEvents();
                if (_IsExit)
                {
                    break;
                }
                Thread.Sleep(1000);
            }

        }
        //检测窗体是否被最小化了
        static void checkMiniMinimized()
        {
            while (true)
            {
                var proc = Process.GetProcessesByName("程序进程名字");
                if (proc.Length > 0)
                {  
                    bool isNotepadMinimized = GetMinimized(proc[0].MainWindowHandle);
                    if (isNotepadMinimized)
                    { 
                       Console.WriteLine("窗口被最小化了");
                    }
                }
                Thread.Sleep(1000);
            }
        }
        private struct WINDOWPLACEMENT
        {
            public int length;
            public int flags;
            public int showCmd;
            public System.Drawing.Point ptMinPosition;
            public System.Drawing.Point ptMaxPosition;
            public System.Drawing.Rectangle rcNormalPosition;
        }

        public static bool GetMinimized(IntPtr handle)
        {
            WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
            placement.length = Marshal.SizeOf(placement);
            GetWindowPlacement(handle, ref placement);
            return placement.showCmd == SW_SHOWMINIMIZED;
            //1 = normal
            //2 = minimized
            //3 = maximized
        }

    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/911509.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号