Exemplo n.º 1
1
        private HwndSource CreateBackgroundWindow()
        {
            HwndSource presentationSource = PresentationSource.FromVisual(Application.Current.MainWindow) as HwndSource;
            IntPtr zero = IntPtr.Zero;
            if (presentationSource != null)
            {
                zero = presentationSource.Handle;
            }
            //WS_EX_TOPMOST   = 0x00000008,WS_CHILDWINDOW   = 0x40000000, WS_EX_NOACTIVATE  = 0x08000000,
            //WS_EX_LAYERED   = 0x00080000,WS_EX_TRANSPARENT  = 0x00000020,
            int num = 0;
            int num2 = 0x4000000;
            int num3 = 0x80000 | 0x20 | 0x00000008 | 0x08000000; //0x8000080;WS_EX_TRANSPARENT | WS_EX_LAYERED

            HwndSourceParameters parameters = new HwndSourceParameters()
            {
                WindowClassStyle = num,
                WindowStyle = num2,
                ExtendedWindowStyle = num3
            };
            parameters.SetPosition(0, 0);
            parameters.UsesPerPixelOpacity = true; //transparent;

            if ((zero != IntPtr.Zero))
            {
                //parameters.ParentWindow = zero;
            }

            HwndSource source2 = new HwndSource(parameters);
            source2.RootVisual = this;
            return source2;
        }
Exemplo n.º 2
0
        internal static void CreateHostHwnd(IntPtr parentHwnd)
        {
            // Set up the parameters for the host hwnd.
            HwndSourceParameters parameters = new HwndSourceParameters("Visual Hit Test", _width, _height);
            parameters.WindowStyle = WS_VISIBLE | WS_CHILD;
            parameters.SetPosition(0, 24);
            parameters.ParentWindow = parentHwnd;
            parameters.HwndSourceHook = new HwndSourceHook(ApplicationMessageFilter);

            // Create the host hwnd for the visuals.
            myHwndSource = new HwndSource(parameters);

            // Set the hwnd background color to the form's background color.
            myHwndSource.CompositionTarget.BackgroundColor = System.Windows.Media.Brushes.OldLace.Color;
        }
Exemplo n.º 3
0
        internal static void CreateHostHwnd(IntPtr parentHwnd)
        {
            // Set up the parameters for the host hwnd.
            var parameters = new HwndSourceParameters("Visual Hit Test", Width, Height)
            {
                WindowStyle = WsVisible | WsChild
            };
            parameters.SetPosition(0, 24);
            parameters.ParentWindow = parentHwnd;
            parameters.HwndSourceHook = ApplicationMessageFilter;

            // Create the host hwnd for the visuals.
            MyHwndSource = new HwndSource(parameters);

            // Set the hwnd background color to the form's background color.
            MyHwndSource.CompositionTarget.BackgroundColor = Brushes.OldLace.Color;
        }
        /// <summary>
        /// Show our Preview user control in the little window provided by the Screen Saver control panel.
        /// </summary>
        /// <param name="hWnd">The hWnd passed to us by the Screen Saver control panel.</param>
        private void ShowPreview(IntPtr hWnd)
        {
            Log("ShowPreview(): Entered.");
            Log("   ShowPreview(): cpl hWnd passed to us = " + hWnd);

            if (NativeMethods.IsWindow(hWnd))
            {
                // Get the rect of the desired parent
                int error = 0;
                System.Drawing.Rectangle ParentRect = new System.Drawing.Rectangle();
                NativeMethods.SetLastErrorEx(0, 0);
                Log("  ShowPreview(): Let's Get the ClientRect of that puppy:");
                Log("  ShowPreview(): Calling GetClientRect(" + hWnd + ")...");
                bool fSuccess = NativeMethods.GetClientRect(hWnd, ref ParentRect);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Log("  ShowPreview(): GetClientRect() returned bool = " + fSuccess + ", rect = " + ParentRect.ToString());
                Log("      GetLastError() returned: " + error.ToString());
                Log(" ");

                // Create the HwndSource which will host our Preview user control
                Log("  ShowPreview(): Let's build a new HwndSource (src), and attach it to the cpl window:");
                HwndSourceParameters parameters = new HwndSourceParameters();
                parameters.WindowStyle = NativeMethods.WindowStyles.WS_CHILD | NativeMethods.WindowStyles.WS_VISIBLE;
                parameters.SetPosition(0, 0);  // in theory, our child will use values relative to parents position
                parameters.SetSize(ParentRect.Width, ParentRect.Height);
                parameters.ParentWindow = hWnd;
                HwndSource src = new HwndSource(parameters);

            #if DEBUG
                // Let's see what Windows thinks
                Log("  ShowPreview(): Attached it. Let's see what Windows thinks:");
                Log("  ShowPreview(): Calling GetParent(src.Handle)...");
                NativeMethods.SetLastErrorEx(0, 0);
                IntPtr handle = IntPtr.Zero;
                handle = NativeMethods.GetParent(src.Handle);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Log("  ShowPreview(): GetParent() returned: " + handle.ToString());
                Log("      GetLastError() returned: " + error.ToString());
                Log(" ");
            #endif

                // Create the user control and attach it
                Log("  ShowPreview(): Creating the user control (Preview)");
                PreviewControl Preview = new PreviewControl();
                Log("  ShowPreview(): setting src.RootVisual to user control...");
                src.RootVisual = Preview;
                Preview.Visibility = Visibility.Visible;

            #if DEBUG
                // Let's find out what Windows thinks
                Log("  ShowPreview(): Set it. Let's see what Windows thinks the HwndSource for Preview is:");
                HwndSource hs = (HwndSource)HwndSource.FromVisual(Preview);
                Log("  ShowPreview(): HwndSource.FromVisual(Preview) is: " + HwndSource.FromVisual(Preview));
                Log("  ShowPreview(): Let's see what Windows thinks the parent hWnd of Preview is:");
                Log("  ShowPreview(): Calling GetParent((HwndSource)HwndSource.FromVisual(Preview).handle)...");
                NativeMethods.SetLastErrorEx(0, 0);
                IntPtr ucHandle = IntPtr.Zero;
                handle = NativeMethods.GetParent(hs.Handle);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Log("  ShowPreview(): GetParent() returned: " + handle.ToString());
                Log("      GetLastError() returned: " + error.ToString());
                Log(" ");

                Log("  ShowPreview(): Is the src window visible?");
                Log("  ShowPreview(): Calling IsWindowVisible(src.Handle)...");
                NativeMethods.SetLastErrorEx(0, 0);
                bool fVisible = NativeMethods.IsWindowVisible(src.Handle);
                error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                Log("  ShowPreview(): IsWindowVisible() returned: " + fVisible.ToString());
                Log("      GetLastError() returned: " + error.ToString());
                Log(" ");
            #endif

                // Let's hook into src's message pump
                Log("  ShowPreview(): Let's hook into src's message pump");
                // TODO: determine if we need to hook into message pump

            }
            else
            {
                Log("  ShowPreview(): Invalid hWnd passed: " + hWnd.ToString());
                throw new ArgumentException("Invalid hWnd passed to ShowPreview(): " + hWnd.ToString());
            }

            Log("ShowPreview(): exiting.");
        }
Exemplo n.º 5
0
 public IntPtr Create(int classStyle, int style, int exStyle, int x, int y, int width, int height,
     string name, IntPtr parent)
 {
     HwndSourceParameters parameters = new HwndSourceParameters(name, width, height);
     parameters.WindowClassStyle = classStyle;
     parameters.WindowStyle = style;
     parameters.ExtendedWindowStyle = exStyle;
     parameters.SetPosition(x, y);
     parameters.ParentWindow = parent;
     parameters.HwndSourceHook = HwndSourceHook;
     return Create(parameters);
 }
Exemplo n.º 6
0
        public HwndSource(
            int classStyle,
            int style,
            int exStyle,
            int x,
            int y,
            string name,
            IntPtr parent)
        {
            SecurityHelper.DemandUIWindowPermission();

            HwndSourceParameters param = new HwndSourceParameters(name);
            param.WindowClassStyle = classStyle;
            param.WindowStyle = style;
            param.ExtendedWindowStyle = exStyle;
            param.SetPosition(x, y);
            param.ParentWindow = parent;
            Initialize(param);
        }
Exemplo n.º 7
0
        public HwndSource(int classStyle,
                          int style,
                          int exStyle,
                          int x,
                          int y,
                          int width,
                          int height,
                          string name,
                          IntPtr parent,
                          bool adjustSizingForNonClientArea)
        {
            SecurityHelper.DemandUIWindowPermission();

            HwndSourceParameters parameters = new HwndSourceParameters(name, width, height);
            parameters.WindowClassStyle = classStyle;
            parameters.WindowStyle = style;
            parameters.ExtendedWindowStyle = exStyle;
            parameters.SetPosition(x, y);
            parameters.ParentWindow = parent;
            parameters.AdjustSizingForNonClientArea = adjustSizingForNonClientArea;
            Initialize(parameters);
        }
Exemplo n.º 8
0
            internal void BuildWindow(int x, int y, Visual placementTarget,
                bool transparent, HwndSourceHook hook, AutoResizedEventHandler handler)
            {
                Debug.Assert(!IsChildPopup || (IsChildPopup && !transparent), "Child popups cannot be transparent");
                transparent = transparent && !IsChildPopup;

                Visual mainTreeVisual = placementTarget;
                if (IsChildPopup)
                {
                    // If the popup is nested inside other popups, get out into the main tree
                    // before querying for the presentation source.
                    mainTreeVisual = FindMainTreeVisual(placementTarget);
                }

                // get visual's PresentationSource
                HwndSource hwndSource = PopupSecurityHelper.GetPresentationSource(mainTreeVisual) as HwndSource;

                // get parent handle
                IntPtr parent = IntPtr.Zero;
                if (hwndSource != null)
                {
                    parent = PopupSecurityHelper.GetHandle(hwndSource);
                }

                int classStyle = 0;
                int style = NativeMethods.WS_CLIPSIBLINGS;
                int styleEx = NativeMethods.WS_EX_TOOLWINDOW | NativeMethods.WS_EX_NOACTIVATE;

                if (IsChildPopup)
                {
                    // The popup was created in an environment where it should be a child window, not a popup window.
                    style |= NativeMethods.WS_CHILD;
                }
                else
                {
                    style |= NativeMethods.WS_POPUP;
                    styleEx |= NativeMethods.WS_EX_TOPMOST;
                }

                // set window parameters
                HwndSourceParameters param = new HwndSourceParameters(String.Empty);
                param.WindowClassStyle = classStyle;
                param.WindowStyle = style;
                param.ExtendedWindowStyle = styleEx;
                param.SetPosition(x, y);

                if (IsChildPopup)
                {
                    if ( parent != IntPtr.Zero )
                    {
                        param.ParentWindow = parent;
                    }
                    else
                    {
                        SecurityHelper.DemandUIWindowPermission();
                    }
                }
                else
                {
                    param.UsesPerPixelOpacity = transparent;
                    if ((parent != IntPtr.Zero) && ConnectedToForegroundWindow(parent))
                    {
                        param.ParentWindow = parent;
                    }
                }

                // create popup's window object
                HwndSource newWindow = new HwndSource(param);

                new UIPermission(UIPermissionWindow.AllWindows).Assert(); //BlessedAssert
                try
                {
                    // add hook to the popup's window
                    newWindow.AddHook(hook);
                }
                finally
                {
                    UIPermission.RevertAssert();
                }

                // initialize the private critical window object
                _window = new SecurityCriticalDataClass<HwndSource>(newWindow);

                // Set background color
                HwndTarget hwndTarget = (HwndTarget)newWindow.CompositionTarget;
                hwndTarget.BackgroundColor = transparent ? Colors.Transparent : Colors.Black;

                // add AddAutoResizedEventHandler event handler
                newWindow.AutoResized += handler;
            }
Exemplo n.º 9
0
        private HwndSource EnsureHiddenWindow() 
        {
            if (_hiddenWindow == null) 
            { 
                HwndSourceParameters param = new HwndSourceParameters("Hidden Window");
                param.SetSize(NativeMethods.CW_USEDEFAULT, NativeMethods.CW_USEDEFAULT); 
                param.SetPosition(NativeMethods.CW_USEDEFAULT, NativeMethods.CW_USEDEFAULT);
                param.WindowStyle = NativeMethods.WS_OVERLAPPEDWINDOW;
                _hiddenWindow = new HwndSource(param);
            } 

            return _hiddenWindow; 
        }