I have created an Add-In for Visual Studio 2008 that opens up a form, using
Form1.Show(this);
If (while the form is open) the user opens/closes a Visual Studio dialog box, (such as Assembly Information) then the user cannot focus back on the form created by the addin.
Is there something I'm missing to allow the user to return to the form? This doesn't occur if I use Form1.ShowDialog(this)
, but I would like the user to see the Assembly Information while my custom form is open.
The add-in implements IWin32Window
using
public System.IntPtr Handle
{
get
{
return new System.IntPtr(_applicationObject.MainWindow.HWnd);
}
}
EDIT: Steps to reproduce
public void Exec(...)
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
f.Show();
Thank you for the reproduction steps. I was able to reproduce your issue.
As far as I was able to find out, the Visual Studio IDE uses Controls rather than Forms.
Not knowing what the intended functionality is of your form I simply added a basic example below to get started.
There could easily be many other ways of doing this. I'm no AddIn developer and as such my knowledge is limited in that area.
The User Control
First, right-click your project and add a new user control. I named mine "MyForm" in my example and placed a simple button on it, displaying "Hello" when clicked. This user control is going to be your form.
namespace MyAddin1
{
public partial class MyForm : UserControl
{
public MyForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}
}
}
Creating the Form
We need to use the application which is hosting your AddIn and the instance of your AddIn. Both those are members already declared in your AddIn project: _applicationObject and _addInInstance. Those are set in the OnConnection event.
In the below code I create a new tool window, hosting my user control in it. I'm using the Windows2.CreateTooWindow2 Method to do that.
I have added my sample code into the Excec event as seen below. Again, I'm not sure if that is the right place for it but for demonstrating the code it should suffice.
/// <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);
}
}
I tested the application it did add my add-in to the tools menu of the IDE and when I clicked my Addin it showed the window and it worked. It also did not freeze, hang or anything when showing the Assembly Dialog.
Perhaps these discussions might help?
Can I set ShowDialog() to not be topmost? How to get the handle of the topmost form in a WinForm app?
精彩评论