[英]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.