2010-09-10 33 views
4

我已經創建了一個外接打開了一個表格,Visual Studio 2008中,如果(在窗體是打開的)用戶打開/關閉一個Visual Studio對話框使用 Form1.Show(this);表單無法獲得焦點一旦失去

框(例如裝配信息),則用戶不能回頭看由插件創建的表單。

有什麼我失蹤,讓用戶返回到窗體?如果我使用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實例中打開項目
  • 打開項目屬性,轉到應用程序選項卡,打開Assembly Information,然後關閉它。
  • +0

    我試圖創建簡單的加載項,但無法重現您的問題。你能告訴我們更多關於你的Form1嗎? – tia 2010-09-13 19:02:58

    +0

    我已經添加了重現步驟。 – JamesMLV 2010-09-14 14:19:04

    回答

    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"); 
         } 
        } 
    } 
    

    創建窗體

    我們需要使用的承載您的外接程序和您的外接程序的實例的應用程序。 這兩個都是已經在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的工具菜單中的應用程序,當我點擊我的外接顯示它的窗口,它的工作。在顯示Assembly對話框時,它也沒有凍結,掛起或者其他任何東西。