[英]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: 在以下情况下,一切正常:
However, when I run my .exe and outlook has the focus when I double click the column, the following happens: 但是,当我运行.exe 并双击列时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 我已经尝试过
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.