Control's OnExit eats up mouseup event for new control when showing another window

前端 未结 3 1157
小鲜肉
小鲜肉 2020-12-12 04:08

I found this question on Experts-Exchange.

Control\'s OnExit eats up mouseup event for new control when showing another window

The probl

相关标签:
3条回答
  • 2020-12-12 04:47

    Once again PostMessage to the rescue! Defer your dialog just a little bit longer so that Windows can finish its focus change. Post yourself a message instead of showing the dialog directly:

    const
      WM_SHOWMYDIALOG = WM_APP + 321;
    
    TForm1 = class(TForm)
      Edit1: TEdit;
      Edit2: TEdit;
      procedure Edit1Exit(Sender: TObject);
    private
      procedure WMSHOWMYDIALOG(var Message: TMessage); message WM_SHOWMYDIALOG;
    end;
    
    procedure TForm1.Edit1Exit(Sender: TObject);
    begin
      PostMessage(Self.Handle, WM_SHOWMYDIALOG, 0, 0);
    end;
    
    procedure TForm1.WMSHOWMYDIALOG(var Message: TMessage);
    begin
      ShowMessage('Nice one');
    end;
    

    And everything is fine :)

    0 讨论(0)
  • 2020-12-12 04:58

    I'm not so sure that the reason of the behavior is an eaten mouse message. Anyway, either that's the case or not, when you activate a window in an OnExit event of a control, what you're doing is, changing the focus while the focus is changing. That's because, the window is activated before WM_SETFOCUS for the newly focused control returns. This is discouraged, the below quote is from 'Best practices' section of 'Win32 Activation and Focus', a blog entry on MSDN:

    Avoid manually changing focus when getting and/or losing focus. This usually proves to be error prone.


    The fact that the controls are disabled during focus transfer due to the modal nature of the activated window certainly would not help. If you really must do this, an approach like Heinrich's answer would at least delay the launch of the window till the focus transfer completes.

    0 讨论(0)
  • 2020-12-12 05:04

    In the onexit event I show a warningbox and then want to proceed as normal - moving the focus to where the user in fact clicked. Is that possible?

    Yes, (Screen.)ActiveControl will always point to Edit3: before and after the call to ShowMessage:

    procedure TForm1.Edit1Exit(Sender: TObject);
    begin
      ShowMessage('Exit');
      PostMessage(ActiveControl.Handle, WM_LBUTTONUP, 0, 0);
    end;
    

    But this is just for completeness sake to your question! And it certainly is no tip nor advice! See Sertac's answer for the reason.

    0 讨论(0)
提交回复
热议问题