强制 MS Outlook 放在前面(在我的应用程序之上)

Force MS Outlook to front (on top of my app)

我有以下功能可以使用 MS Outlook 2010 发送邮件 有时 Outlook 不会出现在我的应用程序前面(MDI 应用程序 pmNone,poDefaultPosOnly) 当 Outlook 在前面弹出时以及它在后台打开时(图标在状态栏上闪烁),我一直无法找到任何连续性 那么我的问题是:是否有任何方法可以强制 Outlook 在所有其他应用程序前面弹出?

function SendMailOutlook(const aFrom, aSubject, aBody, aTo, aCc, aBcc: string; aMailFiles: TStringList; aReceipt: boolean; aPreview: boolean = True): boolean;
var
  Outlook: OleVariant;
  MailItem: OleVariant;
  i: integer;
const
  olMailItem = [=10=]000000;
begin
  try
    try
      Outlook := GetActiveOleObject('Outlook.Application');
    except
      Outlook := CreateOleObject('Outlook.Application');
    end;
    MailItem := Outlook.CreateItem(olMailItem);
    if olAccountValid(aFrom) then
      MailItem.SendUsingAccount := Outlook.Session.Accounts.Item(aFrom);
    MailItem.To := aTo;
    MailItem.Cc := aCc;
    MailItem.Bcc := aBcc;
    MailItem.Subject := aSubject;
    MailItem.Body := aBody;
    for i := 0 to aMailFiles.Count - 1 do
      MailItem.Attachments.Add(aMailFiles.Strings[i]);
    MailItem.ReadReceiptRequested := aReceipt;
    MailItem.OriginatorDeliveryReportRequested := aReceipt;
    if aPreview = True then
      MailItem.Display(True)
    else
      MailItem.Send;
    Result := MailItem.Sent;
  except
    on E:Exception do
      begin
        Logfile.Error('U_Mailing.Outlook.SendMailOutlook: ' + E.Message);
        Result := False;
      end;
  end;
end;

我不确定如何显示我开始工作的代码,所以我将编辑我的问题并将其添加到那里。如果不是这样,请原谅。

function SendMailOutlook(const aFrom, aSubject, aBody, aTo, aCc, aBcc: string; aMailFiles: TStringList; aReceipt: boolean; aPreview: boolean = True): boolean;
var
  Outlook: OleVariant;
  MailItem: OleVariant;
  i: integer;
  MailInspector: variant;
const
  olMailItem = [=11=]000000;
begin
  try
    try
      Outlook := GetActiveOleObject('Outlook.Application');
    except
      Outlook := CreateOleObject('Outlook.Application');
    end;
    MailItem := Outlook.CreateItem(olMailItem);
    if olAccountValid(aFrom) then
      MailItem.SendUsingAccount := Outlook.Session.Accounts.Item(aFrom);
    MailItem.To := aTo;
    MailItem.Cc := aCc;
    MailItem.Bcc := aBcc;
    MailItem.Subject := aSubject;
    MailItem.Body := aBody;
    for i := 0 to aMailFiles.Count - 1 do
      MailItem.Attachments.Add(aMailFiles.Strings[i]);
    MailItem.ReadReceiptRequested := aReceipt;
    MailItem.OriginatorDeliveryReportRequested := aReceipt;

    if aPreview = True then
      begin
        MailInspector := MailItem.GetInspector;
        MailInspector.Display(True);
      end
    else
      MailItem.Send;
    Result := MailItem.Sent;
  except
    on E:Exception do
      begin
        Logfile.Error('U_Mailing.Outlook.SendMailOutlook: ' + E.Message);
        Result := False;
      end;
  end;
end;

我相信您可以使用 SetForegroundWindow 函数,它将创建指定 window 的线程置于前台并激活 window。键盘输入指向 window,并为用户更改各种视觉提示。系统为创建前台的线程 window 分配的优先级略高于其他线程。

您可以获得 Outlook window 句柄将资源管理器或检查器对象投射到 IOleWindow 界面。

您将需要使用 IOleWindow / AttachThreadInput / SetForegroundWindow 来执行此操作。

参见how to make the Outlook Compose window top most?

另见 c# bring outlook window to front