Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

I found this question on Experts-Exchange.

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

The problem can be replicated easily.

place 3 tedits on a form. write a showmessage('exit') in edit1's onexit event run the program give edit1 focus use the mouse to give edit3 focus, click ok to the showmessage observe how you can't write anything in edit3 now, until you click with the mouse somewhere on the form ! give edit2 focus, then use to the mouse to give edit3 focus observe how you can type what you want in edit3 now !

So far I've established that the problem lies in the fact that edit3 doesn't receive a mouseup-message when the old controls onExit event displays a window of any kind, i've tried it as well with showing a form of my own in the onExit event, same result. In fact, windows is under the impression that the mouse is held down over edit3 after you've clicked Ok to the showmessage

I guess it's a bug in Delphi/Windows but how to work around it ? I know i can force a WM_LBUTTONUP on edit3's onMouseDown event (since its the last event called in the process) but that's more than tedious, and not always applicable

I am trying to do something similiar:

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.7k users

...