Locating/Centering a form in an Add-In

473
7
Jump to solution
02-19-2013 11:44 AM
New Contributor
Hello All,
     I'm sure this one's been covered, but for the life of me, i can't find a solution in either the forum or the docs :confused: ...
I'm building an Add-In in Visual BASIC for a Desktop environment (Arcmap 10.1).  I have a simple tool that the user selects a feature, and then I want a simple form to come up to show some attributes and other things...  This is not a dockable window or anything like that, just a simple form...  I am trying to bring up a custom form after the user "Uses/clicks" my Add-In tool... I can presenty get the form to come up, that's NO problem...  It's the location of that form that's causing me grief :mad:. I am trying to center the form on the ArcMap application window...  NO GO! I can't figure out a way of getting the form to come up over/centered on the ArcMap application.  This is particularly important when the user has multiple monitors and has ArcMap on the second or beyond monitor...   Is there a way of getting the desktop (Windows Desktop) location of the running ArcMap application and using that to locate said form???  I'm pretty sure this should be an easy one to solve,, just pulling my hair out trying to figure out! 😞  I'm figuring I should start with My.ArcMap.( something goes here :))

Thanks in Advance,
Slightly Frustrated,

Kevin Orcutt
Reply
0 Kudos
1 Solution

Accepted Solutions
Regular Contributor
Reply
0 Kudos
7 Replies
Regular Contributor
it involves getting the application height, and subtracting your form height, then divide that result by 2.  this is your form's top.

same for width.  get the application width, subtract your form width, then divide that result by 2.  this is your form's left.
Reply
0 Kudos
New Contributor
Hello Leo,
getting there, but not quite, sorry.  I figured that I would have to do something along those lines, but getting the application height and width among other things are escaping my grasp...  The KEY thing that I'm trying to get is the application's location on the desktop.  That way I can place the form over it when it comes up.  As mentioned, this is particular to when ArcMap is on the second monitor or beyond.  I want the form to appear over ArcMap, not back on the first monitor.  So the location is Key...

Any other ideas???

Kevin O.
Reply
0 Kudos
Regular Contributor
I'm not a VB person, but the concept will position your form over ArcMap, wherever ArcMap is.

What happens when you type My.ArcMap.  ?  What do you get?  Height?  Read the API.
Reply
0 Kudos
Regular Contributor
Reply
0 Kudos
New Contributor
Hey Leo,
    Thanks, that did it!!! From there i was able to get what I needed to center the form!!!

Thanks again,

Kevin Orcutt
Reply
0 Kudos
Regular Contributor III
If you set the form's owner when you show it then all you should need to do is set the form's StartupPosition property to CenterParent.

// set the form's owner
yourForm.Show(System.Windows.Forms.Control.FromHandle((IntPtr)m_application.hWnd));
Reply
0 Kudos
MVP Honored Contributor
Neil,

Theoretically that should happen, but my applications have the StartPosition property set to CenterParent for the forms. I set the form's owner using your code and yet the forms appear randomly on the page. I have to use the following sub to center my forms before I show them.


    Public Sub CenterForm(ByVal form As System.Windows.Forms.Form, ByVal m_application As ESRI.ArcGIS.Framework.IApplication)

        Dim pWinPos As ESRI.ArcGIS.Framework.IWindowPosition
        Dim CenterX As Short
        Dim CenterY As Short

        pWinPos = m_application
        CenterX = (pWinPos.Width / 2) + pWinPos.Left
        CenterY = (pWinPos.Height / 2) + pWinPos.Top

        form.Location = New System.Drawing.Point(CenterX - form.Width / 2, CenterY - form.Height / 2)
        form.StartPosition = Windows.Forms.FormStartPosition.Manual

    End Sub
Reply
0 Kudos