Windows 7下使用窗口实时缩略图的类

13 篇文章 0 订阅
6 篇文章 0 订阅
Windows 7下使用窗口缩略图的类
    internal sealed class ThumbnailSafeHandle : SafeHandle
    {
        [DllImport("dwmapi.dll", SetLastError = true)]
        static extern int DwmQueryThumbnailSourceSize(ThumbnailSafeHandle hThumbnail, ref PSIZE pSize);
        [DllImport("dwmapi.dll", SetLastError = true)]
        static extern int DwmRegisterThumbnail(IntPtr hwndDestination, IntPtr hwndSource, out ThumbnailSafeHandle hThumbnailId);
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("dwmapi.dll", SetLastError = true)]
        static extern int DwmUnregisterThumbnail(IntPtr hThumbmailId);
        [DllImport("dwmapi.dll", SetLastError = true)]
        static extern int DwmUpdateThumbnailProperties(ThumbnailSafeHandle hThumbmailId, ref DWM_THUMBNAIL_PROPERTIES ptnProperties);
        [DllImport("user32.dll")]
        static extern int GetWindowRect(IntPtr hwnd, ref RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]
        struct PSIZE
        {
            public int cx;
            public int cy;
        }

        [Serializable, StructLayout(LayoutKind.Sequential)]
        struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
            internal RECT(int l, int t, int r, int b)
            {
                this.left = l;
                this.top = t;
                this.right = r;
                this.bottom = b;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        struct DWM_THUMBNAIL_PROPERTIES
        {
            public int dwFlags;
            public RECT rcDestination;
            public RECT rcSource;
            public byte opacity;
            public bool fVisible;
            public bool fSourceClientAreaOnly;
        }

        private ThumbnailSafeHandle()
            : base(IntPtr.Zero, true)
        {
        }

        //在目标窗口(HWND = hwndDestination)内显示源窗口(HWND = hwndSource)的实时缩略图
        public static ThumbnailSafeHandle Register(IntPtr hwndDestination, IntPtr hwndSource)
        {
            ThumbnailSafeHandle handle;
            DwmRegisterThumbnail(hwndDestination, hwndSource, out handle);
            Size size = handle.Size;
            DWM_THUMBNAIL_PROPERTIES m_ThumbnailProperties = new DWM_THUMBNAIL_PROPERTIES();
            m_ThumbnailProperties.dwFlags = 29;  
            m_ThumbnailProperties.opacity = 128; //透明度
            m_ThumbnailProperties.fVisible = true;
            m_ThumbnailProperties.fSourceClientAreaOnly = true; //只显示客户区
            m_ThumbnailProperties.rcDestination = new RECT(0, 0, (int)size.Width, (int)size.Height); //显示在目标窗口的哪个位置
            DwmUpdateThumbnailProperties(handle, ref m_ThumbnailProperties); 
            return handle;
        }

        public Size Size 
        {
            get 
            {
                PSIZE size = new PSIZE();
                DwmQueryThumbnailSourceSize(this, ref size);
                return new Size(size.cx, size.cy);
            }
        }

        protected override bool ReleaseHandle()
        {
            if (this.IsInvalid)
            {
                return true;
            }
            int num = DwmUnregisterThumbnail(base.handle);
            base.SetHandle(IntPtr.Zero);
            return (num == 0);
        }

        public override bool IsInvalid
        {
            get
            {
                return (IntPtr.Zero == base.handle);
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值