c# - 从 wpf 应用程序打开并且 outlook 正在运行时,如何使 outlook 选择名称对话框聚焦

标签 c# wpf outlook vsto

我正在创建一个 wpf 应用程序来发送 outlook 约会。在此应用程序中,我打开 outlook 选择名称对话框以选择约会的接收者。以下是我的代码:

Outlook.SelectNamesDialog selectNameDialog =
           outlookApp.Session.GetSelectNamesDialog();
        selectNameDialog.SetDefaultDisplayMode(
            Outlook.OlDefaultSelectNamesDisplayMode.olDefaultMeeting);

        foreach (var recipient in recipientsList)
        {
            if (string.IsNullOrEmpty(recipient))
                continue;
            Outlook.Recipient confRoom =
                selectNameDialog.Recipients.Add(recipient);
            // Explicitly specify Recipient.Type.
            confRoom.Type = (int)Outlook.OlMeetingRecipientType.olRequired;
        }

        selectNameDialog.Recipients.ResolveAll();

        selectNameDialog.Display();

我的问题是,当我打开选择名称对话框时,如果 outlook 没有运行,它工作正常。但是,如果 outlook 正在运行并且我从我的应用程序中单击打开此对话框,它会在我的应用程序后面和 outlook 窗口的顶部打开。即使 outlook 正在运行,我也需要在我的应用程序之上显示它。非常感谢任何帮助将此对话带到所有人面前。提前致谢。

最佳答案

你可以尝试获取outlook Process,把他的窗口往前调。没有 .NET Hook 可以执行此操作,您需要为此使用 native Win32 DLL。

    [Flags()]
    private enum SetWindowPosFlags : uint
    {
        SynchronousWindowPosition = 0x4000,
        DeferErase = 0x2000,
        DrawFrame = 0x0020,
        FrameChanged = 0x0020,
        HideWindow = 0x0080,
        DoNotActivate = 0x0010,
        DoNotCopyBits = 0x0100,
        IgnoreMove = 0x0002,
        DoNotChangeOwnerZOrder = 0x0200,
        DoNotRedraw = 0x0008,
        DoNotReposition = 0x0200,
        DoNotSendChangingEvent = 0x0400,
        IgnoreResize = 0x0001,
        IgnoreZOrder = 0x0004,
        ShowWindow = 0x0040,
    }

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool BringWindowToTop(IntPtr hWnd);

    static readonly IntPtr HWND_TOP = new IntPtr(0);
    static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);

    [DllImport("user32.dll")]
    static extern IntPtr SetFocus(IntPtr hWnd);

    int bring_window_to_front_mode = 1; // Various option here, try them out
    Boolean TopMost = false;

        System.Diagnostics.Process[] prcs = System.Diagnostics.Process.GetProcessesByName("XBMCLauncher");
        foreach (var proc in prcs)
        {
            Log.LogLine("Main Window Handle {0}", p.MainWindowHandle.ToInt32());
            switch (bring_window_to_front_mode)
            {
                case 1:
                    if (TopMost)
                    {
                        Log.LogLine("SetWindowPos TopMost {0}", p.MainWindowHandle.ToInt32());
                        SetWindowPos(p.MainWindowHandle, HWND_TOPMOST, 50, 50, 500, 500, SetWindowPosFlags.IgnoreMove | SetWindowPosFlags.IgnoreResize);
                    }
                    else
                    {
                        Log.LogLine("SetWindowPos {0}", p.MainWindowHandle.ToInt32());
                        SetWindowPos(p.MainWindowHandle, HWND_TOP, 50, 50, 500, 500, SetWindowPosFlags.IgnoreMove | SetWindowPosFlags.IgnoreResize);
                    }
                    break;
                case 2:
                    Log.LogLine("BringWindowToTop {0}", p.MainWindowHandle.ToInt32());
                    BringWindowToTop(p.MainWindowHandle);
                    break;
                case 3:
                    Log.LogLine("SetForegroundWindow {0}", p.MainWindowHandle.ToInt32());
                    SetForegroundWindow(p.MainWindowHandle);
                    break;
                case 4:
                    Log.LogLine("SetFocus {0}", p.MainWindowHandle.ToInt32());
                    SetFocus(p.MainWindowHandle);
                    break;
            }
        }

当然是尝试/捕捉...

关于c# - 从 wpf 应用程序打开并且 outlook 正在运行时,如何使 outlook 选择名称对话框聚焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13758817/

相关文章:

c# - Entity Framework 7 使用代码优先方法复数表名

c# - 仅更改选定行的 Horizo​​ntalGridLinesBrush

c# - 应用程序崩溃,事件名称为 CLR20r3?

outlook - 是否可以使用 Office Add In 以编程方式访问 Outlook 中的联系人列表

c# - 使用 C# 的 VSTO Outlook ItemSend

html - Outlook 365 的条件标记

c# - 如何形成一棵代表接口(interface)继承树的树?

c# - 项目或单独项目中的命名空间

c# - wpf保存图片的问题

c# - 从 C#,如何将 clob 值传递给 oracle 存储过程