Title: How to control focus in a MDI application?
Question: How to control focus in a MDI application?
Answer:
Status: MDI-application. MDI window and one child window. MDI window owns a
TPanel component, which owns any component which can get focus (TEdit for example).
Child window owns a TDBGrid component.
Problem: After running this simple test application, the focus is set on child
window's first focusable component - TDBGrid. After switching focus to TEdit
component owned by MDI window, there is no more possibility to switch focus back
to TDBGrid component owned by child window. TDBGrid component is immune to any
mouse events. Why? It looks like a child window is thinking about still having
focus.
This is one of the many shortcomings of the Windows MDI framework, it has never
been designed to cope with controls outside the MDI children that can take the
focus. You can trick it by sending a WM_MDIACTIVATE message to the active MDI
child, here demonstrated by an OnClick handler for a combobox on the toolbar:
procedure TMainForm.ComboBox1Click(Sender: TObject);
begin
{ ... other actions }
if Assigned( ActiveMDIChild ) then
with ActiveMDIChild do
sendmessage( handle, wm_mdiactivate, 0, handle );
end;