Dragging a Popup Window

- BrentDT BrentDT
Originally published in NL #143

Brent shares a demo that shows how to allow the user to drag a popup window. A popup has no titlebar, so it requires a little extra coding to make this possible.

Brent has released this code into the public domain with the following explanation.

This demo works by trapping a left-drag inside the GRAPHICBOX, then tricks the dialog window into believing you are dragging its (non-existent) title bar. The benefit of using this technique (over calling the MoveWindow API inside a similar event handler) is that this automatically takes into account the user's preference for "show window contents while dragging."

Demo

NoMainWin
 
WindowWidth = 400
WindowHeight = 300
UpperLeftX = (DisplayWidth - WindowWidth) / 2
UpperLeftY = (DisplayHeight - WindowHeight) / 2
 
GraphicBox #demo.gfx, 0, 0, WindowWidth, WindowHeight
StyleBits #demo.gfx, 0, _WS_BORDER, 0, 0
 
Open "Demo" For Dialog_Popup As #demo
 
#demo "TrapClose demo.Close"
#demo.gfx "When leftButtonMove demo.gfx.leftButtonMove"
#demo.gfx "Down; Fill ButtonFace; BackColor ButtonFace"
#demo.gfx "\\\ Try dragging me around!"
#demo.gfx "\\ Press Alt+F4 to close."
#demo.gfx "Flush"
 
Wait
 
Sub demo.Close demo$
Close #demo$
End
End Sub
 
Sub demo.gfx.leftButtonMove demo.gfx$, X, Y
demo$ = Word$(demo.gfx$, 1, ".")
 
CallDLL #user32, "ReleaseCapture", _
r As void
 
hWnd = HWnd(#demo$)
CallDLL #user32, "SendMessageA", _
hWnd As ULong, _
_WM_NCLBUTTONDOWN As ULong, _
_HTCAPTION As Long, _
0 As Long, _
r As Long
End Sub