Newsgroups : Borland : borland.public.delphi.rtl.win32 : 2006 Aug : Left mouse button down and focus changes
| Subject: | Left mouse button down and focus changes |
| Posted by: | "Ian Boyd" (ian.borlandnews0..@avatopia.com) |
| Date: | Wed, 2 Aug 2006 09:39:26 |
i've come across an exception when using a 3rd party control (Virtual
Treeview). i am posting here because i don't know who's fault it is and how
it should be handled.
In a WM_LBUTTONDOWN handler:
procedure TBaseVirtualTree.WMLButtonDown(var Message: TWMLButtonDown);
var
TreeNodeBeingHitWithMouse : PVirtualNode; //psuedo-code
begin
inherited;
[...snip...]
TreeNodeBeingHitWithMouse := GetNodeBeingHitByCursorRightNow;
//pseudo-code
[..snip...]
// Focus change. Don't use the SetFocus method as this does not work for
MDI windows.
if not Focused and CanFocus then
Windows.SetFocus(Handle);
[..snip...]
//Use TreeNodeBeingHitWithMouse
[...snip...]
end;
Elsewhere on the form, onExit of an edit box, the treeview is being cleared
and refilled (searching for new results)
procedure TForm1.Edit1Exit(Sender: TObject);
begin
//Clear the tree
VirtualTree.Clear; //pseudo-code
//Add new nodes based on search results
VirtualTree.Add('Alpha'); //pseudo-code
VirtualTree.Add('Bravo'); //pseudo-code
VirtualTree.Add('Charlie'); //pseudo-code
end;
So what's happening is when you are focused inside Edit1, and then click on
the Virtual Treeview, the WM_LBUTTONDOWN fires on the treeview, but not the
OnExit of the edit box. Then during the call to SetFocus(), the OnExit
event of Edit1 finally fires, clearing the treeview, and rendering the stack
variable TreeNodeBeingHitWithMouse back in WMLButtondown into an invalid
reference; and when you go to use it you'll get an AV.
So
i. what is the right way in Windows to do focus changes?
ii. i thought Windows itself does it, sending messages telling windows that
they are gaining and losing focus, and who you are gaining it from/losing it
to?
iii. Doesn't Windows send focus loss messages to the old window before
sending button down messages to the new window?
iv. Does Windows send button down messages before any focus changing logic?
v. Is focus changes in a Windows app cooperative? Am i sent a focus message,
but i actually have to use SetFocus to make it happen?
vi. Is focus changes automatic, and SetFocus is only if i want to move focus
separate from Windows' automatic focus management?
vii.Why would Mike need to call SetFocus at all?
viii. Is having code in an OnExit of a control bad?
ix. If Mike didn't call SetFocus (API or method call), would focus still
change?
x. Does Mike have to call SetFocus (API or method call) and without it focus
won't change in Windows?
Basically, i'd like the why answer, then the what and how.