Strange behavior when displaying WPF window in Outlook
I am using the following code to show my WPF window from a new message box in Outlook:
private void DisplayWindow(Window window) {
var wih = new System.Windows.Interop.WindowInteropHelper(window);
wih.Owner = GetForegroundWindow();
window.ShowInTaskbar = false;
window.ShowDialog();
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
My problem is when the tooltips pop up and the ComboBox, the WPF window disappears behind the new message box, leaving only the "popup" content in front. Can anyone explain why this might be happening, and what is the correct way to position the window?
EDIT:
This only happens after the recipient has been added to the Send field and it only seems to be a problem when the foreground window is a new mailing window.
Repeat:
Add an Outlook add-in project and a WPF project (targeting .NET 4.0) to the new solution.
Place a ComboBox with multiple items in it at MainWindow.xaml.
Remove StartupUri
from App.xaml and add the following to App.cs.
public void ShowWindow() {
MainWindow window = new MainWindow();
var wih = new System.Windows.Interop.WindowInteropHelper(window);
wih.Owner = GetForegroundWindow();
window.ShowInTaskbar = false;
window.ShowDialog();
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
Add links to WindowsBase
, System.Xaml
and PresentationFramework
to your Outlook project.
Add a ribbon (XML) to your Outlook project with the following in the XML file.
<customUI ...>
<ribbon>
<tabs>
<tab idMso="TabNewMailMessage">
<group id="MyGroup"
insertAfterMso="GroupMailNew">
<button id="myButton"
size="large"
onAction="myButton_Action"
imageMso="HappyFace"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Add the following feed code.
MyWpfApplication.App app;
public void Ribbon_Load(Office.IRibbonUI ribbonUI) {
this.ribbon = ribbonUI;
var appThread = new Thread(new ThreadStart(() => {
this.app = new MyWpfApplication.App();
app.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown;
app.Run();
}));
appThread.SetApartmentState(ApartmentState.STA);
appThread.Start();
}
public void myButton_Action(Office.IRibbonControl control) {
// Dispatcher used as cross thread operation.
app.Dispatcher.BeginInvoke((Action)(() => {
app.ShowWindow();
}));
}
Add the following to ThisAddIn
protected override Microsoft.Office.Core
.IRibbonExtensibility CreateRibbonExtensibilityObject() {
return new Ribbon();
}
Start the Outlook add-in, compose a new message, add a recipient, and click the emoticon button. You will see an error when you click on ComboBox
.
source to share
It could be the same problem described here:
The .NET Framework 4.7.1 source code contains comments in System.Windows.FrameworkCompatibilityPreferences , saying that there is a bug in Windows that can cause the z-order for windows to be incorrect in some conditions.
The suggested solution is to add the following code to the app.config file of the WPF application:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="UseSetWindowPosForTopmostWindows" value="True" />
</appSettings>
</configuration>
source to share