Search code examples
c#winformswebviewwebview2

C# WebView2 Transparency Issues


I'm using WebView2 on C# Winforms, and i'm experiencing weird transparency issues that i cannot seem to fix for several hours see here: issue

I want it to be completely see through so you can see my game as the background, it's supposed to be a overlay.

my code, yes i know it's messy but the issue is still there. Can't seem to find the issue. I have tried trasparencyKey and so on and DefaultBackgroundColor and still can't fix it.

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;
            
        }









       
    }
}

Here's my html code:

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>

Solution

  • Windows transparency is a bit finnicky. The way I've discovered achieving this is by setting the TransparencyKey of the form the WebView2 is on to White(just pure, plain, white[rgb: 255,255,255 ]) and then setting the background color of the Form to White(this allows clicks to go through any pixel that is pure white), and then for things I want to appear white; just make them like one color-bit off-white(254,254,254) for example. And then with your WebView2 HTML page there; make the background-color style of the body or wherever the transparency is needed to pure white as well and it should work.