简体   繁体   English

从C#打开的Outlook New Mail窗口似乎具有焦点,但我的应用程序仍然具有它

[英]Outlook New Mail Window opened from C# seems to have focus but my app still has it

I'm having a problem that I've been trying to solve for days now, but without luck! 我有一个问题已经尝试了好几天了,但是没有运气!

On my Windows Forms Application I have a grid. 在Windows窗体应用程序上,我有一个网格。 One column contains an email address. 一栏包含一个电子邮件地址。 When the user double clicks this column, I want to open a new E-Mail Window via Outlook automation. 当用户双击此列时,我想通过Outlook自动化打开一个新的电子邮件窗口。 This window should have the focus and allow the user to type immediately. 该窗口应具有焦点,并允许用户立即键入。

Everything works fine, when: 在以下情况下,一切正常:

  • I'm running my app from Visual Studio. 我正在从Visual Studio运行我的应用程序。
  • Or my app has the focus. 还是我的应用有重点。

However, when I run my .exe and outlook has the focus when I double click the column, the following happens: 但是,当我运行.exe 双击列时Outlook成为焦点时,会发生以下情况:

  • The new Mail window opens as expected 新的邮件窗口将按预期方式打开
  • The cursor blinks in the new mail window (as expected) 光标在新邮件窗口中闪烁(如预期)
  • when the user starts typing, the cursor still blinks in outlook but the typed text appears in the grid of my application, not in outlook. 当用户开始键入时,光标仍然在Outlook中闪烁, 但是键入的文本显示在应用程序的网格中,而不是Outlook中。

I was able to reproduce the problem with a simple form that has a textbox on it. 我能够用一个带有文本框的简单表单来重现该问题。

I use the following code: 我使用以下代码:

private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
 OpenOutlookMail(textBox1.Text);
}

private void OpenOutlookMail(string to)
{
  MailItem item = OutlookApp.CreateItem(OlItemType.olMailItem) as MailItem;
  item.To = to;
  item.Subject = string.Empty;
  item.Body = string.Empty;

  item.Display();
}

protected Application OutlookApp
{
    get
    {
        if (mOutlookApp == null)
        {
            mOutlookApp = new Application();

        }
        return mOutlookApp;
     }
  }

What i already tried was to 我已经尝试过

  • Activate my current form via this.Activate() before the call to OpenOutlookMail 在调用OpenOutlookMail之前通过this.Activate()激活我当前的表单
  • Activate the MailItem Inspector Object 激活MailItem检查器对象
  • Activate the ActiveWindow and ActiveExplorer of Outlook via Automation 通过自动化激活Outlook的ActiveWindow和ActiveExplorer
  • Using AutoIt as explained here Similar Problem with MS Word on the MSDN Forum 如此处所述,使用AutoIt 在MSDN论坛上使用MS Word遇到类似问题

Any help would be appreciated! 任何帮助,将不胜感激!

You can try to use Dispatcher.BeginInvoke(...) with some low priority in your textBox1_MouseDoubleClick(...) method to call OpenOutlookMail(). 您可以尝试在textBox1_MouseDoubleClick(...)方法中以低优先级使用Dispatcher.BeginInvoke(...)来调用OpenOutlookMail()。 It often helps to workaround focus management issues like this one. 它通常有助于解决此类焦点管理问题。

I wrote about focusing a background window some time ago: 我前段时间写过关于聚焦背景窗口的文章:

http://blog.sebastianbrand.com/2010/02/activate-form-in-background.html http://blog.sebastianbrand.com/2010/02/activate-form-in-background.html

private void label1_Click(object sender, EventArgs e)
{
  // mainform.BringToFront(); // doesn't work
  BeginInvoke(new VoidHandler(OtherFormToFront));
}

delegate void VoidHandler();

private void OtherFormToFront()
{
  mainform.BringToFront(); // works
}

If you do have an handle of the bad window, give that a try. 如果您确实有坏窗口的句柄,请尝试一下。

I haven't been able to reproduce the problem with your code. 我无法用您的代码重现该问题。 I've used Microsoft.Office.Interop.Outlook version 14.0.0.0 and in every tests i've done the mail window get the focus. 我使用了Microsoft.Office.Interop.Outlook版本14.0.0.0,在每个测试中,我都完成了邮件窗口的工作。

As you state, 如您所说,

Everything works fine, when: •I'm running my app from Visual Studio. 在以下情况下,一切正常:•我正在从Visual Studio运行我的应用程序。 Or my app has the focus. 或者我的应用具有重点。

Maybe trying to focus your form and/or making your application sleep before opening the mail window would work 也许在打开邮件窗口之前尝试着眼于表单和/或使应用程序进入睡眠状态

private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    this.Focus();
    System.Threading.Thread.Sleep(500);
    OpenOutlookMail(textBox1.Text);
}

Interops often have weird behaviors. 互操作者经常有奇怪的行为。 :s :■

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM