none
WPF工程里使用Windows 窗体控件的问题. 急 RRS feed

  • 问题

  • 按MSDN里的文章,能够实现其例子,即在Windows Presentation Foundation 中承载 Windows 窗体控件.

    可是在我的工程里, Windows 窗体控件不可见.只有位置轮廓.怎么解决啊?

    同样的代码,在我的工程里运行, Windows 窗体控件不可见.

              System.Windows.Forms.Integration.WindowsFormsHost host =
                    new System.Windows.Forms.Integration.WindowsFormsHost();

                // Create the MaskedTextBox control.
                MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");

                // Assign the MaskedTextBox control as the host control's child.
                host.Child = mtbDate;

                // Add the interop host control to the Grid
                // control's collection of child controls.
                this.grid1.Children.Add(host);

    2011年1月8日 18:49

答案

  •  "Airspace“限制了一个GDI绘制的组件必须在WPF的组件上面,即使你可以透明化WPF组件,但这样的效果你无法影响到GDI的组件;因为系统规定,屏幕上的任意一点有且仅有一种技术可以绘制他,GDI、WPF或者Direcr3D。

    所以,你在实现这样的效果的同时,需要权衡下,是否要牺牲一点WPF的特型,不然很多东西是不可能完美的。

     

    我看你只是想用Winform里面的MaskedTextBox ,其实有很多WPF版本的,你倒可以试试: 


    Best day, Best life
    2011年1月10日 17:58
  • 你好,

    正如Jarrey所说了,由于AirSpace的问题,如果想要在WPF中显示一个WindowsForms控件,那么窗体的AllowTransparency就必须设置为False. 以我的了解,根据当前WPF的设计,在WPF的透明窗体中显示一个GDI+的子窗体是不可能实现的。这主要是因为WPF采用Application-Provided Content模式的Layered Windows来实现透明窗体,而在这种模式下,窗体是无法收到WM_PAINT消息的。具体的原因DwayneNeed在他的博客中详尽的解释了,有条件的话可以看一下这篇博客来了解更多的信息。

    我觉得如果你只是要使用MaskedTextBox控件的话,最好还是像Jarrey说的,使用一个WPF的控件。

    不然的话,可能最好的解决方法也就是在你当前的透明窗体上,创建一个Owner为这个透明窗体的另一个窗体用以承载这个WindowsForms控件,然后手动同步两个窗体的位置,让承载WindowsForms控件的窗体看上去像是透明窗体的组件一样。具体的步骤在Chango.V在他的博客中有提到。

    希望这对你有帮助。


    Best regards,
    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年1月11日 9:36
    版主

全部回复