You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSTO Outlook插件开发:获取活动窗口(Inspector/Explorer)屏幕位置

获取Outlook活动窗口(Explorer/Inspector)的屏幕位置(Windows API实现)

由于Outlook对象模型没有提供直接获取窗口屏幕位置的API,我们可以通过调用Windows原生API来实现。核心思路是先获取Outlook活动窗口的句柄(HWND),再通过GetWindowRect函数获取窗口的屏幕坐标。

实现步骤及代码示例

1. 声明Windows API函数和结构体

在VSTO插件的类中引入必要的Windows API声明和结构体:

using System;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;

public static class OutlookWindowPositionHelper
{
    // 获取指定窗口的屏幕矩形区域
    [DllImport("user32.dll")]
    private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    // 获取前台窗口句柄(作为备用方案)
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    // 存储窗口坐标的结构体
    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }
}

2. 核心方法:获取活动窗口位置

添加获取Outlook活动窗口位置的方法,自动区分Explorer(主窗口)和Inspector(邮件/表单窗口):

public static (int Left, int Top, int Width, int Height) GetActiveWindowPosition()
{
    Outlook.Application outlookApp = Globals.ThisAddIn.Application;
    var activeWindow = outlookApp.ActiveWindow();
    IntPtr windowHandle = IntPtr.Zero;

    // 根据窗口类型提取句柄
    if (activeWindow is Outlook.Explorer explorer)
    {
        windowHandle = new IntPtr(explorer.HWND);
    }
    else if (activeWindow is Outlook.Inspector inspector)
    {
        windowHandle = new IntPtr(inspector.HWND);
    }
    else
    {
        // 当ActiveWindow无法获取时, fallback到前台窗口
        windowHandle = GetForegroundWindow();
    }

    if (windowHandle == IntPtr.Zero)
    {
        throw new InvalidOperationException("无法定位Outlook活动窗口");
    }

    if (!GetWindowRect(windowHandle, out RECT rect))
    {
        throw new InvalidOperationException("获取窗口坐标失败");
    }

    // 计算宽高并返回
    int width = rect.Right - rect.Left;
    int height = rect.Bottom - rect.Top;
    return (rect.Left, rect.Top, width, height);
}

3. 调用示例

在插件的事件(如按钮点击、窗口激活)中调用该方法:

// 示例:在插件按钮点击事件中获取并展示窗口位置
private void btnGetWindowPos_Click(object sender, EventArgs e)
{
    try
    {
        var pos = OutlookWindowPositionHelper.GetActiveWindowPosition();
        MessageBox.Show($"Outlook窗口位置:\n左: {pos.Left}\n上: {pos.Top}\n宽: {pos.Width}\n高: {pos.Height}");
    }
    catch (Exception ex)
    {
        MessageBox.Show($"获取失败:{ex.Message}");
    }
}

注意事项

  • 确保项目引用了System.Runtime.InteropServices命名空间
  • 若需判断窗口是否最小化/最大化,可额外引入GetWindowPlacement API扩展功能
  • 在Outlook的安全沙箱环境中运行时,需确保插件已被信任,避免权限限制

内容的提问来源于stack exchange,提问作者Willy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 18:36:24