Board index » cppbuilder » Loosing form input focus of Modal Form

Loosing form input focus of Modal Form


2004-07-31 05:25:50 AM
cppbuilder48
All,
I have the following setup:
Two applications on a single PC using a touch screen. One of the
applications get events in from an external source and that cause the
application to immediately take input focus on the computer.
BringToFront is used.
To switch back to the other application a button is selected on the
application with input focus and via the windows messaging system the
other application is informed to become the foreground application.
Now here is my problem.
If there is a sub-form open in Modal format and an automatic switch
occurs and I then switch back to the original application I loose the
input focus to this modal form. In fact the form is not shown at all.
The application main form is shown in stead. I have to do a single
Alt-Tab to get the modal form back. Why is this. Shouldn't Modal have
the always on top set in this case.
Interesting enough is that I also call sub forms from a Visual C++
generated DLL and these sub forms in modal format come to the foreground
when I force a switch back to the application.
Am I missing a setting on my form properties. I have tried AlwaysOnTop,
but NO.
This is what I do in a switch request:
Sending Side
hndl = FindWindow(WindowClassStr, NULL);
if (hndl)
{
BringWindowToTop(hndl);
PostMessage(hndl, WM_USER + 171, Switch_Command, 0);
}
WindowClassStr is the class name of the main form of the application to
switch to.
Thanks
Werner
 
 

Re:Loosing form input focus of Modal Form

Werner Deyzel < XXXX@XXXXX.COM >wrote:
Quote
[...]
BringWindowToTop(hndl);
From the win32.hlp file:
Use the BringWindowToTop function to uncover any window
that is partially or completely obscured by other windows.
Calling this function is similar to calling the SetWindowPos
function to change a window's position in the Z order.
BringWindowToTop does not make a window a top-level window.
If an application is not in the foreground and wants to be
in the foreground, it should call the SetForegroundWindow
function.
Quote
PostMessage(hndl, WM_USER + 171, Switch_Command, 0);
You might be interested in reading about message management:
www.flounder.com/messages.htm
If using SetForegroundWindow doesn't solve your problem, what
I would do is use GetWindowLong on one of those DLL forms that
do work. You can then examine the Style and ExStyle of that
window (flags can be found in the win32.hlp under CreateWindow
and CreateWindowEx).
Once you know what flags to apply to the form, you can override
it's CreatParams method to get the job done or you can use the
win32 API SetWindowLong.
That should work but if it doesn't, the final option is to use
a global variable that the application can check and bring the
modal form to front - if there is one:
private:
TForm *pModalForm;
void __fastcall TMain::MyAppActivate(TObject *Sender);
//-------------------------------------------------------------
__fastcall TMain::TMain(TComponent* Owner) : TForm(Owner)
{
pModalForm = NULL;
Application->OnActivate = MyAppActivate;
}
//-------------------------------------------------------------
void __fastcall TMain::MyAppActivate(TObject *Sender)
{
if( pModalForm )
{
::SetForegroundWindow( pModalForm->Handle );
or
pModalForm->BringToFront();
}
}
//-------------------------------------------------------------
void __fastcall TMain::Button1Click(TObject *Sender)
{
// allocate your form if it's dynamic
pModalForm = dynamic_cast<TForm*>( MyModalForm );
MyModalForm->ShowModal();
pModalForm = NULL;
}
//-------------------------------------------------------------
~ JD
 

Re:Loosing form input focus of Modal Form

Thanks JD,
I used a combination of your suggestions to take care of the problem.
Regards
WD
JD wrote:
Quote
Werner Deyzel < XXXX@XXXXX.COM >wrote:

>[...]
>BringWindowToTop(hndl);


From the win32.hlp file:

Use the BringWindowToTop function to uncover any window
that is partially or completely obscured by other windows.
Calling this function is similar to calling the SetWindowPos
function to change a window's position in the Z order.
BringWindowToTop does not make a window a top-level window.
If an application is not in the foreground and wants to be
in the foreground, it should call the SetForegroundWindow
function.


>PostMessage(hndl, WM_USER + 171, Switch_Command, 0);


You might be interested in reading about message management:

www.flounder.com/messages.htm

If using SetForegroundWindow doesn't solve your problem, what
I would do is use GetWindowLong on one of those DLL forms that
do work. You can then examine the Style and ExStyle of that
window (flags can be found in the win32.hlp under CreateWindow
and CreateWindowEx).

Once you know what flags to apply to the form, you can override
it's CreatParams method to get the job done or you can use the
win32 API SetWindowLong.

That should work but if it doesn't, the final option is to use
a global variable that the application can check and bring the
modal form to front - if there is one:


private:
TForm *pModalForm;
void __fastcall TMain::MyAppActivate(TObject *Sender);
//-------------------------------------------------------------
__fastcall TMain::TMain(TComponent* Owner) : TForm(Owner)
{
pModalForm = NULL;
Application->OnActivate = MyAppActivate;
}
//-------------------------------------------------------------
void __fastcall TMain::MyAppActivate(TObject *Sender)
{
if( pModalForm )
{
::SetForegroundWindow( pModalForm->Handle );
or
pModalForm->BringToFront();
}
}
//-------------------------------------------------------------
void __fastcall TMain::Button1Click(TObject *Sender)
{
// allocate your form if it's dynamic
pModalForm = dynamic_cast<TForm*>( MyModalForm );
MyModalForm->ShowModal();
pModalForm = NULL;
}
//-------------------------------------------------------------

~ JD

 

{smallsort}

Re:Loosing form input focus of Modal Form

Werner Deyzel < XXXX@XXXXX.COM >wrote:
Quote
Thanks JD,

I used a combination of your suggestions to take care of the problem.
Well? Gonna share?
~ JD