我还没有看到这个问题的可行解决方案。
我有一个启动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.
similar thread but with Delphi code that I do not know how to translate into c#
答案 0 :(得分:0)
一般来说,这不可能。考虑如果两个应用程序同时想要成为最顶层的那么会发生什么?
有关更广泛的讨论,请参阅http://blogs.msdn.com/b/oldnewthing/archive/2005/06/07/426294.aspx。
答案 1 :(得分:0)
我没有安装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
答案 2 :(得分:0)
从how to make the Outlook Compose window top most?转换样本函数是否存在特定问题?
如果您无法使IOleWindows
/ AttachThreadInput
/ SetForegroundWindow
工作,则可以使用Redemption及其SafeInspector / SafeInspector。{{ 1}}方法。以下VB脚本将主Outlook窗口置于前台:
Activate
答案 3 :(得分:0)
这是您需要的工作代码。将窗口置于前台的缺失成分实际上是" 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.
答案 4 :(得分:0)
只是为了解释@Nonus的答案。这适合我。在调用电子邮件显示功能之前,首先最大化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
}
答案 5 :(得分:0)
此解决方案在调用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);
不是宽容的,但可以。
答案 6 :(得分:0)
我尝试使用Boinst的答案,但未能将新电子邮件带到Outlook中的前台。这似乎对我来说更加一致:
MailItem mailItem = applicationOutlook.CreateItem(OlItemType.olMailItem);
mailItem.To = strTo;
mailItem.CC = strCC;
mailItem.Subject = strSubject;
mailItem.HTMLBody = strHtmlBody;
Inspector inspector = mailItem.GetInspector;
inspector.Activate();
inspector.WindowState = OlWindowState.olMinimized;
mailItem.Display(false);
inspector.WindowState = OlWindowState.olNormalWindow;
答案 7 :(得分:-1)
在显示消息之前尝试在调试窗口中打印一些内容:
Set itm = ol.CreateItem(olMailItem)
Set ins = itm.GetInspector
Debug.Print "this makes the window display..."
ins.Activate
ins.WindowState = olNormalWindow
itm.Display