delphi - How to prevent from loss of focus when showing and/or closing secondary forms from the main form? -


showing 2 secondary forms main form , closing both forms cause main form lose focus. (another application gets activated instead of mine)

the secondary forms created either main form directly or creating third form second form.

the secondary forms set cafree in onclose event:

 procedure tform1.formclose(sender: tobject; var action: tcloseaction); begin   action := cafree; end; 

using delphi 2009 (update 3 & 4) xp sp3.

here steps reproducing problem:

  1. create new vcl forms applications
  2. assign onclose event above
  3. drag button onto created form
  4. in click handler create new tform1 , show below

run program. click button show second form. click button on second form create third form. when closing both new forms main form lose focus.

this code in button click event handler:

 tform1.create(application)     show; 

is there way stop main form losing focus?

(interestingly, when creating both secondary forms directly main form, issue appear when closing first created form second created form)


in past had the same issue solved updating delphi installation, in scenario didn't use cafree in onclose event cause bug.

a recommendation set parent property on secondary forms main form, makes new forms bounded main form i'd rather not have. (and solution proposed there reactivate main form causes activation order of forms lost)

i manually activate 'owning' window api call before 1 of forms close:

procedure tform1.formclose(sender: tobject; var action: tcloseaction); begin   action := cafree;   setforegroundwindow(getwindowlong(handle, gwl_hwndparent)); end; 

this won't problem os (i.e. no flashing task bar button) because our application in foreground.

if mainformontaskbar set, owning window our main form, if not hidden application window. in either case application stay in foreground.

the setforegroundwindow call redundant when closing last form - main form, fail if mainformontaskbar true since main form not owned, wouldn't care (then again 1 can of course include test before calling it)..


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -