Page 1 of 1

Set window to always active?

Posted: Mon Jun 09, 2014 3:22 am
by garretthylltun
Is there a way(didn't find anything with forum search) to always make my window active, even when the user moves on to another program window?

What I'm trying to do is: I'm making an AppBar for launching programs. Sits at the top of the desktop. I use a menu bar. What I'm trying to achieve is that when my program loses focus that the menu bar remains active so the menu texts do not go gray.

I can do this in another dev system I use, but do not know the coding behind it, as it's simply a property that's on or off in that dev system. I'm assuming that this will likely be an api call, but haven't a clue where to start looking for such information or what to look for.

Any tips in the right direction greatly appreciated and thanks in advance,
~Garrett

Re: Set window to always active?

Posted: Mon Jun 09, 2014 10:54 am
by luis
garretthylltun wrote:Is there a way(didn't find anything with forum search) to always make my window active, even when the user moves on to another program window?
http://www.purebasic.fr/english/viewtop ... 13&t=58521

http://www.purebasic.com/documentation/ ... indow.html

Re: Set window to always active?

Posted: Mon Jun 09, 2014 4:23 pm
by garretthylltun
Much appreciated Luis, but that only sets the window to ONTOP. If my program loses focus, it is no longer active, ontop yes, but active now and my menu texts turn gray. :-(

Thanks,
~Garrett

Re: Set window to always active?

Posted: Mon Jun 09, 2014 4:51 pm
by luis
... to always make my window active, even when the user moves on to another program window?
... is that when my program loses focus that the menu bar remains active
I know this not answer your question (sorry I misunderstood) and I don't have an answer off the top of my head, never tried it.
But if it's a menu and you haven't set some system wide shortcut you must go back to your window to be able to select something from the menu, so why you want the menu to give the visual clue is active when it's not ? The menu will not work until your window will regain the focus anyway.

And how could you keep a window active when you switched to another one ? Should it receive the keystrokes even if you are typing in another window ? Should receive mouse movements even if the pointer is on another window ? It's a good thing to be able to make a window active and the previous one inactive and overriding the visual clues I'm not sure it's a good idea.

EDIT: I see you really intended "to make my window [look] active" not being active, that's different.

In the taskbar I see only some application launchers (little icons), the tray area (icons) and the running programs (icons + text), not a menu bar, anyway...

Re: Set window to always active?

Posted: Mon Jun 09, 2014 5:30 pm
by garretthylltun
I'm making an AppBar. Similar in nature to the Windows TaskBar or SideBar. Notice how when you are not using the TaskBar or SideBar that all texts remain as active? Mine is just a menu bar for launching programs but I wish to at least have it work as closely as possible as AppBar style programs work, which is, they retain an always active look, but do not have the focus. This does not mean that it takes the focus away from whatever program the user is currently using and it does not affect the hotkeys of the program the user is currently using.

~Garrett

Re: Set window to always active?

Posted: Mon Jun 09, 2014 10:21 pm
by RASHAD
Workaround for windows
It could be much simpler without Menu
If you like it try to adapt it for your needs

Code: Select all

;#CAPTUREBLT = $40000000

Global hWnd,hhkLLMouse

Procedure MouseHook(nCode, wParam, lParam)
    *p.MOUSEHOOKSTRUCT = lParam
     If *p\pt\x >= WindowX(0) And *p\pt\x <= (WindowX(0)+WindowWidth(0)) And *p\pt\y >= WindowY(0) And *p\pt\y <= (WindowY(0)+WindowHeight(0))
         ResizeWindow(1,0,0,0,0)
         SetForegroundWindow_(WindowID(0))
      Else
        BringWindowToTop_(WindowID(1))
        ResizeWindow(1,WindowX(0)-4,WindowY(0)-4,414,340)
     EndIf
  
  ProcedureReturn CallNextHookEx_(0, nCode, wParam, lParam)
EndProcedure

OpenWindow(0,0,0,400,300,"",#PB_Window_SystemMenu| #PB_Window_MaximizeGadget| #PB_Window_ScreenCentered)
  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
      MenuItem( 1, "&Load...")
      MenuItem( 2, "Save")
      MenuItem( 3, "Save As...")
      MenuBar()
      OpenSubMenu("Recents")
        MenuItem( 5, "Pure.png")
        MenuItem( 6, "Basic.jpg")
        OpenSubMenu("Even more !")
          MenuItem( 12, "Yeah")
        CloseSubMenu()
        MenuItem( 13, "Rocks.tga")
      CloseSubMenu()
      MenuBar()
      MenuItem( 7, "&Quit")

    MenuTitle("Edition")
      MenuItem( 8, "Cut")
      MenuItem( 9, "Copy")
      MenuItem(10, "Paste")
      
    MenuTitle("?")
      MenuItem(11, "About")

  EndIf
  
ButtonGadget(1,10,200,80,22,"Test #1")
ButtonGadget(2,10,225,80,22,"Test #2")
DisableGadget(1,1)
DisableGadget(2,1)
Delay(300)

GetWindowRect_(WindowID(0),r.RECT)
hBitmap = CreateImage(0, 418,340)
hdc = StartDrawing(ImageOutput(0))
SelectObject_(hdc, hBitmap)
BitBlt_(hdc, 0,0,418,340, GetDC_(GetDesktopWindow_()), r\left-4, r\top-4, #SRCCOPY);| #CAPTUREBLT)
StopDrawing()
DeleteDC_(hdc)
;SaveImage(0,"e:\imagetest.bmp")

OpenWindow(1,WindowX(0),WindowY(0),400,300,"", #PB_Window_BorderLess)
ImageGadget(3,0,0,418,310,ImageID(0))

DisableGadget(1,0)
DisableGadget(2,0)
hhkLLMouse = SetWindowsHookEx_(#WH_MOUSE_LL, @MouseHook(), GetModuleHandle_(0), 0)

Repeat
           
  Select WaitWindowEvent()
      
      Case #PB_Event_CloseWindow
            Quit = 1
            
      Case #PB_Event_Gadget
             Select EventGadget()
                 Case 1
                         Debug "# 1"
                 
                 Case 2
                          Debug "# 2"
                          
             EndSelect             
  EndSelect 
Until Quit = 1
UnhookWindowsHookEx_(hhkLLMouse)
End
# 2 :
Press START to begin

Code: Select all

#CAPTUREBLT = $40000000

Global hWnd,hhkLLMouse,Run

Procedure MouseHook(nCode, wParam, lParam)
    *p.MOUSEHOOKSTRUCT = lParam
     If *p\pt\x >= WindowX(0) And *p\pt\x <= (WindowX(0)+WindowWidth(0)) And *p\pt\y >= WindowY(0) And *p\pt\y <= (WindowY(0)+WindowHeight(0))
         If IsWindow(1)
               CloseWindow(1)
               Run = 0
         EndIf
         SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_HOTKEY, WindowID(0))
      Else
      SmartWindowRefresh(0,1)
        If Run = 0 And IsImage(0)
              Run +1
              OpenWindow(1,WindowX(0)-4,WindowY(0)-4,415,340,"", #PB_Window_BorderLess)
                 ImageGadget(3,0,0,415,310,ImageID(0))
           EndIf
           SmartWindowRefresh(0,0)
     EndIf
 
  ProcedureReturn CallNextHookEx_(0, nCode, wParam, lParam)
EndProcedure

OpenWindow(0,0,0,400,300,"",#PB_Window_SystemMenu| #PB_Window_MaximizeGadget| #PB_Window_ScreenCentered)
  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
      MenuItem( 1, "&Load...")
      MenuItem( 2, "Save")
      MenuItem( 3, "Save As...")
      MenuBar()
      OpenSubMenu("Recents")
        MenuItem( 5, "Pure.png")
        MenuItem( 6, "Basic.jpg")
        OpenSubMenu("Even more !")
          MenuItem( 12, "Yeah")
        CloseSubMenu()
        MenuItem( 13, "Rocks.tga")
      CloseSubMenu()
      MenuBar()
      MenuItem( 7, "&Quit")

    MenuTitle("Edition")
      MenuItem( 8, "Cut")
      MenuItem( 9, "Copy")
      MenuItem(10, "Paste")
     
    MenuTitle("?")
      MenuItem(11, "About")

  EndIf
 
ButtonGadget(1,10,200,80,22,"START")
ButtonGadget(2,10,225,80,22,"Test #2")
;Delay(300)

hhkLLMouse = SetWindowsHookEx_(#WH_MOUSE_LL, @MouseHook(), GetModuleHandle_(0), 0)

Repeat
           
  Select WaitWindowEvent()
     
      Case #PB_Event_CloseWindow
            Quit = 1
           
      Case #PB_Event_Gadget
             Select EventGadget()
                 Case 1
                         GetWindowRect_(WindowID(0),r.RECT)
                                       hBitmap = CreateImage(0, 418,340)
                                       hdc = StartDrawing(ImageOutput(0))
                                       SelectObject_(hdc, hBitmap)
                                       BitBlt_(hdc, 0,0,418,340, GetDC_(GetDesktopWindow_()), r\left-4, r\top-4, #SRCCOPY| #CAPTUREBLT)
                                       StopDrawing()
                                       DeleteDC_(hdc)
                 
                 Case 2
                          Debug "# 2"
                         
             EndSelect             
  EndSelect
Until Quit = 1
UnhookWindowsHookEx_(hhkLLMouse)
End
Edit : # 2 bugs fixed for windows 7

Re: Set window to always active?

Posted: Tue Jun 10, 2014 3:15 am
by garretthylltun
Thanks a bunch RASHAD, I will definitely experiment with this.

Thanks,
~Garrett

Re: Set window to always active?

Posted: Thu Jun 12, 2014 11:54 am
by RASHAD
Hi
- New approach no Low Level mouse hooking
- Less flickering
- Less coding :P
Tested with PB 5.22 x86 Win 8.1 x64

Code: Select all

#CAPTUREBLT = $40000000

Procedure CaptureWin(*Value)
    GetWindowRect_(WindowID(0),r.RECT)
		hBitmap = CreateImage(0, 418,340)
		hdc = StartDrawing(ImageOutput(0))
		SelectObject_(hdc, hBitmap)
		BitBlt_(hdc, 0,0,418,340, GetDC_(GetDesktopWindow_()), r\left-4, r\top-4, #SRCCOPY| #CAPTUREBLT)
		StopDrawing()
		DeleteDC_(hdc)
EndProcedure

Procedure WinCallback(hWnd, uMsg, wParam, lParam)     
  Select uMsg
    Case #WM_ACTIVATEAPP
      OpenWindow(1,WindowX(0)-4,WindowY(0)-4,415,338,"" ,#WS_POPUP |#PB_Window_BorderLess)
      BringWindowToTop_(WindowID(1))
		        UseGadgetList(WindowID(1))
		        ImageGadget(3,0,0,415,338,ImageID(0))          
  EndSelect
  ProcedureReturn #PB_ProcessPureBasicEvents 
EndProcedure 

If OpenWindow(0, 0, 0, 400, 300, "Window", #PB_Window_MinimizeGadget| #PB_Window_MaximizeGadget| #PB_Window_ScreenCentered) 
  SetWindowCallback(@WinCallback())
  
  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
      MenuItem( 1, "&Load...")
      MenuItem( 2, "Save")
      MenuItem( 3, "Save As...")
      MenuBar()
      OpenSubMenu("Recents")
        MenuItem( 5, "Pure.png")
        MenuItem( 6, "Basic.jpg")
        OpenSubMenu("Even more !")
          MenuItem( 12, "Yeah")
        CloseSubMenu()
        MenuItem( 13, "Rocks.tga")
      CloseSubMenu()
      MenuBar()
      MenuItem( 7, "&Quit")

    MenuTitle("Edition")
      MenuItem( 8, "Cut")
      MenuItem( 9, "Copy")
      MenuItem(10, "Paste")
      
    MenuTitle("?")
      MenuItem(11, "About")

  EndIf
  
ButtonGadget(1,10,200,80,22,"START")
ButtonGadget(2,10,225,80,22,"Test #2")
Delay(300)

Thread = CreateThread(@Capturewin(),25)

If IsImage(0)
  KillThread(Thread)
EndIf

 ;Debug IsAppThemed_()
Repeat

  Select WaitWindowEvent() 
    Case #PB_Event_CloseWindow 
      End 
      
    Case #WM_MOUSEMOVE
            GetCursorPos_ (@p.POINT) 
            ScreenToClient_ (WindowID(0), @p)
            If ChildWindowFromPoint_ (WindowID(0), p\y<< 32+p\x) = 0 And IsWindow(1)
              SendMessage_(#HWND_BROADCAST, #WM_SYSCOMMAND, #SC_HOTKEY, WindowID(0))
              If IsWindow(1)
                CloseWindow(1)
              EndIf
            EndIf

  EndSelect 
ForEver  
EndIf

Edit : Bugs Fixed

Re: Set window to always active?

Posted: Thu Jun 12, 2014 5:15 pm
by garretthylltun
Hi RASHAD,

Does it need two windows to work? I haven't tried this with my project yet, but will this work with a borderless window?

Also got an error when I closed your example without having used any of the buttons.

Code: Select all

[ERROR] untitled2.pb (Line:  16)
[ERROR]The specified #Window is not inialised.
Running PB 5.22 LTS
Windows 7 (32bit)

Thanks,
~Garrett

Re: Set window to always active?

Posted: Thu Jun 12, 2014 6:15 pm
by RASHAD
Hi
Bugs fixed for Win 7
Check and report

Unfortunately I did not find any other way even with lock windows ghosting for one task
Yes you have to use a border less window as long as you created a menu other than that you can create a titled one
You can create as many threads but not tasks for now
Maybe next PB ver. will support Parallel computing (task parallelism)

Re: Set window to always active?

Posted: Fri Jun 13, 2014 7:03 pm
by RASHAD