can't get focus

Windows specific questions.
noop
Posts: 130
Joined: Sep 18, 2006 10:29

can't get focus

Postby noop » Dec 27, 2009 0:10

Hey,

I'm trying to get my program into focus:

I'm launching with "shell" a program.
Afterwards I open a window with screenres.
But this window seems to be below every other window.
Only its icon flashes in the taskbar.

So I tried to force it into foreground.
I tried showwindow -> completely failed.
setforegroundwindow -> failed also.
setactivewindow -> nothing.
setwindowpos(topmost) -> in foreground but no focus.
setfocus -> really nothing~~

Well I have no idea what to do now.
I hope you have an idea.

noop

Edit:
v20
Win7 64bit
fxm
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Dec 27, 2009 7:39

Try to call "ScreenRes" with the flag : GFX_ALWAYS_ON_TOP (&h20), which creates a window that stays always on top :
ScreenRes width, height [, [depth] [, [num_pages] [, [flags] [, refresh_rate ]]]]
SARG
Posts: 904
Joined: May 27, 2005 7:15
Location: FRANCE

Postby SARG » Dec 27, 2009 14:02

Since W2000 or XP the API setforeground has not the same behaviour as in W98.
I tried (for FBdebugger) some solutions found on the WEB but without real success, nearly always the flashing in the taskbar. I guess that fxm gave you the best way.
fxm
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Dec 27, 2009 15:45

Remark about the FreeBasic keyword 'Shell'

Shell sends a command to the system command interpreter, and main program execution is suspended until the command interpreter exits.

From your main program, if you want launch an other program 'MyProgram' from an other path 'MyPath', you code :
'Main program
....................
Shell "cd MyPath & MyProgram"
....................

The main program waits the end of the execution of 'MyProgram' before to continue (synchronous launch).


But there is a way to launch the execution of 'MyProgram' and to continue to execute the main program without wait the end of 'MyProgram' (asynchronous launch), using the command 'start' :
'Main program
....................
Shell "start /dMyPath MyProgram"
....................
noop
Posts: 130
Joined: Sep 18, 2006 10:29

Postby noop » Dec 27, 2009 17:47

thx for your replies.

I'm able to put the window on top of others with the topmost (setwindowpos) but still the focus is somewhere else.
It's really strange: Windows which are set to HWND_TOP receive the focus but not my HWND_TOPMOST window which is in front~~

@fxm:I'm using shell to achieve a synchronous launch.
What I'm exactly doing is mounting a volume with TrueCrypt via command line. When TrueCrypt is done it closes itself automatically and gives back the control to cmd which gives it back to my program. But somehow it screws sth. else up.
I could use a different launch method but this would mean that I have to know how long it takes TrueCrypt to mount a volume (which is pure guessing) or I have to use CreateProcess to "watch" TrueCrypt but this makes it so much more complicated.

Here's extracted code which gives me the trouble:

Code: Select all

#include "windows.bi"
#include "fbgfx.bi"
dim fbgfxHwnd as hwnd
dim key as string
dim as integer depth,rate

screeninfo ,,depth,,,rate
shell "tc.lnk /v "+chr(34)+"s:\s2.tc"+chr(34)+" /l x /h n /c n /q"
screenres 600,360,depth,,&h20,rate
width 600/8,360/16
print "press enter to continue and esc to quit"
ScreenControl(FB.GET_WINDOW_HANDLE,cast(integer,fbgfxHwnd))
setfocus(fbgfxHwnd)
do
    key=inkey$
    sleep 10
    select case key
        case chr(13):exit do
        case chr(27):end
        case chr(255)+"k":end
    end select
loop
showwindow(fbgfxhwnd,sw_hide)
sleep 2000
showwindow(fbgfxhwnd,sw_show)
sleep


Edit:
Ok, even shellexecute doesn't work.
It seems to be a problem of TrueCrypt.
If I use explorer.exe or sth. else it works as it's supposed to.
I still don't get why setfocus cannot handle this problem.
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Postby Zippy » Dec 27, 2009 22:58

I expect that you are using -lang fblite (hopefully not -lang qb), in which case getting the HWND using ScreenControl returns an invalid.. HWND.

I wasn't willing to dl and install TrueCrypt, I was able to reproduce the bad behavior using a test .lnk AND -lang fblite:

Code: Select all

'test code, create .lnk from this exe, "tc-2.lnk"
for c as integer =1 to 5
print c,time
sleep 1000
next


Then code that demos the bad HWND and using GetForegroundWindow that does work:

Code: Select all


#include "windows.bi"
#include "fbgfx.bi"
Dim fbgfxHwnd, thwnd As hwnd
Dim Key As String
Dim As Integer depth,rate

screeninfo ,,depth,,,rate
Shell "tc-2.lnk" 'my test .lnk
screenres 600,360,depth,,&h20,rate
Width 600/8,360/16
Print "press enter to continue and esc to quit"
ScreenControl(FB.GET_WINDOW_HANDLE,cast(Integer,fbgfxHwnd))

thwnd=GetForeGroundWindow
print fbgfxHwnd 'invalid in fblite
print thwnd

setfocus(thwnd)
Do
    Key=Inkey$
    Sleep 10
    Select Case Key
        Case Chr(13):Exit Do
        Case Chr(27):End
        Case Chr(255)+"k":End
    End Select
Loop
showwindow(thwnd,sw_hide)
Sleep 2000
showwindow(thwnd,sw_show)
Sleep

You may want to use FindWindow rather than GetForeground..
noop
Posts: 130
Joined: Sep 18, 2006 10:29

Postby noop » Dec 28, 2009 0:51

No, I'm not using any dialect.
Compiler command is:
"<$fbc>" "<$file>" -s gui

If I use getforegroundwindow while my own window is on top (topmost) I get the handle of the window below which has the focus...

Edit:
Ok, this seems to be an issue of my computer.
It works on Win7 32bit nvidia
Now I'm working on Win7 64bit ati
So either the 64bit Version of Windows is the problem or the ATI driver.
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Postby Zippy » Dec 28, 2009 1:27

noop wrote:No, I'm not using any dialect.
Compiler command is:
"<$fbc>" "<$file>" -s gui

If I use getforegroundwindow while my own window is on top (topmost) I get the handle of the window below which has the focus...

Edit:
Ok, this seems to be an issue of my computer.
It works on Win7 32bit nvidia
Now I'm working on Win7 64bit ati
So either the 64bit Version of Windows is the problem or the ATI driver.

Google ForegroundLockTimeout. If setting that at HKCU\Control Panel\Desktop to "0" doesn't work, try (surely you've done these) executing from the commandline outside of FBIDE, try shell-ing another .lnk (like my test) to isolate TrueCrypt, and including SetEnviron("fbgfx=GDI") at the beginning of your code.

Bizarre.

This has come up before..
Orgie2012
Posts: 4
Joined: Dec 12, 2011 0:58
Location: Canada

Focus lost forever ...

Postby Orgie2012 » Dec 12, 2011 1:08

Hi,

I'm am currently developping an application on Windows 7 lite 32 bits with ATI HD graphics. Everything works fine on this machine.

Today I tested it on another computer which runs Windows 7 64 bits with Intel graphics. The program kept losing focus within a second from startup, in fullscreen mode, so that the display stopped being refreshed. I tried setting flags like always on top, high priority, no switch. I tried screensync, lock/unlock. The fact is, when the program ends, I get a glimpse of the screen as it should be. Similarly, if I switch to another program using Alt-Tab, and then get back to my program, the screen is refreshed. That's why I suspected a focus issue. Everything else works fine. I know I can detect the loss of focus with events, but I have no idea how to take it back. The stange thing is: using sleep, I can still press any key to resume the program, but it still won't refresh the screen even though there is new text of drawing.

Any tip would be much appreciated!

Jim
fxm
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Dec 12, 2011 8:26

By default, GfxLib uses the DirectX driver.

Have you tested the behavior of your fullscreen mode program using the GDI driver (the "safest" one)?
For try that, you have two solutions:
- either insert:
SetEnviron("fbgfx=GDI")
at the top of your program (before initializing the graphic mode),
- or, (requiring fbgfx.bi to be included):
ScreenControl(FB.SET_DRIVER_NAME, "GDI")
(always before initializing the graphic mode)
Orgie2012
Posts: 4
Joined: Dec 12, 2011 0:58
Location: Canada

GDI

Postby Orgie2012 » Dec 25, 2011 19:40

Hi! Thanks for the fast answer. I, on the other hand, don't have Internet home (somehow ironic for a programmer, isn't it?). Hope you don't mind the delay.

Actually, I had no idea I could do that. I really thought DirectX was the only option. So I tried using "SetEnviron("fbgfx=GDI")" as suggested and now it works fine on both machines! I even find it to initialize faster. Thank you so much! Are there any limitations I should now about as opposed to DirectX?
fxm
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: GDI

Postby fxm » Dec 25, 2011 19:55

Orgie2012 wrote:So I tried using "SetEnviron("fbgfx=GDI")" as suggested and now it works fine on both machines!
Have you compare the versions of DirectX between your two machines?
(version of the 'ddraw.dll' file in the 'system32' directory)

Orgie2012 wrote:Are there any limitations I should now about as opposed to DirectX?
I haven't seen much of a performance difference between GDI and DirectX.
Orgie2012
Posts: 4
Joined: Dec 12, 2011 0:58
Location: Canada

DirectX version

Postby Orgie2012 » Dec 25, 2011 20:28

It's the same version: 6.1.7600.16385
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Postby kiyotewolf » Dec 29, 2011 3:41

When I want to use the SAVE AS window in my paint program, I make the program minimize itself and lose focus, then run the Windows API, then after, it regrabs the focus, and pops up again.

You might want to do this..

Code: Select all

dim as HWND GfxHandle

'initalize screen mode before you get a handle pointer from it
'init screen code here..

ScreenControl fb.GET_WINDOW_HANDLE,cast(Integer,GfxHandle)


       ShowWindow(GfxHandle,SW_HIDE)

'REM SHELL CODE GOES HERE..

       ShowWindow(GfxHandle,SW_SHOW)
     
       SetFocus(GfxHandle)





~Kiyote!

Also, alot of times around those commands, I put little delays, to ensure that the computer has thunk enough thinking thoughts to do right what I'm trying to make it do.

P.S.S. You might need to #include something to make that work, not sure.

I use this at the beginning of my program.


#define WIN_INCLUDEALL
#include once "windows.bi"

#include once "fbgfx.bi"
Cherry
Posts: 351
Joined: Oct 23, 2007 12:06
Location: Austria
Contact:

Postby Cherry » Dec 29, 2011 19:07

Try this function:

Code: Select all

Sub ForceToForeground(hWnd As HWND)
   Var fgWnd = GetForegroundWindow()
   AttachThreadInput(GetCurrentThreadId(), GetWindowThreadProcessId(hWnd, NULL), TRUE)
   AttachThreadInput(GetCurrentThreadId(), GetWindowThreadProcessId(fgWnd, NULL), TRUE)
   SetForegroundWindow(hWnd)
   BringWindowToTop(hWnd)
   AttachThreadInput(GetCurrentThreadId(), GetWindowThreadProcessId(fgWnd, NULL), FALSE)
   AttachThreadInput(GetCurrentThreadId(), GetWindowThreadProcessId(hWnd, NULL), FALSE)
End Sub


What also often helps, is...

Code: Select all

ShowWindow(hWnd, SW_MINIMIZE)
ShowWindow(hWnd, SW_RESTORE)

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 21 guests