keep focus on my form

Hi,

I'd like to press the callkey, of the smartphone, with kbd_event method, when there is an incoming call, but, when there is an incoming call, a popup apears end I loose the focus on my program...

So I'd like keep the focus on my form thanks to a loop like this :

DeclareFunction SetForegroundWindowLib "CoreDll.dll" (ByVal hWndAsInteger)AsBoolean

hwin =--handleOfMyForm--

while(true)

SetForegroundWindow(hwin)

End While

But how can I get the Handle of my Form?

[1156 byte] By [jjm] at [2008-1-27]
# 1
Hi jjm,

how about GetCapture?
I'v a little sample in C#:


[DllImport("coredll.dll")] // Get handle
extern static IntPtr GetCapture();
/******* Return handle to my forml **********************/
private static IntPtr GetHandle(System.Windows.Forms.Form theFrm) {

IntPtr hWnd=null; // Capture handle

theFrm.Capture=true;
hWnd=GetCapture();
// WinCE - API
theFrm.Capture=bCapture;
return hWnd;
}

[DllImport(strHelperDLL)]
private static extern int _SetForegroundWindow(int hWnd);
/******* Tries to set the form as foreground window **********************/
/**
***** Returns success of the operation **********************/
public static bool SetForegroundWindow(System.Windows.Forms.Form theFrm) {
try {
IntPtr iPtr=GetHandle(theFrm);
return(_SetForegroundWindow(iPtr.ToInt32())==0 ? false : true );
}
catch {
return(false);
}
}

HTH

Manfred

ManniAT at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 2

Ok, thanks Manfred, it runs with this code :

Me.Capture = True
hwin = GetCapture()
Me.Capture = False

But I fact, it doesn't solve my problem, because with this loop, my form is always on top, even if i run an other application, exepted the phonecall popup...
I thinks that this popup do the same thing, she catch the focus in a loop.

How could i do ?

jjm at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 3
Hi,

your question was "how can I get the handle of my form".

Now you are asking for another thing.
I'm not very shure if I understand your needs, but I assume:
a.) if your app is in foreground you want to keep the foucs
b.) if your app is in the background it should stay there

So how to solve this.
First get the handle of your window with a call to GetHandle(myForm)
What I forgot (missing line) is the boolean to keep the before state!!!
The line to reset it is there.
So a change to the function is needed:
After declaration of the hwnd add
bool bCapture=theFrm.Capture.

So with this and the line before the return (theFrm.Captur=bCapture) you reset the state.
But that is just a little "helping".

So now to the app:
I would do 3 things:
a.) create a timer (duration as long as possible)
the duration tells the time you are without focus after the popup
b.) create a variable to keep the status of Activation (bool bActive)
c.) create a handler for form activation (Activated)
therein set bActive to true
d.) create a handler for form deactivation (Deactivate)
therein set bActive to false
e.) create a timer handler like this

if(bActive) SetForeGroundWindow(this);

So if your form gets hidden (another app on top) bActive is false - and you stay hidden. If you are the foregroundwindow the popup maybe (!!!!) will result in a Deactivated and after closing an Activate.

If this is the fact you can forget the deactivate handler and the timer.
Use the SetForegroudnWindow directly in this function.

To check it out simply place a textbox and in the handlers add some text to show the statuschanges.

Cheers

Manfred

ManniAT at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 4
If you need to fix your form at the top of the z-order, then rather than continually pulling the window to the front, use SetWindowPos (you'll need to P/Invoke) with the HWND_TOPMOST flag. This will keep your form at the top even if it loses focus. See the code for OpenNETCF.Win32.Win32Window for P/Invoke declaration:-
http://vault.netcf.tv/VaultService/VaultWeb/GetFile.aspx?repid=2&path=%24%2fSDF%2fOpenNETCF.Windows.Forms%2fWin32%2fWin32Window.cs&version=2
(username guest, password guest)

Peter

PeterFoot at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...