Пример #1
0
        /// <summary>
        /// Creates new instance of EditControl
        /// </summary>
        /// <param name="placementTarget">FrameworkElement on WPF Window holding the EditControl</param>
        public EditControl(FrameworkElement placementTarget)
        {
            //set placement target
            _PlacementTarget = placementTarget;
            //get WPF window
            var owner = Window.GetWindow(placementTarget);

            if (owner == null)
            {
                return;
            }
            //store WPF window
            _WpfWindow = owner;
            //create new WinForm
            _WinForm = new EditContainer
            {
                Opacity         = owner.Opacity,
                StartPosition   = FormStartPosition.CenterScreen,
                ShowInTaskbar   = false,
                FormBorderStyle = FormBorderStyle.None,
                AllowDrop       = true
            };
            //get WPF window background
            var brush = owner.Background as SolidColorBrush;

            //set WinForm backcolor
            if (brush != null)
            {
                _WinForm.BackColor = Color.FromArgb(brush.Color.A, brush.Color.R, brush.Color.G, brush.Color.B);
            }
            //add PNRichEditBox to WinForm controls
            _WinForm.Controls.Add(_EditBox);

            //set event handlers for size and location changes
            owner.LocationChanged        += delegate { OnSizeLocationChanged(); };
            _PlacementTarget.SizeChanged += delegate { OnSizeLocationChanged(); };

            if (owner.IsVisible)
            {
                //if WPF window is visible - show WinForm
                InitialShow();
            }
            else
            {
                //otherwise set handlers for SourceInitialized (in order to show WinForm) and Loaded events
                owner.SourceInitialized += delegate { InitialShow(); };
                owner.Loaded            += delegate { _WinForm.Opacity = owner.Opacity; };
            }
            //add handler for WPF window opacity changes
            DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(UIElement.OpacityProperty, typeof(Window));

            dpd.AddValueChanged(owner, delegate { _WinForm.Opacity = _WpfWindow.Opacity; });
            //add handler for WinForm FormClosing event
            _WinForm.FormClosing += delegate { _WpfWindow.Close(); };
        }
Пример #2
0
        /// <summary>
        /// Creates new instance of EditControl
        /// </summary>
        /// <param name="placementTarget">FrameworkElement on WPF Window holding the EditControl</param>
        public EditControl(FrameworkElement placementTarget)
        {
            //set placement target
            _PlacementTarget = placementTarget;
            //get WPF window
            var owner = Window.GetWindow(placementTarget);

            if (owner == null) return;
            //store WPF window
            _WpfWindow = owner;
            //create new WinForm
            _WinForm = new EditContainer
            {
                Opacity = owner.Opacity,
                StartPosition = FormStartPosition.CenterScreen,
                ShowInTaskbar = false,
                FormBorderStyle = FormBorderStyle.None,
                AllowDrop = true
            };
            //get WPF window background
            var brush = owner.Background as SolidColorBrush;
            //set WinForm backcolor
            if (brush != null)
            {
                _WinForm.BackColor = Color.FromArgb(brush.Color.A, brush.Color.R, brush.Color.G, brush.Color.B);
            }
            //add PNRichEditBox to WinForm controls
            _WinForm.Controls.Add(_EditBox);

            //set event handlers for size and location changes
            owner.LocationChanged += delegate { OnSizeLocationChanged(); };
            _PlacementTarget.SizeChanged += delegate { OnSizeLocationChanged(); };

            if (owner.IsVisible)
                //if WPF window is visible - show WinForm
                InitialShow();
            else
            {
                //otherwise set handlers for SourceInitialized (in order to show WinForm) and Loaded events
                owner.SourceInitialized += delegate { InitialShow(); };
                owner.Loaded += delegate { _WinForm.Opacity = owner.Opacity; };
            }
            //add handler for WPF window opacity changes
            DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(UIElement.OpacityProperty, typeof(Window));
            dpd.AddValueChanged(owner, delegate { _WinForm.Opacity = _WpfWindow.Opacity; });
            //add handler for WinForm FormClosing event
            _WinForm.FormClosing += delegate { _WpfWindow.Close(); };
        }