how to make the Outlook Compose window top most?

2019-01-29 11:49发布

问题:

I am creating an Outlook Message. Sometimes the Outlook Compose window appears behind other windows.

How can I make it the top most?

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.Subject = "Help";

oMailItem.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
oMailItem.Attachments.Add("H:\\file.txt");

oMailItem.Body = "Call me";  
// body, bcc etc...
oMailItem.Display(true);

I am using WinForm and .Net 2.0 (target)

回答1:

Firstly, call MailItem.GetInspector to get the Inspector object (you can then call Inspector.Display), secondly, cast Inspector to IOleWindow interface and call IOleWindows::GetWindow to retrieve the inspector's HWND. Once you have that, you can call SetForegroundWindow. One thing to keep in mind is that Windows will nto bring the window to the foreground if the parent process is not in the foreground. You would need to use AttachThreadInput function for that - see below (Delphi):

function ForceForegroundWindow(hWnd: THandle): BOOL;
var
  hCurWnd: THandle;
begin
  hCurWnd := GetForegroundWindow;
  AttachThreadInput(
    GetWindowThreadProcessId(hCurWnd, nil),
    GetCurrentThreadId, True);
  Result := SetForegroundWindow(hWnd);
  AttachThreadInput(
    GetWindowThreadProcessId(hCurWnd, nil),
    GetCurrentThreadId, False);
end;