Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss all WPF menus, popups, etc. by DevExpress programmatically to get around WindowsFormsHost related issue?

I want it to behave such as you clicked somewhere on application. (which collapses all menus, drop downs, etc)

Actually, I'm trying to get around the interoperability related focus issue you get when you are hosting Windows Forms controls in a WPF application using WindowsFormsHost: If a WPF menu/popup by DevExpress is open and you click on a Windows Forms control, the menu/popup doesn't get dismissed automatically.

Now I have a lot of Windows Forms controls in the WindowsFormsHost and also a lot of DevExpress controls in the WPF area. To get around this easily, I have added a message filter to hook all clicks in application and then I see if the clicked control was a Windows Forms control. Then I need to do something to make all WPF menus, etc. by DevExpress dismissed if they were open.

GlobalMouseHandler globalClick = new GlobalMouseHandler();
System.Windows.Forms.Application.AddMessageFilter( globalClick );

GlobalMouseHandler:

public class GlobalMouseHandler : System.Windows.Forms.IMessageFilter
{
  private const int WM_LBUTTONDOWN = 0x201;
  private const int WM_RBUTTONDOWN = 0x204;

  public bool PreFilterMessage( ref System.Windows.Forms.Message m )
  {
    if( m.Msg == WM_LBUTTONDOWN || m.Msg == WM_RBUTTONDOWN )
    {
      var c = System.Windows.Forms.Control.FromHandle( m.HWnd );

      if( c != null )
        // TODO: CLOSE ALL WPF MENUS ETC
        // Didn't work: MainWindow.Instance.ARandomControl.Focus();
    }

    return false;
  }
}
like image 441
user1004959 Avatar asked Feb 05 '14 11:02

user1004959


3 Answers

I made a prototype out of your issue and everything works (when I click inside Windows Form Host the outside WPF combox collapse and vice versa).

So we know the native controls works as expected, the problem might be because of the UI framework you are using.

like image 89
Hiệp Lê Avatar answered Nov 20 '22 04:11

Hiệp Lê


https://documentation.devexpress.com/#wpf/DevExpressXpfBarsBarManager_CloseAllPopupstopic

So I had to:

MainWindow.Instance.BarManager.CloseAllPopups();
like image 3
user1004959 Avatar answered Nov 20 '22 06:11

user1004959


Did you try to loop through the controls and raise the lose focus event?

like image 1
Arachnid Avatar answered Nov 20 '22 04:11

Arachnid