首页 > 其他分享 >WPF中webview2鼠标移动窗体

WPF中webview2鼠标移动窗体

时间:2024-07-15 12:18:40浏览次数:6  
标签:CoreWebView2 distanceX webview2 窗体 var WPF document webView

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>  
             
    */



        }

  

标签:CoreWebView2,distanceX,webview2,窗体,var,WPF,document,webView
From: https://www.cnblogs.com/wgscd/p/18302927

相关文章

  • Simple WPF: WPF使用Windows API发送Toast通知
    最新内容优先发布于个人博客:小虎技术分享站,随后逐步搬运到博客园。创作不易,如果觉得有用请在Github上为博主点亮一颗小星星吧!以前看到Windows10的气泡通知觉得很有意思,但是一直不知道该如何实现。最近一次上网冲浪过程中偶然的机会看到了相关资料就自己来试试。本文介绍了在WPF......
  • WPF 滚动轮播文字(走马灯效果)
     XAML调用示例:<pp:RunningTextGrid.Row="2"Grid.Column="1"Padding="126"Space="0"Speed="120"FontSize="12"Direction="LeftToRight"Background="#5D6B99"Foreground="#......
  • Wpf使用Prism的IRegionManager实现页面导航
    Wpf使用Prism的IRegionManager实现页面导航背景之前使用winform的形式,利用事件和委托(复杂可以利用反射)实现了wpf的页面跳转。页面间的导航可以通过使用Prism类库实现,本章节主要讲述这个。参考内容文字讲解因为有人做了更好的讲解,所以直接将索引放在下方。但单凭讲解没有完......
  • WPF generate rows and columns via C# dynamically
    //xaml<Windowx:Class="WpfApp214.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF Canvas ZoomIn ZoomOut via set Background="Transparent"
    <CanvasGrid.Column="1"Background="Transparent"x:Name="cvs"ClipToBounds="True"MouseWheel="cvs_MouseWheel"MouseDown="cvs_MouseDown"MouseUp="cvs_MouseUp"MouseMove="cvs_......
  • WPF canvas locate
    //xaml<Windowx:Class="WpfApp210.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • 在WPF中使用WriteableBitmap对接工业相机及常用操作
    写作背景写这篇文章主要是因为工业相机(海康、大恒等)提供的.NET开发文档和示例程序都是用WinForm项目来说明举例的,而在WPF项目中对图像的使用和处理与在WinForm项目中有很大不同。在WinForm中用System.Drawing.Bitmap来处理图像,而在WPF中是用System.Windows.Media.Imaging.Writeab......
  • WPF中style的应用(小白快速上手)
    1.解释说明    -通过设置style资源词典可以批量设置控件,不仅节省大量时间,还能方便统一修改    -重复利用Border这个控件,可以自由设计新的控件风格    -这里要注意,虽然style也是写在xaml文件中,但是其文件类型为资源词典类型,这里程序示例也进行不......
  • 优质WPF免费学习资源分享(含代码)
    WPF自学资源分享背景自身是winform开发,winform岗位比较少。wpf和winfom殊途同归,所以自身最近也在学习wpf。分享一下自己寻找到的wpf学习资源,希望对大家有帮助。学习资源推荐书籍学习资源《深入简出WPF》-刘铁猛作者是微软的高级开发工程师,多年的wpf开发经验。不过作者......
  • AutoCAD VBA 模态窗体焦点处理
    引用的win32apiOptionExplicitDeclarePtrSafeFunctionSetParentLib"user32"(ByValhWndChildAsLong,ByValhWndNewParentAsLong)AsLongDeclarePtrSafeFunctionCoCreateGuidLib"ole32.dll"(GAsGUID)AsLongDeclarePtrSafeFunct......