Example #1
0
        /// <summary>
        /// Handles a basic <see cref="MessageBoxMessage" /> on a window by showing built-in <see cref="MessageBox"/>
        /// and invokes the callback.
        /// </summary>
        /// <param name="owner">The owner.</param>
        public void HandleWithPlatform(Window owner)
        {
            MessageBoxResult res = MessageBoxResult.None;
            if (owner == null)
            {
                res = MessageBox.Show(Content, Caption, Button, Icon, DefaultResult, Options);
            }
            else
            {
                res = MessageBox.Show(owner, Content, Caption, Button, Icon, DefaultResult, Options);
            }

            if (Callback != null)
            {
                if (owner == null || owner.CheckAccess())
                {
                    Callback(res);
                }
                else
                {
                    owner.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        Callback(res);
                    }));
                }
            }
        }
Example #2
0
        internal int RunInternal(Window window)
        {
            VerifyAccess();

#if DEBUG_CLR_MEM
            if (CLRProfilerControl.ProcessIsUnderCLRProfiler &&
               (CLRProfilerControl.CLRLoggingLevel >= CLRProfilerControl.CLRLogState.Performance))
            {
                CLRProfilerControl.CLRLogWriteLine("Application_Run");
            }
#endif // DEBUG_CLR_MEM

            EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordGeneral | EventTrace.Keyword.KeywordPerf, EventTrace.Event.WClientAppRun);

            //
            // NOTE: hamidm 08/06/04
            // PS Windows OS Bug # 901085 (Can't create app and do run/shutdown followed
            // by run/shutdown)
            //
            // Devs could write the following code
            //
            // Application app = new Application();
            // app.Run();
            // app.Run();
            //
            // In this case, we should throw an exception when Run is called for the second time.
            // When app is shutdown, _appIsShutdown is set to true.  If it is true here, then we
            // throw an exception
            if (_appIsShutdown == true)
            {
                throw new InvalidOperationException(SR.Get(SRID.CannotCallRunMultipleTimes, this.GetType().FullName));
            }

            if (window != null)
            {
                if (window.CheckAccess() == false)
                {
                    throw new ArgumentException(SR.Get(SRID.WindowPassedShouldBeOnApplicationThread, window.GetType().FullName, this.GetType().FullName));
                }

                if (WindowsInternal.HasItem(window) == false)
                {
                    WindowsInternal.Add(window);
                }

                if (MainWindow == null)
                {
                    MainWindow = window;
                }

                if (window.Visibility != Visibility.Visible)
                {
                    Dispatcher.BeginInvoke(
                        DispatcherPriority.Send,
                        (DispatcherOperationCallback) delegate(object obj)
                        {
                            Window win = obj as Window;
                            win.Show();
                            return null;
                        },
                        window);
                }
            }

            EnsureHwndSource();

            //Even if the subclass app cancels the event we still want to create and run the dispatcher
            //so that when the app explicitly calls Shutdown, we have a dispatcher to service the posted
            //Shutdown DispatcherOperationCallback

            // Invoke the Dispatcher synchronously if we are not in the browser
            if (!BrowserInteropHelper.IsBrowserHosted)
            {
                RunDispatcher(null);
            }

            return _exitCode;
        }
Example #3
0
        private static bool _SetWindowThemeAttribute(Window window, bool showCaption, bool showIcon)
        {
            bool isGlassEnabled;

            Assert.IsNotNull(window);
            Assert.IsTrue(window.CheckAccess());

            // This only is expected to work if Aero glass is enabled.
            try
            {
                isGlassEnabled = NativeMethods.DwmIsCompositionEnabled();
            }
            catch (DllNotFoundException)
            {
                // Not an error.  Just not on Vista so we're not going to get glass.
                return false;
            }

            IntPtr hwnd = new WindowInteropHelper(window).Handle;
            if (IntPtr.Zero == hwnd)
            {
                throw new InvalidOperationException("Window must be shown before we can modify attributes.");
            }

            var options = new WTA_OPTIONS
            {
                dwMask = (WTNCA.NODRAWCAPTION | WTNCA.NODRAWICON)
            };
            if (isGlassEnabled)
            {
                if (!showCaption)
                {
                    options.dwFlags |= WTNCA.NODRAWCAPTION;
                }
                if (!showIcon)
                {
                    options.dwFlags |= WTNCA.NODRAWICON;
                }
            }

            NativeMethods.SetWindowThemeAttribute(hwnd, WINDOWTHEMEATTRIBUTETYPE.WTA_NONCLIENT, ref options, WTA_OPTIONS.Size);

            bool addHook = !_attributedWindows.ContainsKey(hwnd);

            if (addHook)
            {
                HwndSourceHook hook = delegate(IntPtr unusedHwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
                {
                    if (WM.DWMCOMPOSITIONCHANGED == (WM)msg)
                    {
                        _SetWindowThemeAttribute(window, showCaption, showIcon);
                        handled = false;
                    }
                    return IntPtr.Zero;
                };

                _attributedWindows.Add(hwnd, hook);
                HwndSource.FromHwnd(hwnd).AddHook(hook);
                window.Closing += _OnAttributedWindowClosing;
            }

            return isGlassEnabled;
        }
Example #4
0
        /// <summary>
        /// Handles a basic <see cref="MessageBoxMessage" /> on a window by showing a <see cref="ModernMessageBox" />
        /// and invokes the callback.
        /// </summary>
        /// <param name="owner">The owner.</param>
        public void HandleWithModern(Window owner)
        {
            if (owner == null) { throw new ArgumentNullException("owner"); }

            var res = ModernMessageBox.Show(owner, Content, Caption, Button, Icon, DefaultResult);
            if (Callback != null)
            {
                if (owner == null || owner.CheckAccess())
                {
                    Callback(res);
                }
                else
                {
                    owner.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        Callback(res);
                    }));
                }
            }
        }
Example #5
0
        private static bool _ExtendGlassFrameInternal(Window window, Thickness margin)
        {
            Assert.IsNotNull(window);
            Assert.IsTrue(window.CheckAccess());

            // Expect that this might be called on OSes other than Vista.
            if (!Utility.IsOSVistaOrNewer)
            {
                // Not an error.  Just not on Vista so we're not going to get glass.
                return false;
            }

            IntPtr hwnd = new WindowInteropHelper(window).Handle;
            if (IntPtr.Zero == hwnd)
            {
                throw new InvalidOperationException("Window must be shown before extending glass.");
            }

            HwndSource hwndSource = HwndSource.FromHwnd(hwnd);

            bool isGlassEnabled = NativeMethods.DwmIsCompositionEnabled();

            if (!isGlassEnabled)
            {
                window.Background = SystemColors.WindowBrush;
                hwndSource.CompositionTarget.BackgroundColor = SystemColors.WindowColor;
            }
            else
            {
                // Apply the transparent background to both the Window and the HWND
                window.Background = Brushes.Transparent;
                hwndSource.CompositionTarget.BackgroundColor = Colors.Transparent;

                // Thickness is going to be DIPs, need to convert to system coordinates.
                Point deviceTopLeft = DpiHelper.LogicalPixelsToDevice(new Point(margin.Left, margin.Top));
                Point deviceBottomRight = DpiHelper.LogicalPixelsToDevice(new Point(margin.Right, margin.Bottom));

                var dwmMargin = new MARGINS
                {
                    // err on the side of pushing in glass an extra pixel.
                    cxLeftWidth = (int)Math.Ceiling(deviceTopLeft.X),
                    cxRightWidth = (int)Math.Ceiling(deviceBottomRight.X),
                    cyTopHeight = (int)Math.Ceiling(deviceTopLeft.Y),
                    cyBottomHeight = (int)Math.Ceiling(deviceBottomRight.Y),
                };

                NativeMethods.DwmExtendFrameIntoClientArea(hwnd, ref dwmMargin);
            }

            // Even if glass isn't currently enabled, add the hook so we can appropriately respond
            // if that changes.

            bool addHook = !_extendedWindows.ContainsKey(hwnd);

            if (addHook)
            {
                HwndSourceHook hook = delegate(IntPtr innerHwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
                {
                    if (WM.DWMCOMPOSITIONCHANGED == (WM)msg)
                    {
                        _ExtendGlassFrameInternal(window, margin);
                        handled = false;
                    }
                    return IntPtr.Zero;
                };

                _extendedWindows.Add(hwnd, hook);
                hwndSource.AddHook(hook);
                window.Closing += _OnExtendedWindowClosing;
            }

            return isGlassEnabled;
        }
Example #6
0
        internal static void ShowView(Window view, bool show, bool updateActive=true)
        {
            if (!view.CheckAccess())
            {
                var action = new Action(() => ShowView(view, show, updateActive));
                view.Dispatcher.Invoke(action);
                return;
            }

            if (updateActive)
                ConfigurationHelper.Configuration.Repositories.FirstOrDefault(r => r.View == view).isActive = show;

            if (show)
            {
                if (view.Visibility == Visibility.Hidden) Position(view);
                view.Show();
            }
            else 
                view.Hide();
        }
Example #7
0
        private static bool _ExtendGlassFrameInternal(Window window, Thickness margin)
        {
            Assert.IsNotNull(window);
            Assert.IsTrue(window.CheckAccess());

            // Expect that this might be called on OSes other than Vista.
            if (Environment.OSVersion.Version.Major < 6)
            {
                // Not an error.  Just not on Vista so we're not going to get glass.
                return false;
            }

            IntPtr hwnd = new WindowInteropHelper(window).Handle;
            if (IntPtr.Zero == hwnd)
            {
                throw new InvalidOperationException("Window must be shown before extending glass.");
            }

            HwndSource hwndSource = HwndSource.FromHwnd(hwnd);

            bool isGlassEnabled;
            NativeMethods.DwmIsCompositionEnabled(out isGlassEnabled);

            if (!isGlassEnabled)
            {
                window.Background = SystemColors.WindowBrush;
                hwndSource.CompositionTarget.BackgroundColor = SystemColors.WindowColor;
            }
            else
            {
                // Apply the transparent background to both the Window and HWND
                window.Background = Brushes.Transparent;
                hwndSource.CompositionTarget.BackgroundColor = Colors.Transparent;

                var dwmMargin = new MARGINS(margin);
                NativeMethods.DwmExtendFrameIntoClientArea(hwnd, ref dwmMargin);
            }

            // Even if glass isn't currently enabled, add the hook so we can appropriately respond
            // if that changes.

            bool addHook = !_extendedWindows.ContainsKey(hwnd);

            if (addHook)
            {
                HwndSourceHook hook = delegate(IntPtr innerHwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
                {
                    if (WM.DWMCOMPOSITIONCHANGED == (WM)msg)
                    {
                        _ExtendGlassFrameInternal(window, margin);
                        handled = false;
                    }
                    return IntPtr.Zero;
                };

                _extendedWindows.Add(hwnd, hook);
                hwndSource.AddHook(hook);
                window.Closing += _OnExtendedWindowClosing;
            }

            return isGlassEnabled;
        }
Example #8
0
        /// <summary>
        /// Handles the <see cref="ChooseFileMessage" /> on a window by showing a <see cref="FileDialog" /> based on the message options.
        /// </summary>
        /// <param name="owner">The owner.</param>
        public virtual void HandleWithPlatform(Window owner)
        {
            FileDialog dialog = null;

            switch (Purpose)
            {
                case FilePurpose.OpenMultiple:
                    var d = new OpenFileDialog();
                    d.Multiselect = true;
                    dialog = d;
                    break;
                case FilePurpose.OpenSingle:
                    dialog = new OpenFileDialog();
                    break;
                case FilePurpose.Save:
                    dialog = new SaveFileDialog();
                    break;
            }

            if (dialog != null)
            {
                dialog.Title = Caption;

                if (!string.IsNullOrEmpty(InitialFolder))
                    dialog.InitialDirectory = InitialFolder;
                if (!string.IsNullOrEmpty(InitialFileName))
                    dialog.FileName = InitialFileName;
                if (!string.IsNullOrEmpty(Filters))
                    dialog.Filter = Filters;

                var result = owner == null ? dialog.ShowDialog().GetValueOrDefault() : dialog.ShowDialog(owner).GetValueOrDefault();
                if (result)
                {
                    if (owner == null || owner.CheckAccess())
                    {
                        DoCallback(dialog.FileNames);
                    }
                    else
                    {
                        owner.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            DoCallback(dialog.FileNames);
                        }));
                    }
                }
            }
        }
Example #9
0
 public static void Set_Title(Window winx, string text, bool waitUntilReturn = false)
 {
     Action settitle = () => winx.Title = "SAPP Remote" + text;
     if (winx.CheckAccess()) {
         settitle();
     } else if (waitUntilReturn) {
         winx.Dispatcher.Invoke(settitle);
     } else {
         winx.Dispatcher.BeginInvoke(settitle);
     }
 }