设为首页 收藏本站
查看: 1152|回复: 0

【VS Shell Integrated】在VS.Net 2008 IDE中如何设置直接弹出的WPF窗体的Owner(对<在VS.Net 2008 IDE中直接

[复制链接]

尚未签到

发表于 2015-4-28 10:59:56 | 显示全部楼层 |阅读模式
  在随笔 【VS Shell Integrated】在VS.Net 2008 IDE中直接使用WPF窗体作为弹出窗体 中虽然实现使用WPF窗体, 但是有一个问题,就是在操作系统任务栏切换VS.Net 2008 IDE和弹出的WPF窗口或其它程序窗体时,WPF窗口有时会被 VS.Net 2008 IDE 挡住,也就是跑到VS.Net 2008 IDE的后面去了. 这是因为,虽然使用了ShowDialog方法,但是弹出前没有设置WPF窗体的Owner.
  
  理虽是这个理但是怎么设呢,层层包裹,皮厚的很啊,如下图.
DSC0000.png
  
  怎么办,烧香,请GOOGLE大兄,结果找到下面这文章:
  http://blogs.msdn.com/wpfsdk/archive/2007/04/03/centering-wpf-windows-with-wpf-and-non-wpf-owner-windows.aspx
  
  查看下文时,先看上面引用的文章
  
  但是关键的代码没写,真是杯具啊
  

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

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


public partial class ExplorerControlWPF : System.Windows.Controls.UserControl
{
public ExplorerControlWPF()
{
InitializeComponent();
}
public IntPtr ParentHandle;
  2) 在Winform 用户控件中传递Handle


  public partial class ExplorerControl : System.Windows.Forms.UserControl
{
public ExplorerControl()
{
InitializeComponent();
//
//传ElementHost的Handle
//
//或者传ExplorerControl.Handle
//(this.elementHost1.Child as ExplorerControlWPF).ParentHandle = this.elementHost1.Parent.Handl
//
(this.elementHost1.Child as ExplorerControlWPF).ParentHandle = this.elementHost1.Handle;
}
  3) 经过上述步骤,就可以在WPF用户控件ExplorerControlWPF中使用Handle了


  private void MuNewProject_Click(object sender, RoutedEventArgs e)
{
RegProjectForm RegWin = new RegProjectForm();
WindowInteropHelper helper = new WindowInteropHelper(RegWin);
helper.Owner = this.ParentHandle;
try
{
if (RegWin.ShowDialog() == true)
{
  运行,大功告成!
  
  通过类似的方法,也可以把Tool Window的Handle传给ExplorerControlWPF   
  
  1) 在 ExplorerControl  , 声明一个成员变量 ParentWindow


public partial class ExplorerControl : System.Windows.Forms.UserControl
{
public  ToolWindowPane ParentWindow;
public ExplorerControl()
{
InitializeComponent();
//
//传ElementHost的Handle
//
//或者传ExplorerControl.Handle
//(this.elementHost1.Child as ExplorerControlWPF).ParentHandle = this.elementHost1.Parent.Handl
//
//(this.elementHost1.Child as ExplorerControlWPF).ParentHandle = this.elementHost1.Handle;
}

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




    [Guid("e6a26ad4-7f6d-48e1-b1b4-dfb65654800f")]
public class ExplorerForm : ToolWindowPane
{        
private ExplorerControl ContentControl;
public ExplorerForm() :
base(null)
{
this.Caption = UiResources.ExplorerWindowTitle;
this.BitmapResourceID = 301;
this.BitmapIndex = 1;
this.ContentControl = new ExplorerControl();
//传递自身
this.ContentControl.ParentWindow = this;            
}
3)在 ExplorerControl的 OnLoad事件中传递Tool Window的handle给ExplorerControlWPF.   注意:不是在构造函数里,因为这时ParentWindow还没被赋上引用.

public partial class ExplorerControl : System.Windows.Forms.UserControl
{
public  ToolWindowPane ParentWindow;
public ExplorerControl()
{
InitializeComponent();
//
//传ElementHost的Handle
//
//或者传ExplorerControl.Handle
//(this.elementHost1.Child as ExplorerControlWPF).ParentHandle = this.elementHost1.Parent.Handle
//
//(this.elementHost1.Child as ExplorerControlWPF).ParentHandle = this.elementHost1.Handle;
}
protected override void OnLoad(EventArgs e)
{
(this.elementHost1.Child as ExplorerControlWPF).ParentHandle = this.ParentWindow.Window.Handle;
base.OnLoad(e);
}  4) 经过上述步骤,就可以在WPF用户控件ExplorerControlWPF中使用Handle了


  private void MuNewProject_Click(object sender, RoutedEventArgs e)
{
RegProjectForm RegWin = new RegProjectForm();
WindowInteropHelper helper = new WindowInteropHelper(RegWin);
helper.Owner = this.ParentHandle;
try
{
if (RegWin.ShowDialog() == true)
{  运行,大功告成!
  这三个Handle,传哪一个都可以,效果完全一致.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-61502-1-1.html 上篇帖子: SHELL技巧:处理文件名中的那些空格 下篇帖子: shell脚本中一些特殊符号
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表