14

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;
  }
}
user1004959
  • 627
  • 6
  • 16
  • I tried to set focus to other controls by calling their Focus method but it didn't work. – user1004959 Feb 05 '14 at 11:57
  • 1
    Show what have you tried. Explain your problem more clear.. – Soner Gönül Feb 05 '14 at 11:59
  • Can you show some code that shows the problem? Such as a simplified wpf window showing the issue? – Patrick Hofman Feb 07 '14 at 13:01
  • 6
    I have just realized the problem only lies with the third-party WPF controls that I'm using all over the place. :\ Luckily they have a static method to close all popups. What a waste of +200 rep. :) – user1004959 Feb 07 '14 at 14:51
  • You can't answer your own question and be eligible to the bounty? – Vitor Canova Feb 07 '14 at 15:16
  • Don't think so. But worth a shot? Answer your own question, you may get half back if automatic bounty award allows it. Manual bounty award definitely won't work. – Maverik Feb 07 '14 at 15:39
  • I brought this to moderator's attention. Maybe they'll help you. – drankin2112 Feb 07 '14 at 19:01
  • I'd post an answer and see if you can award the bounty to it. – Rob Perkins Feb 11 '14 at 21:54
  • @user1004959: http://meta.stackexchange.com/questions/220725/reward-bounty-to-the-one-asking-the-question-when-he-found-the-answer-himself – Patrick Hofman Feb 12 '14 at 10:38
  • 3
    You should still write up the solution as an answer, at least if you can do it in such a way that someone else having the same problem with those controls might find it. You can't get the bounty back, but people can still vote up your answer if they find it useful. – Ilmari Karonen Feb 12 '14 at 12:16

3 Answers3

3

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.

Hiệp Lê
  • 636
  • 4
  • 8
3

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

So I had to:

MainWindow.Instance.BarManager.CloseAllPopups();
user1004959
  • 627
  • 6
  • 16
1

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

Arachnid
  • 280
  • 1
  • 3
  • 12