Page 1 of 1
Disable third-party window movement?
Posted: Fri Nov 05, 2021 1:47 pm
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)
Re: Disable third-party window movement?
Posted: Sat Nov 06, 2021 12:37 pm
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
Re: Disable third-party window movement?
Posted: Tue Nov 09, 2021 11:45 am
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?
Re: Disable third-party window movement?
Posted: Wed Nov 10, 2021 12:21 am
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 ?
(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
Re: Disable third-party window movement?
Posted: Wed Nov 10, 2021 12:29 am
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.
Re: Disable third-party window movement?
Posted: Wed Nov 10, 2021 1:15 am
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