Window drag with PostMessage 0xA1 and WebView

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Tvlao
Posts: 18
Joined: 29 Jun 2023, 03:39

Window drag with PostMessage 0xA1 and WebView

27 Feb 2024, 15:40

This window drag code works. The window is dragged.

Code: Select all

TGui := Gui('+Resize')
TGui.Title := "TGui"
TGui.Show("w300 h100")

OnMessage(0x0201, WM_LBUTTONDOWN)

WM_LBUTTONDOWN(wParam, lParam, msg, hwnd) {
    if wParam = 1{
        PostMessage 0xA1, 2,,, TGui.Hwnd
    }
}
But if you load WebView into a window, then this window can no longer be dragged.

Code: Select all

TGui := Gui('+Resize')
TGui.Title := "TGui"
TGui.Show("w300 h100")
wvc := WebView2.create(TGui.Hwnd)
wv := wvc.CoreWebView2
wv.Navigate('file:///' . A_ScriptDir . '/test.html')

OnMessage(0x0201, WM_LBUTTONDOWN)

WM_LBUTTONDOWN(wParam, lParam, msg, hwnd) {
    if wParam = 1{
        PostMessage 0xA1, 2,,, TGui.Hwnd
    }
}
Tvlao
Posts: 18
Joined: 29 Jun 2023, 03:39

Re: Window drag with PostMessage 0xA1 and WebView

29 Feb 2024, 08:58

This is from here: https://github.com/thqby/ahk2_lib/blob/master/WebView2/WebView2.ahk

Is this a library bug? Or is there no way to move the window when WebView is loaded?
william_ahk
Posts: 502
Joined: 03 Dec 2018, 20:02

Re: Window drag with PostMessage 0xA1 and WebView

01 Mar 2024, 08:01

It's not a bug. Edge WebView2 is a separate window and process glued to the AHK's Gui window. So OnMessage cannot listen for messages from other processes. Fortunately Microsoft has already implemented this. You can use css rules to set a draggable region, it's actually easier than handling barebone window messages.

First enable the feature in the flags:

Code: Select all

wvc := WebView2.create(MyGui.Hwnd,,,,, { AdditionalBrowserArguments: "--enable-features=msWebView2EnableDraggableRegions" })
Then define the regions in the css.

Code: Select all

<style>
    body {
        --app-region: drag;
        -webkit-app-region: drag;
    }
    #app {
        --app-region: no-drag;
        -webkit-app-region: no-drag;
    }
</style>
<div id="app">
    <input type="text" />
</div>

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: felipe93, mikeyww and 28 guests