Window Focus ?

Just starting out? Need help? Post your questions and find answers here.
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Window Focus ?

Post by Nubcake »

I'm using SetWindowState() to return a window's state back to normal however the focus is not set on the window so I tried to use SetActiveWindow but it failed so does StickyWindow , are there reasons to why this is happening?/Solutions to this problem?

Edit:

PROBLEM - If there is a webgadget in the window and it's minimized , when you try to use SetWindowState(#Window,#PB_Window_Normal) it will work ONLY if the focus is not in PB IDE/elsewhere however it won't be brought up on the current layer , stickywindow will help but then there's no way to set the focus , how do you fix this problem? :cry:

Test code proof

Code: Select all

OpenWindow(0,0,0,320,240,"",#PB_Window_Minimize | #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
WebGadget(0,0,0,320,240,"")

Global r
Procedure max(a)
  Delay(1000)
  SetWindowState(0,#PB_Window_Normal)
  StickyWindow(0,1)
  r=1
EndProcedure

CreateThread(@max(),0)

Repeat
  If r=1
    SetActiveWindow(0)
    r=0
  EndIf   
Until WaitWindowEvent(10)=#WM_CLOSE
Last edited by Nubcake on Tue Jun 19, 2012 4:41 pm, edited 3 times in total.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Window Focus ?

Post by IdeasVacuum »

SetActiveWindow() is the one to use - if it fails, post a working snippet that demonstrates the failure.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Re: Window Focus ?

Post by Nubcake »

IdeasVacuum wrote:SetActiveWindow() is the one to use - if it fails, post a working snippet that demonstrates the failure.

Code: Select all

OpenWindow(0,0,0,320,240,"",#PB_Window_Minimize | #PB_Window_ScreenCentered | #PB_Window_SystemMenu)

Procedure max(a)
  Delay(1000)
  SetWindowState(0,#PB_Window_Normal)
  Delay(500)
  SetActiveWindow(0)
EndProcedure 

CreateThread(@max(),0)

Repeat : Until WaitWindowEvent()=#WM_CLOSE

Random Question: Is SetGadgetText() with the webgadget threadsafe? I've been using it and it works however I've been getting IMA 184 sometimes.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Window Focus ?

Post by IdeasVacuum »

....the webgadet itself is not thread safe so, treading on egg shells there.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Re: Window Focus ?

Post by Nubcake »

IdeasVacuum wrote:....the webgadet itself is not thread safe so, treading on egg shells there.
So why does it still work sometimes and not throw up an error :| It is advised to use 'unthreadsafe' functions for the webgadget in the main program thread? At first i thought it was threadsafe because it worked in a thread and did not give any errors.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Window Focus ?

Post by IdeasVacuum »

Window Focus: Well, seems it is the use of the thread that is the issue as it works fine without CreateThread().
Try this:

Code: Select all

OpenWindow(0,0,0,320,240,"",#PB_Window_Invisible | #PB_Window_ScreenCentered | #PB_Window_SystemMenu)

Procedure max(a)
;-------------------
  Delay(100)
  HideWindow(0,#False)

EndProcedure


     CreateThread(@max(),0)
  SetActiveWindow(0)

Repeat : Until WaitWindowEvent()=#WM_CLOSE
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Re: Window Focus ?

Post by Nubcake »

IdeasVacuum wrote:Window Focus: Well, seems it is the use of the thread that is the issue as it works fine without CreateThread().
Try this:

Code: Select all

OpenWindow(0,0,0,320,240,"",#PB_Window_Invisible | #PB_Window_ScreenCentered | #PB_Window_SystemMenu)

Procedure max(a)
;-------------------
  Delay(100)
  HideWindow(0,#False)

EndProcedure


     CreateThread(@max(),0)
  SetActiveWindow(0)

Repeat : Until WaitWindowEvent()=#WM_CLOSE
I changed it to how i wrote it in the original snippet and it seems to be working now however in my actual code i wasn't calling it from a thread and it didn't work but i guess SetActiveWindow does not work with threads.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Window Focus ?

Post by IdeasVacuum »

So why does it still work sometimes and not throw up an error
Luck :|
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Re: Window Focus ?

Post by Nubcake »

I thought it a function was not threadsafe it would immediately throw up an error when it was being used.
Zach
Addict
Addict
Posts: 1677
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Window Focus ?

Post by Zach »

No, it just means that it is not safe to use with threads because the behavior is unstable and unpredictable.

So you may get an error sometimes, or you may not.
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Re: Window Focus ?

Post by Nubcake »

Zach wrote:No, it just means that it is not safe to use with threads because the behavior is unstable and unpredictable.

So you may get an error sometimes, or you may not.
Ok thanks for clearing that up I know now not to use functions in threads which are not threadsafe with the webgadget now :D

Edit:

PROBLEM - If there is a webgadget in the window and it's minimized , when you try to use SetWindowState(#Window,#PB_Window_Normal) it will work ONLY if the focus is not in PB IDE/elsewhere however it won't be brought up on the current layer , stickywindow will help but then there's no way to set the focus , how do you fix this problem? :cry:

Test code to prove it

Code: Select all

OpenWindow(0,0,0,320,240,"",#PB_Window_Minimize | #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
WebGadget(0,0,0,320,240,"")

Global r
Procedure max(a)
  Delay(1000)
  SetWindowState(0,#PB_Window_Normal)
  StickyWindow(0,1)
  r=1
EndProcedure

CreateThread(@max(),0)

Repeat 
  If r=1
    SetActiveWindow(0)
    r=0
  EndIf   
Until WaitWindowEvent(10)=#WM_CLOSE

IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Window Focus ?

Post by IdeasVacuum »

Code: Select all

Global r

Procedure OpenWin()
;------------------

If OpenWindow(0,0,0,320,240,"",#PB_Window_Minimize | #PB_Window_ScreenCentered | #PB_Window_SystemMenu)

         WebGadget(1,0,0,320,240,"")
EndIf

EndProcedure


Procedure max(a)
;---------------
  Delay(1000)
  SetWindowState(0,#PB_Window_Normal)
  r=1
EndProcedure

     OpenWin()
CreateThread(@max(),0)

Repeat

  If r=1
    SetActiveWindow(0)
       StickyWindow(0,#True)
    r=0
  EndIf 
  
Until WaitWindowEvent(10)=#WM_CLOSE
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Re: Window Focus ?

Post by Nubcake »

Still doesn't work for some reason , did you set the focus in the PB IDE and try it ? When you do that the window will be set to normal state but not have the focus on.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Window Focus ?

Post by IdeasVacuum »

Well, I saved it as an exe - but you can't have two Windows with focus. With the PB IDE in focus, if I double-click the exe, the explorer Window has the focus momentarily, then the exe has focus (and not PB or the Explorer Window). Then of course if PB is clicked, it now has the focus again........
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Re: Window Focus ?

Post by Nubcake »

I know 2 windows can't have the focus however if you comment out the webgadget it should work properly but I need to have a webgadget in my program I still don't understand why it doesn't set the focus to the window with a webgadget inside of it.
Post Reply