C# WebView2 透明度问题

问题描述 投票:0回答:1

我在 C# Winforms 上使用 WebView2,并且遇到了奇怪的透明度问题,我似乎几个小时都无法修复,请参阅此处:问题

我希望它完全透明,这样你就可以看到我的游戏作为背景,它应该是一个覆盖层。

我的代码,是的,我知道它很混乱,但问题仍然存在。似乎找不到问题所在。我已经尝试了trasparencyKey等和DefaultBackgroundColor,但仍然无法修复它。

using Microsoft.Web.WebView2.Core;
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Input;

namespace nightset
{
    public partial class keystrokes : Form
    {
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImport("User32.dll")]
        public static extern bool ReleaseCapture();

        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        public keystrokes()
        {
            InitializeComponent();


        }





        public static void MakeDraggable(Control form)
        {
            form.MouseDown += (sender, e) =>
            {
                // Check if the left mouse button is pressed
                if (e.Button == MouseButtons.Left)
                {
                    ReleaseCapture();
                    SendMessage(form.Parent.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
                }
            };
        }

        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);

        public static void ApplyRoundedCorners(Control form, int radius)
        {
           

            IntPtr region = CreateRoundRectRgn(0, 0, form.Width, form.Height, radius * 2, radius * 2);
            SetWindowRgn(form.Handle, region, true);
        }


        private void webView21_Click(object sender, EventArgs e)
        {

        }

       

        private void loginpage_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
        {
        }

        private void loginpage_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
        {
        }

        private async void keystrokes_Load_1(object sender, EventArgs e)
        {
            this.AllowTransparency = true;
            this.BackColor = Color.Lime;
            this.TransparencyKey = Color.Lime;
            await loginpage.EnsureCoreWebView2Async(null);
            ApplyRoundedCorners(loginpage, 5);
            ApplyRoundedCorners(this, 5);
            MakeDraggable(this.panel1);
            MakeDraggable(this);
            
            string baseDirectory = AppDomain.CurrentDomain.BaseDirectory + "\\dependencies\\html\\";
            string htmlFilePath = Path.Combine(baseDirectory, "keystrokes.html");
            loginpage.Source = new Uri(htmlFilePath);
            loginpage.CoreWebView2InitializationCompleted += loginpage_CoreWebView2InitializationCompleted;
            loginpage.WebMessageReceived += loginpage_WebMessageReceived;
            
        }









       
    }
}

这是我的html代码:

html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nightset</title>
    <style>
      body, html {
        margin: 0;
        padding: 0;
        font-family: 'Roboto', sans-serif;
        overflow: hidden;
      }

      .container {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        display: grid;
        grid-template-areas: 
          ". W ."
          "A S D";
        gap: 20px; 
        text-align: center;
        color: #fff;
        font-size: 20px;
        text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
      }

      .box {
        background: rgba(207, 80, 201, 0.1); 
        padding: 10px;
        border-radius: 10px;
        box-shadow: 0 0 20px rgba(207, 80, 201, 0.3);
        backdrop-filter: blur(10px);
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        font-size: 18px;
      }

      .box.W { grid-area: W; }
      .box.A { grid-area: A; }
      .box.S { grid-area: S; }
      .box.D { grid-area: D; }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="box W">W</div>
      <div class="box A">A</div>
      <div class="box S">S</div>
      <div class="box D">D</div>
    </div>
  </body>
</html>
c# winforms webview webview2
1个回答
0
投票

Windows 透明度有点挑剔。我发现实现此目的的方法是将 WebView2 所在表单的 TransparencyKey 设置为白色(只是纯、普通、白色 [rgb: 255,255,255 ]),然后将表单的背景颜色设置为白色(这允许单击穿过任何纯白色的像素),然后对于我想要出现白色的东西;例如,只需将它们设置为一种颜色位灰白色(254,254,254)即可。然后你的 WebView2 HTML 页面就在那里;将主体的背景颜色样式或需要透明度的地方也设置为纯白色,它应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.