I'm working on a VSTO Outlook Add-in and now I'm facing a particular use case where I need to get the screen location of the current active Outlook window (explorer or inspector). Sure it's not possible to use any Outlook API mechanism, but how can I do it using Windows API functions? Any code snippet will be greatly appreciated.
Asked
Active
Viewed 66 times
2 Answers
1
You can use the GetWindowRect function which retrieves the dimensions of the bounding rectangle of the specified window. The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen. For example:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
Rectangle myRect = new Rectangle();
private void button1_Click(object sender, System.EventArgs e)
{
RECT rct;
if(!GetWindowRect(new HandleRef(this, this.Handle), out rct ))
{
MessageBox.Show("ERROR");
return;
}
MessageBox.Show( rct.ToString() );
myRect.X = rct.Left;
myRect.Y = rct.Top;
myRect.Width = rct.Right - rct.Left + 1;
myRect.Height = rct.Bottom - rct.Top + 1;
}
Also you may find the GetWindowPlacement function helpful, the function retrieves the show state and the restored, minimized, and maximized positions of the specified window.

Eugene Astafiev
- 47,483
- 3
- 24
- 45
1
Both Explorer and Inspector Outlook Object Model objects expose the Top
/Left
/Width
/Height
properties as well as WindowState
. There is no need to use Windows API to retrieve the window position.

Dmitry Streblechenko
- 62,942
- 4
- 53
- 78