WPF中webview2滑鼠移動窗體

wgscd發表於2024-07-15

WPF裡webview2會一直處於其他控制元件最上層,是個歷史遺留問題。

為了能在webview2裡滑鼠移動讓窗體跟著移動位置程式碼如下:

  async Task InitializeAsync()
       {
            AppLog.AddLog("InitializeAsync...........");
 try
            {
                CoreWebView2EnvironmentOptions opts = new CoreWebView2EnvironmentOptions() { AdditionalBrowserArguments = "--autoplay-policy=user-gesture-required" };
                var env = await CoreWebView2Environment.CreateAsync(userDataFolder: System.IO.Path.Combine(System.IO.Path.GetTempPath(), "wgscd_Browser"), options: opts);
                await webView.EnsureCoreWebView2Async(env);

                // webView.CoreWebView2.AddWebResourceRequestedFilter(null, CoreWebView2WebResourceContext.Websocket);

                // webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
                //https://weblog.west-wind.com/posts/2021/Jan/14/Taking-the-new-Chromium-WebView2-Control-for-a-Spin-in-NET-Part-1

              //  webView.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
              //  webView.ContextMenuOpening += WebView_ContextMenuOpening;
                webView.CoreWebView2.Settings.AreHostObjectsAllowed = true;
                webView.CoreWebView2.Settings.IsWebMessageEnabled = true;
                webView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived;

                webView.DefaultBackgroundColor = System.Drawing.Color.FromArgb(0, 0, 0, 0);
               //webView.Source = new System.Uri("E:\\mm\\2.html");

                AppLog.AddLog("InitializeAsync...compete.");
            }

            catch (Exception ex)
            {
                MessageBox.Show("init webview err:\r\n" + ex.Message);

            }

        }




  private void CoreWebView2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
        {
            var message = e.TryGetWebMessageAsString();
            try
            {
                if (message.StartsWith("DragMove:"))
                {
                    // 解析訊息以獲取需要移動的距離或其他資訊  
                    // 這裡只是一個示例,你可能需要解析更復雜的訊息格式  
                    var xy = message.Substring("DragMove:".Length).Split(',');
                    var x = int.Parse(xy[0]);
                    var y = int.Parse(xy[1]);
                    Debug.Print("" + x+","+y);
                    Title = "" + "" + x + "," + y;
                    Left = Left + x;
                    Top = Top + y;

                }
            }
            catch { }

            /*
             page js:
            <script>  
let startX, startY, distanceX, distanceY;
document.addEventListener('mousedown', function(e) {
  startX = e.pageX;
  startY = e.pageY;
  document.addEventListener('mousemove', mouseMoveHandler);
  document.addEventListener('mouseup', mouseUpHandler);
});
 
function mouseMoveHandler(e) {
  distanceX = e.pageX - startX;
  distanceY = e.pageY - startY; 
  window.chrome.webview.postMessage("DragMove:"+distanceX+","+distanceY); 
  console.log(`Dragged ${distanceX} horizontally and ${distanceY} vertically.`);
}
 
function mouseUpHandler(e) {
  document.removeEventListener('mousemove', mouseMoveHandler);
  document.removeEventListener('mouseup', mouseUpHandler);
}
</script>  
             
    */



        }

  

相關文章