[英]c# bring outlook window to front
我还没有看到解决此问题的可行解决方案。
我有一个启动Outlook撰写窗口的外部应用程序,我想确保它总是在前面弹出。 并非一直如此。 例如,如果我使用Tab键查看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.
这是您需要的工作代码。 实际上,将窗口带到前台所缺少的成分是“ 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.
通常,这无法完成。 考虑如果两个应用程序都希望同时位于最高位置会发生什么情况?
有关更广泛的讨论,请参见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/zh-cn/library/windows/desktop/ms633539( v=vs.85) .aspx
显示窗: http : //msdn.microsoft.com/zh-cn/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
只是为了阐明@Nonus答案。 这对我有用。 在调用电子邮件显示功能之前,请先最大化前景窗口。
[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
}
此解决方案在调用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);
没有宽限期,但可以。
在显示消息之前,尝试在调试窗口中打印一些内容:
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.