Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make extension form always open in same display as VS?

Status
Not open for further replies.

PPettit

IS-IT--Management
Sep 13, 2003
511
US
I wrote myself an extension (Visual Studio Package) for Visual Studio 2012. When triggered, it always opens up a form in the left-hand monitor of my dual-monitor setup. My problem is that I want the form to open up in the same monitor as VS no matter which monitor VS is currently showing in. To add to the problem, I may have more than one instance of VS running when the extension is triggered. I need to make sure that the extension's form stays with the instance of VS that launched it.

Is there any way to get something like the X and Y coordinates of the launching instance of VS when the extension is triggered? I figure that if I can get these coordinates, it shouldn't be too hard to position my form in relation to them.
 
In case anyone is interested, I think I found something that I can work with. It was right in front of me the whole time.

Basically, the environment provides the ActiveWindow property which allows you to get the coordinates.

Code:
...
Public Shared mySDTE As EnvDTE.DTE
...
Private Sub MenuItemCallback(ByVal sender As Object, ByVal e As EventArgs)
    mySDTE = TryCast(Me.GetService(GetType(SDTE)), EnvDTE.DTE)
    Debug.Print("Left:  " & mySDTE.ActiveWindow.Left & "  Top:  " & mySDTE.ActiveWindow.Top)
    frmFileSelector = New MyFileSelector
    If Not isOpened(frmFileSelector) Then
        frmFileSelector.Show()
    End If
End Sub
...

With VS fully maximized in my left monitor it returns "Left: 31 Top: 106". Not sure why it doesn't return zeros for both values. Fully maximized in my right monitor, I get "Left: 1951 Top: 106". This gives me a good idea as to where VS is so that I can open my form on top of it no matter it is.
 
In case anyone is interested, here's more detail about how I did the positioning of the form in the monitor where Visual Studio is displayed. If anyone has some improvements, let me know. I'm still relatively new to VB.Net and this was my first attempt at an extension for VS 2012.

In the main class, I have this code that gets called whenever the extension is triggered:
Code:
    ....
    Public Shared mySDTE As EnvDTE.DTE
    Public Shared frmSecondary As SecondaryForm
    Public Shared intVSPositionLeft As Integer

    ....

    Private Sub MenuItemCallback(ByVal sender As Object, ByVal e As EventArgs)
        'Short version:  create object representing VS
        mySDTE = TryCast(Me.GetService(GetType(SDTE)), EnvDTE.DTE)
        'Get the position of the left edge of VS
        intVSPositionLeft = mySDTE.ActiveWindow.Left

        'create an object representing a new instance of the secondary form
        frmSecondary = New SecondaryForm
        'show the form as a modal dialog box so that you can't do anything else until you're done with the form
        frmSecondary.ShowDialog()
    End Sub
    ....

In the secondary form's Load event:
Code:
    ...
        Dim intScreenX As Integer
        Dim intScreenWidth As Integer
        Dim intScreenHeight As Integer
        Dim listDisplay As New List(Of Screen)

        'Cycle through each monitor/display area to get the measurements of the one that encompasses the left edge of VS
        For Each myScreen As Screen In Screen.AllScreens
            If MainClass.intVSPositionLeft > myScreen.Bounds.X And MainClass.intVSPositionLeft < myScreen.Bounds.X + myScreen.Bounds.Width Then
                'left/starting/horizontal edge of the display area
                intScreenX = myScreen.Bounds.X
                intScreenHeight = myScreen.Bounds.Height
                intScreenWidth = myScreen.Bounds.Width
            End If
        Next

        'Position the secondary form in the center of the display area where VS appears.
        Me.Left = intScreenX + ((intScreenWidth / 2) - Me.Width / 2)
        Me.Top = (intScreenHeight - Me.Height) / 2
    ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top