简体   繁体   English

C#将Outlook窗口置于最前面

[英]c# bring outlook window to front

I have not yet been able to see a workable solution for this problem. 我还没有看到解决此问题的可行解决方案。

I have an external application that launches an outlook compose window, and I want to make sure it always pop up in front. 我有一个启动Outlook撰写窗口的外部应用程序,我想确保它总是在前面弹出。 It does not all the time. 并非一直如此。 Eg if I tab to Outlook and then back to the application and launch the task, it will just blink in the bottom. 例如,如果我使用Tab键查看Outlook,然后返回到应用程序并启动任务,它将在底部闪烁。

I've tried several suggestions with getinspector.Active() etc. but nothing works. 我已经尝试过与getinspector.Active()等的一些建议,但是没有任何效果。

Some sample code: 一些示例代码:

String address = "someone@example.com";

Outlook.Application oApp = new Outlook.Application();
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMailItem.To = address

oMailItem.Body = "example";  

oMailItem.Display(true); //true = modal which I need for this task, have tried without also.

similar thread but with Delphi code that I do not know how to translate into c# 类似的线程,但与德尔福代码,我不知道如何转换为C#

Here's the working code you need. 这是您需要的工作代码。 The missing ingredient to bring the window to the foreground is in fact "inspector.Activate()", and you must call this after "mailItem.Display". 实际上,将窗口带到前台所缺少的成分是“ inspector.Activate()”,您必须 “ mailItem.Display” 之后调用它。

var outlookApp = new Microsoft.Office.Interop.Outlook.Application();
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.Subject = "subject";
var inspector = mailItem.GetInspector; // Force the "HTMLBody" property to be populated with any email signature, so that we can append it to our content.
mailItem.HTMLBody = "My message" + mailItem.HTMLBody;
mailItem.Attachments.Add("attachment.dat", OlAttachmentType.olByValue);
mailItem.Display(false); // Display the email
inspector.Activate(); // Bring the editor to the foreground.

This cannot, in general, be done. 通常,这无法完成。 Considered what would happen if two applications both wanted to be topmost at the same time? 考虑如果两个应用程序都希望同时位于最高位置会发生什么情况?

See http://blogs.msdn.com/b/oldnewthing/archive/2005/06/07/426294.aspx for a more extensive discussion. 有关更广泛的讨论,请参见http://blogs.msdn.com/b/oldnewthing/archive/2005/06/07/426294.aspx

I dont have outlook installed but you could bring the whole outlook window to front if it was minimized: 我没有安装Outlook,但是如果将其最小化,则可以将整个Outlook窗口置于最前面:

        [DllImport("user32.dll")]
        private static extern int SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        private const int SW_SHOWNORMAL = 1;
        private const int SW_RESTORE = 9;

        Process proc = Process.GetProcessesByName("spotify").FirstOrDefault();
        if (proc != null)
        {
             ShowWindow(proc.MainWindowHandle, SW_SHOWNORMAL);   // Make the window visible if it was hidden
             ShowWindow(proc.MainWindowHandle, SW_RESTORE);      // Next, restore it if it was minimized
             SetForegroundWindow(proc.MainWindowHandle);         // Finally, activate the window 
        }

SetForegroundWindow: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx SetForegroundWindow: http : //msdn.microsoft.com/zh-cn/library/windows/desktop/ms633539( v=vs.85) .aspx

ShowWindow: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx 显示窗: http : //msdn.microsoft.com/zh-cn/library/windows/desktop/ms633548%28v=vs.85%29.aspx

Do you have a particular problem translating the sample function from how to make the Outlook Compose window top most? 您是否在转换示例函数方面有一个特殊的问题,该示例函数是如何使Outlook Compose窗口最顶部的? ?

If you cannot make IOleWindows / AttachThreadInput / SetForegroundWindow work, you can use Redemption and its SafeInspector / SafeInspector . 如果无法使IOleWindows / AttachThreadInput / SetForegroundWindow工作,则可以使用Redemption及其SafeInspector / SafeInspector Activate methods. Activate方法。 The following VB script will bring the main Outlook window to the foreground: 以下VB脚本会将Outlook主窗口置于前台:

set App = CreateObject("Outlook.Application")
set sExplorer = CreateObject("Redemption.SafeExplorer")
sExplorer.Item = App.ActiveExplorer
sExplorer.Activate

Just to expound @Nonus answer. 只是为了阐明@Nonus答案。 This works for me. 这对我有用。 Maximize the outlook window first before calling the Email display function. 在调用电子邮件显示功能之前,请先最大化前景窗口。

    [DllImport("user32.dll")]
    private static extern int SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);   
    private const int SW_SHOWMAXIMIZE = 3; 

    public void Display()
    {
        message = RedemptionLoader.new_SafeMailItem();
        message.Item = mailApp.CreateItem(Outlook.OlItemType.olMailItem);
        Process proc = Process.GetProcessesByName("outlook").FirstOrDefault();
        if (proc != null)
        {
            ShowWindow(proc.MainWindowHandle, SW_SHOWMAXIMIZE);   
            SetForegroundWindow(proc.MainWindowHandle);         
        }

        ((Outlook.MailItem)message.Item).Display(false);    // Show email to user, false = Non-Modal

    }

This solution sets the Outlook process MainWindow in the foreground, right after calling oMailItem.Display(true). 此解决方案在调用oMailItem.Display(true)之后立即将Outlook进程MainWindow设置在前台。

 [DllImport("User32.dll", SetLastError = true)]
 static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

  oMailItem.Display(true);
  var outlookProcess = Process.GetProcessesByName("OUTLOOK");                  
  IntPtr handle = outlookProcess[0].MainWindowHandle;
  SwitchToThisWindow(handle, true);      

Not gracefull, but it works. 没有宽限期,但可以。

try printing something to the debug window before displaying the message: 在显示消息之前,尝试在调试窗口中打印一些内容:

Set itm = ol.CreateItem(olMailItem)

Set ins = itm.GetInspector

Debug.Print "this makes the window display..."

ins.Activate
ins.WindowState = olNormalWindow
itm.Display

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM