Fix window dragging - use ReleaseCapture + SendMessage for native drag

- webkit-app-region doesn't work in WebView2, use Win API instead
- Bind windowDrag() that calls ReleaseCapture + SendMessage(WM_NCLBUTTONDOWN)
- Title bar onmousedown triggers native window drag behavior
- stopPropagation on window control buttons to prevent drag on click
- Window can now be dragged by clicking anywhere on the title bar

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
taqin
2026-04-12 21:23:22 +07:00
parent 8bf4c2ffdb
commit 1a4430e235
2 changed files with 16 additions and 7 deletions

13
main.go
View File

@@ -28,8 +28,10 @@ var (
procGetWindowLong = user32.NewProc("GetWindowLongW") procGetWindowLong = user32.NewProc("GetWindowLongW")
procSetWindowLong = user32.NewProc("SetWindowLongW") procSetWindowLong = user32.NewProc("SetWindowLongW")
procSetWindowPos = user32.NewProc("SetWindowPos") procSetWindowPos = user32.NewProc("SetWindowPos")
procPostMessage = user32.NewProc("PostMessageW") procPostMessage = user32.NewProc("PostMessageW")
procShowWindow = user32.NewProc("ShowWindow") procShowWindow = user32.NewProc("ShowWindow")
procReleaseCapture = user32.NewProc("ReleaseCapture")
procSendMessage = user32.NewProc("SendMessageW")
) )
const ( const (
@@ -48,6 +50,8 @@ const (
swpNoZOrder = 0x0004 swpNoZOrder = 0x0004
wmSysCommand = 0x0112 wmSysCommand = 0x0112
wmNcLButtonDown = 0x00A1
htCaption = 2
scMinimize = 0xF020 scMinimize = 0xF020
scMaximize = 0xF030 scMaximize = 0xF030
scRestore = 0xF120 scRestore = 0xF120
@@ -125,6 +129,11 @@ func main() {
removeWindowFrame(hwnd) removeWindowFrame(hwnd)
// Bind window control functions for custom title bar buttons // Bind window control functions for custom title bar buttons
// Drag: simulate title bar click to enable native window dragging
w.Bind("windowDrag", func() {
procReleaseCapture.Call()
procSendMessage.Call(hwnd, wmNcLButtonDown, htCaption, 0)
})
w.Bind("windowMinimize", func() { w.Bind("windowMinimize", func() {
procPostMessage.Call(hwnd, wmSysCommand, scMinimize, 0) procPostMessage.Call(hwnd, wmSysCommand, scMinimize, 0)
}) })

View File

@@ -9,8 +9,8 @@
<body> <body>
<div class="app"> <div class="app">
<!-- Custom Title Bar (frameless window) --> <!-- Custom Title Bar (frameless window) -->
<div class="titlebar" id="titlebar"> <div class="titlebar" id="titlebar" onmousedown="windowDrag()">
<div class="titlebar-left"> <div class="titlebar-left" onmousedown="event.stopPropagation()">
<img src="icon-app.png" class="app-logo-img" alt=""> <img src="icon-app.png" class="app-logo-img" alt="">
<div class="app-title"> <div class="app-title">
<span class="app-name">DRIVER BOOSTER</span> <span class="app-name">DRIVER BOOSTER</span>
@@ -24,13 +24,13 @@
</div> </div>
</div> </div>
<div class="titlebar-controls"> <div class="titlebar-controls">
<button class="win-btn win-minimize" onclick="windowMinimize()" title="Minimize"> <button class="win-btn win-minimize" onmousedown="event.stopPropagation()" onclick="windowMinimize()" title="Minimize">
<svg width="10" height="1" viewBox="0 0 10 1"><rect width="10" height="1" fill="currentColor"/></svg> <svg width="10" height="1" viewBox="0 0 10 1"><rect width="10" height="1" fill="currentColor"/></svg>
</button> </button>
<button class="win-btn win-maximize" onclick="windowMaximize()" title="Maximize"> <button class="win-btn win-maximize" onmousedown="event.stopPropagation()" onclick="windowMaximize()" title="Maximize">
<svg width="10" height="10" viewBox="0 0 10 10"><rect x="0.5" y="0.5" width="9" height="9" fill="none" stroke="currentColor" stroke-width="1"/></svg> <svg width="10" height="10" viewBox="0 0 10 10"><rect x="0.5" y="0.5" width="9" height="9" fill="none" stroke="currentColor" stroke-width="1"/></svg>
</button> </button>
<button class="win-btn win-close" onclick="windowClose()" title="Close"> <button class="win-btn win-close" onmousedown="event.stopPropagation()" onclick="windowClose()" title="Close">
<svg width="10" height="10" viewBox="0 0 10 10"><line x1="0" y1="0" x2="10" y2="10" stroke="currentColor" stroke-width="1.2"/><line x1="10" y1="0" x2="0" y2="10" stroke="currentColor" stroke-width="1.2"/></svg> <svg width="10" height="10" viewBox="0 0 10 10"><line x1="0" y1="0" x2="10" y2="10" stroke="currentColor" stroke-width="1.2"/><line x1="10" y1="0" x2="0" y2="10" stroke="currentColor" stroke-width="1.2"/></svg>
</button> </button>
</div> </div>