解决 WPF AllowsTransparency = true 和 Webbrowser 等控件显示冲突

代码:

      

 public class FormsWebBrowser
    {
        Window _owner;
        FrameworkElement _placementTarget;
        Form _form;
        AxAcroPDF _wb = new AxAcroPDF();

        public AxAcroPDF WebBrowser { get { return _wb; } }

        public FormsWebBrowser(FrameworkElement placementTarget,string path) //, Window mainWindow)
        {
            _placementTarget = placementTarget;
            Window owner = Window.GetWindow(placementTarget); 
            //Window owner = mainWindow; //page中传入window
            Debug.Assert(owner != null);
            _owner = owner;

            _form = new Form();
            _form.Opacity = owner.Opacity;
            _form.ShowInTaskbar = false;
            _form.FormBorderStyle = FormBorderStyle.None;
            _wb.Dock = DockStyle.Fill;
            _form.Controls.Add(_wb);
            _form.Load += (a,b) => {
                _wb.LoadFile(path);
            };
            owner.LocationChanged += delegate { OnSizeLocationChanged(); };
            _placementTarget.SizeChanged += delegate { OnSizeLocationChanged(); };

            if (owner.IsVisible)
                InitialShow();
            else
                owner.SourceInitialized += delegate
                {
                    InitialShow();
                };

            DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(UIElement.OpacityProperty, typeof(Window));
            dpd.AddValueChanged(owner, delegate { _form.Opacity = _owner.Opacity; });

            _form.FormClosing += delegate { _owner.Close(); };
        }

        void InitialShow()
        {
            NativeWindow owner = new NativeWindow();
            owner.AssignHandle(((HwndSource)HwndSource.FromVisual(_owner)).Handle);
            _form.Show(owner);
            owner.ReleaseHandle();
        }

        DispatcherOperation _repositionCallback;

        void OnSizeLocationChanged()
        {
            if (_repositionCallback == null)
                _repositionCallback = _owner.Dispatcher.BeginInvoke(new Action(Reposition), DispatcherPriority.Input);
        }

        void Reposition()
        {
            _repositionCallback = null;

            Point offset = _placementTarget.TranslatePoint(new Point(), _owner);
            Point size = new Point(_placementTarget.ActualWidth, _placementTarget.ActualHeight);
            HwndSource hwndSource = (HwndSource)HwndSource.FromVisual(_owner);
            CompositionTarget ct = hwndSource.CompositionTarget;
            offset = ct.TransformToDevice.Transform(offset);
            size = ct.TransformToDevice.Transform(size);

            Win32.POINT screenLocation = new Win32.POINT(offset);
            Win32.ClientToScreen(hwndSource.Handle, ref screenLocation);
            Win32.POINT screenSize = new Win32.POINT(size);

            Win32.MoveWindow(_form.Handle, screenLocation.X, screenLocation.Y, screenSize.X, screenSize.Y, true);
        }
    }

    static class Win32
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;

            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
            public POINT(Point pt)
            {
                X = Convert.ToInt32(pt.X);
                Y = Convert.ToInt32(pt.Y);
            }
        };

        [DllImport("user32.dll")]
        internal static extern bool ClientToScreen(IntPtr hWnd, ref POINT lpPoint);

        [DllImport("user32.dll")]
        internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

    }
View Code

   当前代码段中,使用的是显示pdf 控件 AxAcroPDF   出现,显示问题,问题表现为:当窗体属性设置为 AllowsTransparency = true,

AxAcroPDF  控件区域透明,内容存在,但不可见,或者是使用Webbrowser

以上代码直封装为一个类,如果要使用Webbrowser,AxAcroPDF 替换为  Webbrowser 类即可。

原文是Webbrowser显示问题,经过简单修改和传参,解决了与其雷同问题的 AxAcroPDF  显示的问题。

调用时,代码如下:

 WpfForUnity3D.Code.FormsWebBrowser web = new WpfForUnity3D.Code.FormsWebBrowser(this.showCon, filePath); //或者直接this

// this.showCon 为WPF 中你所要显示控件区域的容器ID或Name , 也可为this

// filepath 为要显示的wpf 的文件名

原文地址:https://www.cnblogs.com/zhiyin/p/6054567.html