表格丢失后无法获得焦点

Jam*_*MLV 4 c# add-in visual-studio-2008

我已经为Visual Studio 2008创建了一个加载项,用于打开表单 Form1.Show(this);

如果(在窗体打开时)用户打开/关闭Visual Studio对话框(例如程序集信息),则用户无法重新关注由插件创建的表单.

是否有一些我错过的东西允许用户返回表单?如果我使用Form1.ShowDialog(this),这不会发生,但我希望用户在我的自定义表单打开时看到程序集信息.

加载项实现IWin32Window使用

public System.IntPtr Handle
{
    get
    {
        return new System.IntPtr(_applicationObject.MainWindow.HWnd);
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:重现的步骤

  • 创建一个visual studio加载项项目
  • 添加对System.Windows.Forms的引用
  • 添加以下内容 public void Exec(...)

    System.Windows.Forms.Form f = new System.Windows.Forms.Form();
    f.Show();
    
    Run Code Online (Sandbox Code Playgroud)

  • 运行加载项,并在已启动的visual studio实例中打开一个项目
  • 打开项目属性,转到"应用程序"选项卡,打开"装配信息",然后将其关闭.
  • Nop*_*ope 5

    感谢您的复制步骤.我能够重现你的问题.

    据我所知,Visual Studio IDE使用Controls而不是Forms.

    我不知道您的表单的功能是什么,我只需在下面添加一个基本示例即可开始使用.

    很容易有很多其他方法可以做到这一点.我不是AddIn开发人员,因此我的知识在该领域是有限的.

    用户控件

    首先,右键单击您的项目并添加新的用户控件.我在我的示例中将我的名字命名为"MyForm",并在其上放置一个简单的按钮,单击时显示"Hello".此用户控件将成为您的表单.

    namespace MyAddin1
    {
        public partial class MyForm : UserControl
        {
            public MyForm()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Hello");
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    创建表单

    我们需要使用托管AddIn的应用程序和AddIn的实例.这些都是已在AddIn项目中声明的成员:_applicationObject和_addInInstance.这些是在OnConnection事件中设置的.

    在下面的代码中,我创建了一个新的工具窗口,在其中托管我的用户控件.我正在使用Windows2.CreateTooWindow2方法来做到这一点.

    我已将示例代码添加到Excec事件中,如下所示.同样,我不确定这是否适合它,但为了展示代码它应该足够了.

    /// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
    /// <param term='commandName'>The name of the command to execute.</param>
    /// <param term='executeOption'>Describes how the command should be run.</param>
    /// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
    /// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
    /// <param term='handled'>Informs the caller if the command was handled or not.</param>
    /// <seealso class='Exec' />
    public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
    {
        object tempObject = null;   // It's required but I'm not sure what one can do with it...
        Windows2 windows2 = null;   // Reference to the window collection displayed in the application host.
        Assembly asm = null;        // The assembly containing the user control.
        Window myWindow = null;     // Will contain the reference of the new Tool Window.
    
        try
        {
            handled = false;
    
            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                if (commandName == "MyAddin1.Connect.MyAddin1")
                {
                    handled = true;
    
                    // Get a reference to the window collection displayed in the application host.
                    windows2 = (Windows2)_applicationObject.Windows;
    
                    // Get the executing assembly.
                    asm = Assembly.GetExecutingAssembly();
    
                    // Create the tool window and insert the user control.
                    myWindow = windows2.CreateToolWindow2(_addInInstance, asm.Location, "MyAddin1.MyForm", "My Tool Window", "{CB2AE2BD-2336-4615-B0A3-C55B9C7794C9}", ref tempObject);
    
                    // Set window properties to make it act more like a modless form.
                    myWindow.Linkable = false;  // Indicates whether the window can be docked with other windows in the IDE or not.
                    myWindow.IsFloating = true; // Indicates whether the window floats over other windows or not.
    
                    // Show the window.
                    myWindow.Visible = true;
    
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    我测试了它确实将我的插件添加到IDE的工具菜单中的应用程序,当我点击我的Addin时它显示了窗口并且它有效.在显示程序集对话框时,它也没有冻结,挂起或任何东西.