解決 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 的文件名

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM