You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WinForms WebView2透明效果异常问题求助

WinForms WebView2控件透明效果异常求助

在C# WinForms项目中使用WebView2控件时,遇到了透明效果异常的问题,已花费数小时尝试解决仍未成功。我需要将控件设置为完全透明,作为游戏画面的覆盖层。目前尝试过TransparencyKey、DefaultBackgroundColor等方法,但问题依然存在,控件存在非预期的不透明区域。

以下是项目中的C#代码与HTML代码:

C#代码

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) =>
            {
                // 检查是否按下鼠标左键
                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代码

<!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>

内容的提问来源于stack exchange,提问作者Storm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 12:44:55