Net/WFP窗体枚举类

时间:2014-05-29 10:48:13   收藏:0   阅读:359
bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Xml;

// author: andy , 20140521 
// WPF下窗体枚举
namespace DrawCanvas {
    public class XmalWinEnumHelper {
        private delegate bool EnumChildDelegateProc(IntPtr hWnd, IntPtr lParam);
        public struct POINT
        {
            public int X;
            public int Y;
            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }
        
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool EnumChildWindows(IntPtr hwndParent, XmalWinEnumHelper.EnumChildDelegateProc lpEnumFunc, IntPtr lParam);
        [DllImport("user32.dll")]
        private static extern bool ScreenToClient(IntPtr hWnd, ref XmalWinEnumHelper.POINT lpPoint);
        [DllImport("user32.dll")]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool IsWindowVisible(IntPtr hWnd);

        //=> FUNCITON
        public static List<int> GetChildWindows(IntPtr hWndParent) {
            List<int> list = new List<int>();
            GCHandle value = GCHandle.Alloc(list);
            XmalWinEnumHelper.EnumChildDelegateProc lpEnumFunc = new XmalWinEnumHelper.EnumChildDelegateProc(XmalWinEnumHelper.DoEnumChildWindowsProc);
            XmalWinEnumHelper.EnumChildWindows(hWndParent, lpEnumFunc, GCHandle.ToIntPtr(value));
            value.Free();
            list.Sort();
            return list;
        }
        private static bool DoEnumChildWindowsProc(IntPtr hWnd, IntPtr lParam) {
            List<int> list = GCHandle.FromIntPtr(lParam).Target as List<int>;
            if (list != null && XmalWinEnumHelper.IsWindowVisible(hWnd)) {
                list.Add(hWnd.ToInt32());
            }
            return true;
        }

        public static bool ConvertPointFromScreenToClient(IntPtr hWnd, ref System.Windows.Point pt) {
            if (hWnd.ToInt32() == 0) {
                return false;
            }
            XmalWinEnumHelper.POINT pOINT = new XmalWinEnumHelper.POINT((int)pt.X, (int)pt.Y);
            bool flag = XmalWinEnumHelper.ScreenToClient(hWnd, ref pOINT);
            if (flag) {
                pt.X = (double)pOINT.X / 1.0;
                pt.Y = (double)pOINT.Y / 1.0;
            }
            return flag;
        }
        public static string GetWindowTitle(IntPtr hWnd) {
            StringBuilder stringBuilder = new StringBuilder(512);
            XmalWinEnumHelper.GetWindowText(hWnd, stringBuilder, 512);
            return stringBuilder.ToString();
        }
    }
}
bubuko.com,布布扣

 

 

 

Net/WFP窗体枚举类,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!