【问题标题】:Form cannot receive focus once lost表单丢失后无法获得焦点
【发布时间】:2010-09-10 15:55:04
【问题描述】:

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

如果(在表单打开时)用户打开/关闭 Visual Studio 对话框(例如程序集信息),则用户无法将注意力重新集中在插件创建的表单上。

我是否缺少允许用户返回表单的内容?如果我使用 Form1.ShowDialog(this),则不会发生这种情况,但我希望用户在我的自定义表单打开时查看程序集信息。

插件使用IWin32Window实现

public System.IntPtr Handle
{
    get
    {
        return new System.IntPtr(_applicationObject.MainWindow.HWnd);
    }
}

编辑:重现的步骤

  • 创建 Visual Studio 插件项目
  • 添加对 System.Windows.Forms 的引用
  • 将以下内容添加到public void Exec(...)
    System.Windows.Forms.Form f = new System.Windows.Forms.Form();
    f.Show();
    

  • 运行插件,并在已启动的visual studio实例中打开一个项目
  • 打开项目属性,转到应用程序选项卡,打开程序集信息,然后将其关闭。
  • 【问题讨论】:

    • 我尝试创建简单的加载项,但无法重现您的问题。你能告诉我们更多关于你的Form1吗?
    • 我已经添加了重现的步骤。

    标签: c# visual-studio-2008 add-in


    【解决方案1】:

    感谢您提供复制步骤。我能够重现您的问题。

    据我所知,Visual Studio IDE 使用控件而不是表单。

    不知道您的表单的预期功能是什么,我只是在下面添加了一个基本示例以开始使用。

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

    用户控制

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

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

    创建表单

    我们需要使用托管您的 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);
        }
    }
    

    我测试了应用程序,它确实将我的插件添加到了 IDE 的工具菜单中,当我点击我的插件时,它显示了窗口并且它工作正常。显示组装对话框时,它也没有冻结、挂起或任何其他情况。

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      相关资源
      最近更新 更多