locked
C# code to get main window of devenv.exe RRS feed

  • Question

  • Hi,

    I have a vs2012 extension and need to get the main window of devenv.exe. I am getting the correct window handle (I've checked it is the one that is shown by spy++), but the NativeWindow.FromHandle returns null for it. Here is the code snippet:

    // returns correct process object
    Process p = Process.GetCurrentProcess();
    // returns correct window handle
    IntPtr mwh = p.MainWindowHandle;
    // returns null?!
    NativeWindow w = NativeWindow.FromHandle(mwh);

    Originally I needed do this from my TFS check-in policy but when I ran into the problem I though this might be something specific to TFS client or Team Explorer window. So I tried to do it another way. I created an extension using VSPackage project template with a menu item added under the Tools menu and tried to get the window object from within the Packag.MenuItemCallback method, but it still returns null.

    Any idea what I might be doing wrong?

    Thanks,
    Gevorg

    • Moved by Amanda Zhu Wednesday, May 14, 2014 5:52 AM
    Tuesday, May 13, 2014 3:04 PM

Answers

  • Hi,

    >>I am getting the correct window handle (I've checked it is the one that is shown by spy++), but the NativeWindow.FromHandle returns null for it. Here is the code snippet:

    I have checked the NativeWindow.FromHandle method. You want to get a window handle. In fact, it is related with Windows Form.

    Here are 2 threads about getting the window handle for your reference.

    How to I get the window handle by giving the process name that is running? 

    Return Window handle by it's name / title  They both include the code snippet.

    If they can not help you, please ask in C# forum for better response.

    >>I created an extension using VSPackage project template with a menu item added under the Tools menu and tried to get the window object from within the Packag.MenuItemCallback method, but it still returns null.

    I suggest you read this document " Walkthrough: Creating a VSPackage ". And pay attention to the content under "changing the menu command handler".

    You want to get a window object and the following APIs are helpful.

    ActiveWindow : Returns the currently active window, or the top-most window if no others are active.

    MainWindow : Gets a Window object representing the main development environment window.

    WindowConfigurations : Gets the WindowConfigurations collection, representing all available window configurations.

    Windows : Gets a Windows collection containing the windows that display in the object.

    Best regards,


    • Edited by Anna Cc Tuesday, May 20, 2014 9:57 AM
    • Marked as answer by Anna Cc Wednesday, June 4, 2014 2:12 PM
    Tuesday, May 20, 2014 9:56 AM
  • Hi Gevorg,

    I never said invoking a modal dialog is unsupported. Attempting to get at an implementation detail like an HWND typically is. As noted earlier, a lot of the windows you now see in Visual Studio are WPF based, and are no longer represented as native HWNDs.

    If you're building a VSPackage, just derive your dialog from the Microsoft.VisualStudio.PlatformUI.DialogWindow and invoke it via it's ShowModal method, and the underlying framework will take care of the modality.

    If by chance you cannot do this (for example if your dialog is native or winform based), use the IVsUIShell.EnableModeless API to set the IDE's modality.

    Sincerely,


    Ed Dore

    • Marked as answer by Anna Cc Wednesday, June 4, 2014 2:12 PM
    Monday, May 26, 2014 12:25 AM

All replies

  • Hi Gevorg,

    I moved this thread to Visual Studio Integrate forum for better support because

    VS General Question forum mainly discusses the usage of Visual Studio IDE such as WPF & SL designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System and Visual Studio Editor but your issue is more related to Visual Studio Extension Development.

    Thanks,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 14, 2014 5:54 AM
  • Hi,

    >>I am getting the correct window handle (I've checked it is the one that is shown by spy++), but the NativeWindow.FromHandle returns null for it. Here is the code snippet:

    I have checked the NativeWindow.FromHandle method. You want to get a window handle. In fact, it is related with Windows Form.

    Here are 2 threads about getting the window handle for your reference.

    How to I get the window handle by giving the process name that is running? 

    Return Window handle by it's name / title  They both include the code snippet.

    If they can not help you, please ask in C# forum for better response.

    >>I created an extension using VSPackage project template with a menu item added under the Tools menu and tried to get the window object from within the Packag.MenuItemCallback method, but it still returns null.

    I suggest you read this document " Walkthrough: Creating a VSPackage ". And pay attention to the content under "changing the menu command handler".

    You want to get a window object and the following APIs are helpful.

    ActiveWindow : Returns the currently active window, or the top-most window if no others are active.

    MainWindow : Gets a Window object representing the main development environment window.

    WindowConfigurations : Gets the WindowConfigurations collection, representing all available window configurations.

    Windows : Gets a Windows collection containing the windows that display in the object.

    Best regards,


    • Edited by Anna Cc Tuesday, May 20, 2014 9:57 AM
    • Marked as answer by Anna Cc Wednesday, June 4, 2014 2:12 PM
    Tuesday, May 20, 2014 9:56 AM
  • Do you actually need NativeWindow?. VisualStudio is a WPF application. So you can easily get WPF System.Windows.Window object:

    var win = System.Windows.Application.Current.MainWindow;

    P.S. Do not foget to add references to System.Xaml, PresentationFramework, PresentationCore & WindowsBase assemblies

    Tuesday, May 20, 2014 5:17 PM
  • Hi Gevorg,

    That would be a very unsupported way of doing things. What sort of VS2012 extension are you building (a VSPackage or Add-in)? And what exactly are you hoping to do with an HWND?

    Since VS 2010, most of the IDE is now WPF based, and no longer Win32/HWND based. Additionally, the VSSDK and EnvDTE automation model, both expose a lot of functionality for extensibility purposes, but we don't expose the HWND directly.

    Thanks,


    Ed Dore

    Tuesday, May 20, 2014 5:46 PM
  • Hi Vitevic,

    Thanks for your reply. At first this seemed just what I wanted, but it wasn't :). the System.Windows.Application.Current.MainWindow is of type System.Windows.Window, while the ShowDialog requires IWin32Window and there is no direct conversion from one to another.

    However while lookign for way to get IWin32Window from System.Windows.Window I came across discussion that did exactly what I wanted. It is not the most elegant solution but hell, it does the trick so I am happy. Here is the link:

    http://stackoverflow.com/questions/10296018/get-system-windows-forms-iwin32window-from-wpf-window

    The pros of this approach is that I don't have to use the WPF classes and add all the references to my assembly, the cons is addition of dummy wrapper class. If this is so easy (and honestly this seems like a pretty generic task to me), why this has not been implemented in the .Net?

    Thanks,
    Gevorg

    Thursday, May 22, 2014 10:58 PM
  • Hi Ed,

    I am building a VSPackage. The extension is a custom TFS check-in policy that needs to bring up a dialog when user hits the Check In button, and in my opinion it is bstter to make the dialog modal to prevent user from hitting any other controls in the middle of checkin operation.

    Could you please elaborate why is creating a dialog as modal is considered unsupported? After all I managed to find a solution to my problem (see my reply above to Vitevic's comment) but again, this seems like a generic task and IMHO should've been implemented inside of the framework.

    Thanks,
    Gevorg

    Thursday, May 22, 2014 11:03 PM
  • Hi Gevorg,

    I never said invoking a modal dialog is unsupported. Attempting to get at an implementation detail like an HWND typically is. As noted earlier, a lot of the windows you now see in Visual Studio are WPF based, and are no longer represented as native HWNDs.

    If you're building a VSPackage, just derive your dialog from the Microsoft.VisualStudio.PlatformUI.DialogWindow and invoke it via it's ShowModal method, and the underlying framework will take care of the modality.

    If by chance you cannot do this (for example if your dialog is native or winform based), use the IVsUIShell.EnableModeless API to set the IDE's modality.

    Sincerely,


    Ed Dore

    • Marked as answer by Anna Cc Wednesday, June 4, 2014 2:12 PM
    Monday, May 26, 2014 12:25 AM