December 05, 2019, 02:58:39 pm


Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........

Setting the focus back to your program

Started by Andy, November 23, 2018, 03:57:14 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


A normal process of using a program, then minimising it or opening another program for example means your program loses focus.

In the constants search program version 5 (and below - link here):;topicseen#msg45852

You will see that when you click on a constant in the list view and then select "Search the Internet" your browser will open and show you the search results, okay no problem there.

But when you return to your program (in this case the constants search program), the program itself has lost focus. So trying to use the Up and Down keyboard keys to navigate the list view does not work.

Now wouldn't it be nice if when you return to viewing the program, the focus would automatically return to it and you could use those Up / Down keys again without having to click the list view?

Here is a simple way to do it......

Add these variables to the program.

Code Select
int NoFocus = 0
int hwndx,hwndy

Just before the WAITUNTIL command in your program add this:

Code Select

hwndx = GetForegroundWindow()    '<------- this line

In a timer section of the window's handler add this:

Code Select

hwndy = GetForegroundWindow()

'The program is not the one in focus
if hwndx <> hwndy
   NoFocus = 1

'The prgram is now back in focus
if NoFocus = 1 and hwndx = hwndy
   NoFocus = 0
   setfocus win,ListView

So now when you minimise the program, do something else, and then restore your program back to view it, it now has the focus back on the list view and you can use those Up / Down arrow keys again without having to click the list view.

Just thought it might help someone - and of course, I'm sure there are other ways to do this.

Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.