c#读取系统下所有执行文件窗体名称及句柄

获得窗体的句柄可以用GetWindow方法获得窗体标题用GetWindowText方法,显示隐藏某个窗体用ShowWindow方法,给你举个例子

using System.Runtime.InteropServices;

         private const int WS_VISIBLE =268435456;//窗体可见
        private const int WS_MINIMIZEBOX= 131072;//有最小化按钮
        private const int WS_MAXIMIZEBOX= 65536;//有最大化按钮
        private const int WS_BORDER =8388608;//窗体有边框
        private const int GWL_STYLE =(-16);//窗体样式
        private const int GW_HWNDFIRST =0;
        private const int GW_HWNDNEXT =2;
        private const int SW_HIDE = 0;
        private const int SW_SHOW = 5;

       [DllImport("User32.dll")]
        private extern static intGetWindow(int hWnd, int wCmd);
       [DllImport("User32.dll")]
        private extern static intGetWindowLongA(int hWnd, int wIndx);
       [DllImport("user32.dll")]
        private static extern boolGetWindowText(int hWnd, StringBuilder title, int maxBufSize);
       [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private extern static intGetWindowTextLength(IntPtr hWnd);
       [DllImport("user32.dll")]
        public static extern intShowWindow(int hwnd, int nCmdShow);

        //获得包含窗体可见、有边框、有最大化按钮的窗体的句柄和标题(窗体的属性出这几种外还有很多种)
        public staticList<FromInfo> GetHandleList(int Handle)
        {
            List<FromInfo> fromInfo = newList<FromInfo>();
            int handle =GetWindow(Handle, GW_HWNDFIRST);
            while (handle > 0)
            {
                int IsTask = WS_VISIBLE |WS_BORDER | WS_MAXIMIZEBOX;//窗体可见、有边框、有最大化按钮
                int lngStyle = GetWindowLongA(handle,GWL_STYLE);
                bool TaskWindow =((lngStyle & IsTask) == IsTask);
                if (TaskWindow)
                {
                    int length =GetWindowTextLength(new IntPtr(handle));
                    StringBuilder stringBuilder = newStringBuilder(2 * length + 1);
                    GetWindowText(handle,stringBuilder, stringBuilder.Capacity);
                    string strTitle =stringBuilder.ToString();
                    if (!string.IsNullOrEmpty(strTitle))
                    {
                        fromInfo.Add(newFromInfo(strTitle, handle));
                    }
                    else
                    {
                        fromInfo.Add(newFromInfo("", handle));
                    }
                }
                handle =GetWindow(handle, GW_HWNDNEXT);
            }
            return fromInfo;
        }
        //获得所有窗体的句柄和标题
        public staticList<FromInfo> GetHandleList(int Handle)
        {
            List<FromInfo> fromInfo= new List<FromInfo>();
            int handle =GetWindow(Handle, GW_HWNDFIRST);
            while (handle > 0)
            {
                int length =GetWindowTextLength(new IntPtr(handle));
                StringBuilder stringBuilder= new StringBuilder(2 * length + 1);
                GetWindowText(handle,stringBuilder, stringBuilder.Capacity);
                string strTitle =stringBuilder.ToString();
                if(!string.IsNullOrEmpty(strTitle))
                {
                    fromInfo.Add(newFromInfo(strTitle, handle));
                }
                else
                {
                    fromInfo.Add(newFromInfo("", handle));
                }
                handle =GetWindow(handle, GW_HWNDNEXT);
            }
            return fromInfo;
        }

        public class FromInfo
        {
            public FromInfo(string title,int handle)
            {
                this.title = title;
                this.handle = handle;
            }
            private string title;
            private int handle;

            public string Title
            {
                get { return title; }
                set { title = value; }
            }
            public int Handle
            {
                get { return handle; }
                set { handle = value; }
            }
        }

        //获得窗体句柄和标题
        private void button1_Click(objectsender, EventArgs e)
        {
            List<FromInfo> fromInfo= GetHandleList(this.Handle.ToInt32());
        }
        private void button2_Click(objectsender, EventArgs e)
        {
            //隐藏窗体
           ShowWindow(this.Handle.ToInt32(), SW_HIDE);
            //显示窗体
           ShowWindow(this.Handle.ToInt32(), SW_SHOW);
        }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值