c#将outlook窗口置于前面

我还没有看到这个问题的可行解决方案。

我有一个外部应用程序启动一个outlook compose窗口,我想确保它总是在前面弹出。 它并不是所有的时间。 例如,如果我选择Outlook,然后返回应用程序并启动任务,它将只在底部闪烁。

我已经尝试了几个建议与getinspector.Active()等但没有任何作用。

一些示例代码:

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. 

类似的线程,但用Delphi代码,我不知道如何转换成c#

通常,这不能完成。 考虑如果两个应用程序都希望同时成为最顶层的话会发生什么?

有关更广泛的讨论,请参见http://blogs.msdn.com/b/oldnewthing/archive/2005/06/07/426294.aspx 。

我没有安装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

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

您是否在将示例函数与Outlook Compose窗口的最顶层进行翻译时遇到了特殊问题? ?

如果无法使IOleWindows / AttachThreadInput / SetForegroundWindow工作,则可以使用Redemption及其SafeInspector / SafeInspector .Activate方法。 以下VB脚本将主Outlook窗口置于前台:

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

这是您需要的工作代码。 将窗口置于前台的缺失成分实际上是“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. 

只是为了解释@Nonus的答案。 这对我有用。 在调用电子邮件显示function之前,首先最大化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_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 }