Exemple #1
0
        void ActivateGlass()
        {
            PresentationSource p            = PresentationSource.FromVisual(this);
            HwndSource         s_hwndSource = p as HwndSource;

            if (s_hwndSource != null)
            {
                s_hwndSource.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);
                handle = s_hwndSource.Handle;
            }


            SafeNativeMethods.RECT r = new SafeNativeMethods.RECT();

            UnsafeNativeMethods.GetClientRect(handle, ref r);

            IntPtr hrgn = UnsafeNativeMethods.CreateRectRgn(0, 0, 1, 1);

            SafeNativeMethods.DWM_BLURBEHIND bb = new SafeNativeMethods.DWM_BLURBEHIND();
            bb.dwFlags  = SafeNativeMethods.DWM_BB_ENABLE | SafeNativeMethods.DWM_BB_BLURREGION;
            bb.fEnable  = true;
            bb.hRgnBlur = hrgn;

            UnsafeNativeMethods.DwmEnableBlurBehindWindow(handle, ref bb);

            SafeNativeMethods.MARGINS mar = new SafeNativeMethods.MARGINS();

            mar.cyTopHeight = 37;

            UnsafeNativeMethods.DwmExtendFrameIntoClientArea(handle, ref mar);

            //Need to make the Window size dirty.
            this.WindowState = WindowState.Minimized;
            this.WindowState = WindowState.Normal;
        }
Exemple #2
0
        private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam)
        {
            SafeNativeMethods.MINMAXINFO mmi = (SafeNativeMethods.MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(SafeNativeMethods.MINMAXINFO));

            // Adjust the maximized size and position to fit the work area of the correct monitor
            int    MONITOR_DEFAULTTONEAREST = 0x00000002;
            IntPtr monitor = SafeNativeMethods.MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);

            if (monitor != IntPtr.Zero)
            {
                SafeNativeMethods.MONITORINFO monitorInfo = new SafeNativeMethods.MONITORINFO();

                SafeNativeMethods.GetMonitorInfo(monitor, monitorInfo);
                SafeNativeMethods.RECT rcWorkArea    = monitorInfo.rcWork;
                SafeNativeMethods.RECT rcMonitorArea = monitorInfo.rcMonitor;
                mmi.ptMaxPosition.X = Math.Abs(rcWorkArea.Left - rcMonitorArea.Left);
                mmi.ptMaxPosition.Y = Math.Abs(rcWorkArea.Top - rcMonitorArea.Top);
                mmi.ptMaxSize.X     = Math.Abs(rcWorkArea.Right - rcWorkArea.Left);
                mmi.ptMaxSize.Y     = Math.Abs(rcWorkArea.Bottom - rcWorkArea.Top);
            }

            Marshal.StructureToPtr(mmi, lParam, true);
        }
        private Bitmap CaptureWindowFromWindow_(IntPtr window, bool clientArea)
        {
            string traceMsg = Tracer.FormatCall(
                "CaptureWindowFromWindow_", window, clientArea);

            try
            {
                logger_.Log(traceMsg);

                DateTime start = DateTime.Now;

                // we actually capture the root window and then copy the requested region
                // since several windows may paint on the same area of the control.
                IntPtr rwh = SafeNativeMethods.GetAncestor(
                    window,
                    SafeNativeMethods.GetAncestorFlags.GA_ROOT);

                if (!SafeNativeMethods.GetWindowRect(rwh, out SafeNativeMethods.RECT rwr))
                {
                    logger_.Log(
                        "{0}: GetWindowRect failed ({1})",
                        traceMsg,
                        Marshal.GetLastWin32Error());
                    return(null);
                }

                if (rwr.Width == 0 || rwr.Height == 0)
                {
                    return(null);
                }

                using (Bitmap rbmp = new Bitmap(rwr.Width, rwr.Height))
                {
                    using (Graphics g = Graphics.FromImage(rbmp))
                    {
                        IntPtr dc = g.GetHdc();
                        try
                        {
                            if (!SafeNativeMethods.PrintWindow(rwh, dc, 0))
                            {
                                logger_.Log(
                                    "{0}: PrintWindow failed ({1})",
                                    traceMsg,
                                    Marshal.GetLastWin32Error());
                                return(null);
                            }
                        }
                        finally
                        {
                            g.ReleaseHdc();
                        }
                    }

                    if (!WindowsGuiUtils.GetWindowRectangles(logger_, window, out SafeNativeMethods.RECT wr, out SafeNativeMethods.RECT cr))
                    {
                        logger_.Log("{0}: GetWindowRectangles failed", traceMsg);
                        return(null);
                    }

                    SafeNativeMethods.RECT r = clientArea ? cr : wr;

                    // make r's coordinates relative to rbmp; that is, (0,0)
                    r.Move(-rwr.Left, -rwr.Top);

                    Bitmap bmp = rbmp.Clone(
                        new Rectangle(r.Left, r.Top, r.Width, r.Height),
                        rbmp.PixelFormat);

                    logger_.Verbose(
                        "{0} succeeded in {1}ms",
                        traceMsg,
                        DateTime.Now.Subtract(start).TotalMilliseconds);

                    return(bmp);
                }
            }
            catch (Exception ex)
            {
                logger_.Verbose("{0} failed: {1}", traceMsg, ex);
                throw;
            }
        }