Disable third-party window movement?

Windows specific forum
BarryG
Addict
Addict
Posts: 3322
Joined: Thu Apr 18, 2019 8:17 am

Disable third-party window movement?

Post by BarryG »

Hi, the following code works great for Win32 windows (Notepad, RegEdit, etc) but not for UWP apps (Calculator, Calendar, etc). How can I make it work with UWP apps? It's for a tool where the user wants to lock a window to certain spot. Thanks.

Code: Select all

; Run this code and click a window within 3 seconds to stop it moving.

Procedure DisableWindowMovement(hWnd,yn)
  RemoveMenu_(GetSystemMenu_(hWnd,1-yn),1,#MF_DISABLED|#MF_BYPOSITION)
  DrawMenuBar_(hWnd)
EndProcedure

Sleep_(3000)
DisableWindowMovement(GetForegroundWindow_(),1)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Disable third-party window movement?

Post by RASHAD »

Hi
Adapt it for your need

Code: Select all

Prototype DwmGetWindowAttribute(hWnd,dwAttribute.l,*pvAttribute,cbAttribute.l)
Global DwmGWA.DwmGetWindowAttribute,hWnd,title${#MAX_PATH},class${#MAX_PATH}
#DWMWA_CLOAKED = 14

Global handle

Procedure GetWinHandle(pro$)
  hWnd = FindWindow_(0, 0)
  Repeat
    hWnd = GetWindow_(hWnd, #GW_HWNDNEXT)
    If hWnd And IsWindowVisible_(hWnd) And GetWindowLongPtr_(hWnd, #GWL_HWNDPARENT) = 0
      GetWindowText_(hWnd, @title$, #MAX_PATH)
      GetClassName_(hWnd,@class$,#MAX_PATH)
       If class$ = "ApplicationFrameWindow" Or class$ = "Windows.UI.Core.CoreWindow"
        dll = OpenLibrary(#PB_Any,"DWMAPI.DLL")
        If dll
          DwmGWA = GetFunction(dll,"DwmGetWindowAttribute")
          If DwmGWA
            DwmGWA(hWnd,#DWMWA_CLOAKED,@Cloaked,SizeOf(Cloaked))
            If Cloaked = 0 And title$ = pro$
              handle = hwnd
              ProcedureReturn handle
            EndIf
          EndIf
          CloseLibrary(dll)
        EndIf
      EndIf
    EndIf
  Until hWnd = 0
  ProcedureReturn #False
EndProcedure

RunProgram("calc.exe")
Delay(1000)
GetWinHandle("Calculator")

Repeat
  Delay(100)
  MoveWindow_(handle,400,400,400,600,1)
  
ForEver
Egypt my love
BarryG
Addict
Addict
Posts: 3322
Joined: Thu Apr 18, 2019 8:17 am

Re: Disable third-party window movement?

Post by BarryG »

Rashad, I already have the hWnd to the UWP window, so your example is really just doing this:

Code: Select all

Repeat
  Delay(100)
  MoveWindow_(handle,400,400,400,600,1) 
ForEver
Is there no way to stop it being dragged and then snapping back, which makes it "fight" the user?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Disable third-party window movement?

Post by RASHAD »

Hi
Now all you have to do is to prevent the User from resizing the UWP
I did my work ,no move
did not I ? :D
(It maybe there is another solution with the DWM)

Code: Select all

Prototype DwmGetWindowAttribute(hWnd,dwAttribute.l,*pvAttribute,cbAttribute.l)
Global DwmGWA.DwmGetWindowAttribute,hWnd,title${#MAX_PATH},class${#MAX_PATH}
#DWMWA_CLOAKED = 14

Global handle

Procedure GetWinHandle(pro$)
  hWnd = FindWindow_(0, 0)
  Repeat
    hWnd = GetWindow_(hWnd, #GW_HWNDNEXT)
    If hWnd And IsWindowVisible_(hWnd) And GetWindowLongPtr_(hWnd, #GWL_HWNDPARENT) = 0
      GetWindowText_(hWnd, @title$, #MAX_PATH)
      GetClassName_(hWnd,@class$,#MAX_PATH)
      If class$ = "ApplicationFrameWindow" Or class$ = "Windows.UI.Core.CoreWindow"
        dll = OpenLibrary(#PB_Any,"DWMAPI.DLL")
        If dll
          DwmGWA = GetFunction(dll,"DwmGetWindowAttribute")
          If DwmGWA
            DwmGWA(hWnd,#DWMWA_CLOAKED,@Cloaked,SizeOf(Cloaked))
            If Cloaked = 0 And title$ = pro$
              handle = hwnd
              ProcedureReturn handle
            EndIf
          EndIf
          CloseLibrary(dll)
        EndIf
      EndIf
    EndIf
  Until hWnd = 0
  ProcedureReturn #False
EndProcedure


OpenWindow(0,0,0,200,40,"Main Window",#PB_Window_SystemMenu)

RunProgram("calc.exe")
Delay(1000)
GetWinHandle("Calculator")
GetWindowRect_(Handle,r.RECT)
OpenWindow(1,r\left,r\top,r\right-r\top,40,"",#PB_Window_BorderLess)
SetWindowLongPtr_(WindowID(1), #GWL_EXSTYLE,#WS_EX_LAYERED | #WS_EX_TOOLWINDOW | #WS_EX_TOPMOST)
SetLayeredWindowAttributes_(WindowID(1),0, 1, #LWA_ALPHA)
SetWindowPos_(WindowID(1),#HWND_TOPMOST	,-1,-1,-1,-1,#SWP_NOSIZE|#SWP_NOMOVE)	

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
  EndSelect
Until Quit = 1

Last edited by RASHAD on Wed Nov 10, 2021 1:15 am, edited 3 times in total.
Egypt my love
BarryG
Addict
Addict
Posts: 3322
Joined: Thu Apr 18, 2019 8:17 am

Re: Disable third-party window movement?

Post by BarryG »

That's what I'm talking about. Thanks, that works great!

[Edit] Wait... now Calculator can't be closed. Hmm. Will see what I can tweak.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Disable third-party window movement?

Post by RASHAD »

Add ButtonGadget() to the main window

Code: Select all

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0
          SendMessage_(handle, #WM_SYSCOMMAND, #SC_CLOSE, 0)
      EndSelect
      
  EndSelect
Until Quit = 1
Egypt my love
Post Reply