您的位置:首页 > 运维架构 > Shell

【VS Shell Integrated】在VS.Net 2008 IDE中如何设置直接弹出的WPF窗体的Owner(对<在VS.Net 2008 IDE中直接使用WPF窗体作为弹出窗体>的补充

2010-03-18 10:24 661 查看
在随笔【VSShellIntegrated】在VS.Net2008IDE中直接使用WPF窗体作为弹出窗体中虽然实现使用WPF窗体,但是有一个问题,就是在操作系统任务栏切换VS.Net2008IDE和弹出的WPF窗口或其它程序窗体时,WPF窗口有时会被VS.Net2008IDE挡住,也就是跑到VS.Net2008IDE的后面去了.这是因为,虽然使用了ShowDialog方法,但是弹出前没有设置WPF窗体的Owner.

理虽是这个理但是怎么设呢,层层包裹,皮厚的很啊,如下图.





怎么办,烧香,请GOOGLE大兄,结果找到下面这文章:

http://blogs.msdn.com/wpfsdk/archive/2007/04/03/centering-wpf-windows-with-wpf-and-non-wpf-owner-windows.aspx

查看下文时,先看上面引用的文章

但是关键的代码没写,真是杯具啊

//Getthehandletothenon-WPFownerwindow
IntPtrownerWindowHandle=...;//GethWndfornon-WPFwindow
没法子,发信给作者,问他怎么能直接得到ToolWindow或IDE的handle.
结果是微软DeveloperDivisionUserEducation的一个姐姐(不知年龄)给了回复.不过,来来回回,好像她还是没完全搞明白程序结构.
只给了一些基本的提示,有些用处,但是没解决根本性问题,不过还是非常感谢这位姐姐.
我这一看,看来玩直接的是不行啦,咱变通一下吧,用点间接方法吧,很简单,就是通过实例成员,传!

1)在WPF用户控件定义一个成员变量ParentHandle

publicpartialclassExplorerControlWPF:System.Windows.Controls.UserControl
{
publicExplorerControlWPF()
{
InitializeComponent();
}

publicIntPtrParentHandle;


2)在Winform用户控件中传递Handle

publicpartialclassExplorerControl:System.Windows.Forms.UserControl
{
publicExplorerControl()
{
InitializeComponent();

//
//传ElementHost的Handle
//
//或者传ExplorerControl.Handle
//(this.elementHost1.ChildasExplorerControlWPF).ParentHandle=this.elementHost1.Parent.Handl
//

(this.elementHost1.ChildasExplorerControlWPF).ParentHandle=this.elementHost1.Handle;
}


3)经过上述步骤,就可以在WPF用户控件ExplorerControlWPF中使用Handle了

privatevoidMuNewProject_Click(objectsender,RoutedEventArgse)
{
RegProjectFormRegWin=newRegProjectForm();
WindowInteropHelperhelper=newWindowInteropHelper(RegWin);
helper.Owner=this.ParentHandle;

try
{
if(RegWin.ShowDialog()==true)
{


运行,大功告成!

通过类似的方法,也可以把ToolWindow的Handle传给ExplorerControlWPF

1)在ExplorerControl,声明一个成员变量ParentWindow

publicpartialclassExplorerControl:System.Windows.Forms.UserControl
{
publicToolWindowPaneParentWindow;
publicExplorerControl()
{
InitializeComponent();

//
//传ElementHost的Handle
//
//或者传ExplorerControl.Handle
//(this.elementHost1.ChildasExplorerControlWPF).ParentHandle=this.elementHost1.Parent.Handl
//

//(this.elementHost1.ChildasExplorerControlWPF).ParentHandle=this.elementHost1.Handle;
}


2)在ToolWindow里,把自身传给ExplorerControl的实例

[Guid("e6a26ad4-7f6d-48e1-b1b4-dfb65654800f")]
publicclassExplorerForm:ToolWindowPane
{
privateExplorerControlContentControl;

publicExplorerForm():
base(null)
{

this.Caption=UiResources.ExplorerWindowTitle;
this.BitmapResourceID=301;
this.BitmapIndex=1;
this.ContentControl=newExplorerControl();

//传递自身
this.ContentControl.ParentWindow=this;
}

3)在ExplorerControl的OnLoad事件中传递ToolWindow的handle给ExplorerControlWPF.
注意:不是在构造函数里,因为这时ParentWindow还没被赋上引用.

publicpartialclassExplorerControl:System.Windows.Forms.UserControl
{
publicToolWindowPaneParentWindow;
publicExplorerControl()
{
InitializeComponent();

//
//传ElementHost的Handle
//
//或者传ExplorerControl.Handle
//(this.elementHost1.ChildasExplorerControlWPF).ParentHandle=this.elementHost1.Parent.Handle
//

//(this.elementHost1.ChildasExplorerControlWPF).ParentHandle=this.elementHost1.Handle;
}

protectedoverridevoidOnLoad(EventArgse)
{
(this.elementHost1.ChildasExplorerControlWPF).ParentHandle=this.ParentWindow.Window.Handle;
base.OnLoad(e);
}

4)经过上述步骤,就可以在WPF用户控件ExplorerControlWPF中使用Handle了

privatevoidMuNewProject_Click(objectsender,RoutedEventArgse)
{
RegProjectFormRegWin=newRegProjectForm();
WindowInteropHelperhelper=newWindowInteropHelper(RegWin);
helper.Owner=this.ParentHandle;

try
{
if(RegWin.ShowDialog()==true)
{

运行,大功告成!

这三个Handle,传哪一个都可以,效果完全一致.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
章节导航