Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background doesn't Transparent after Close OffScreen Browser DevTools. #2264

Closed
lalafellsleep opened this issue Feb 5, 2018 · 2 comments
Closed
Labels
upstream These issues require fixing in the Chromium Embedded Framework(CEF) or Chromium.
Milestone

Comments

@lalafellsleep
Copy link

lalafellsleep commented Feb 5, 2018

  • What version of the product are you using?
    63.0.0-pre03 (Nuget)
    63.0.0-pre01 (Nuget)

  • What architecture x86 or x64?
    x86, x64

  • On what operating system?
    Windows 10

  • Are you using WinForms, WPF or OffScreen?
    OffScreen

  • What steps will reproduce the problem?
    I had unknown transparency bug. running application on offscreen, show devtools and offscreen browser has background color after close devtools.

    Remote Debug (DevTool) same result

  • What is the expected output? What do you see instead?
    I think continued transparent background. but closing DevTool, background set forced #FFF.

  • Please provide any additional information below.
    I run This Code

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace OverlayTest
{
    public partial class Form1 : Form
    {
        private CefSharp.OffScreen.ChromiumWebBrowser browser;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            browser = new CefSharp.OffScreen.ChromiumWebBrowser("https://amethyst.ffxiv.io/unstable/");
            browser.OnPaint += Browser_OnPaint;
            browser.BrowserInitialized += Browser_BrowserInitialized;
            browser.Size = Size;
            rect();
        }
        private void Browser_BrowserInitialized(object sender, EventArgs e)
        {
            browser.GetBrowser().GetHost().ShowDevTools();
        }
        private void Browser_OnPaint(object sender, CefSharp.OnPaintEventArgs e)
        {
            var b = (CefSharp.OffScreen.ChromiumWebBrowser)sender;
            if(b.Bitmap != null)
            {
                var bmp = (Bitmap)b.Bitmap.Clone();
                SetBitmap(bmp, this);
            }
        }
        private void rect()
        {
            var rect = new Rectangle(0, 0, Width, Height);
            var bmp = new Bitmap(Width, Height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Color.Transparent);
                g.DrawRectangle(new Pen(Brushes.White, 2f), new Rectangle(1, 1, Width - 2, Height - 2));
            }
            SetBitmap(bmp, this);
        }
        protected override CreateParams CreateParams
        {
            get
            {
                var style = base.CreateParams;
                style.ClassStyle |= 200; // NoCloseBtn
                style.ExStyle |= 0x8; // TopMost
                style.ExStyle |= 0x80000; // Layered
                style.ExStyle |= 0x8000000; // NoActive
                return style;
            }
        }
        public static void SetBitmap(Bitmap bitmap, Form frm)
        {
            IntPtr screenDc = NativeMethods.GetDC(IntPtr.Zero);
            IntPtr compatibleMemoryDc = NativeMethods.CreateCompatibleDC(screenDc);
            IntPtr hgdiBitmap = IntPtr.Zero;
            IntPtr hgdiOldBitmap = IntPtr.Zero;

            try
            {
                try
                { hgdiBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); } catch { return; }
                hgdiOldBitmap = NativeMethods.SelectObject(compatibleMemoryDc, hgdiBitmap);
                NativeMethods.SizeStruct size = new NativeMethods.SizeStruct()
                {
                    X = bitmap.Width,
                    Y = bitmap.Height
                };
                NativeMethods.PointStruct sourcePoint = new NativeMethods.PointStruct();
                NativeMethods.PointStruct topPoint = new NativeMethods.PointStruct()
                {
                    X = frm.Left,
                    Y = frm.Top
                };
                NativeMethods.BlendFunctionStruct blend = new NativeMethods.BlendFunctionStruct()
                {
                    BlendOp = 255 /* opacity */,
                    BlendFlags = 0x00 /* AC_SRC_OVER */,
                    AlphaFormat = 0x01 /* AC_SRC_ALPHA */,
                    SourceConstantAlpha = byte.MaxValue
                };
                frm.Invoke((MethodInvoker)delegate
                {
                    NativeMethods.UpdateLayeredWindow(frm.Handle, screenDc, ref topPoint, ref size, compatibleMemoryDc, ref sourcePoint, 0, ref blend, 2 /* ULW_ALPHA */);
                });
            }
            finally
            {
                if (screenDc != IntPtr.Zero)
                {
                    NativeMethods.ReleaseDC(IntPtr.Zero, screenDc);
                }
                if (hgdiBitmap != IntPtr.Zero)
                {
                    NativeMethods.SelectObject(compatibleMemoryDc, hgdiOldBitmap);
                    NativeMethods.DeleteObject(hgdiBitmap);
                }
                NativeMethods.DeleteDC(compatibleMemoryDc);
            }
        }
    }
    internal class NativeMethods
    {
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
        [DllImport("user32.dll", SetLastError = true)]
        public extern static bool UpdateLayeredWindow(IntPtr handle, IntPtr hdcDst, ref PointStruct pptDst, ref SizeStruct pSize, IntPtr hDc, ref PointStruct pptSrc, int crKey, ref BlendFunctionStruct pBlend, int dwFlags);
        [DllImport("user32.dll", ExactSpelling = false, SetLastError = true)]
        public extern static IntPtr SetWindowLong(IntPtr handle, IntPtr index, IntPtr dwNewLong);
        [DllImport("user32.dll", ExactSpelling = false, SetLastError = true)]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll", SetLastError = true)]
        public extern static IntPtr GetDC(IntPtr handle);
        [DllImport("user32.dll", SetLastError = false)]
        public extern static IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
        [DllImport("gdi32.dll", SetLastError = true)]
        public extern static IntPtr CreateCompatibleDC(IntPtr hDc);
        [DllImport("gdi32.dll", SetLastError = true)]
        public extern static bool DeleteDC(IntPtr hDc);
        [DllImport("gdi32.dll", SetLastError = false)]
        public extern static IntPtr SelectObject(IntPtr hDc, IntPtr hgdiObject);
        [DllImport("gdi32.dll", SetLastError = true)]
        public extern static bool DeleteObject(IntPtr hgdiObject);
        public struct PointStruct { public int X; public int Y; }
        public struct SizeStruct { public int X; public int Y; }
        public struct BlendFunctionStruct { public byte BlendOp; public byte BlendFlags; public byte SourceConstantAlpha; public byte AlphaFormat; }
    }
}

Ok, this code is successful running. but... see below image.

img

Same Bug occur this application: Aliapoh.Overlay
how can i fix this? i think this problem not on c#

@lalafellsleep lalafellsleep changed the title background doesn't Transparent after Close OffScreen Browser DevTools. Background doesn't Transparent after Close OffScreen Browser DevTools. Feb 5, 2018
@amaitland amaitland added the upstream These issues require fixing in the Chromium Embedded Framework(CEF) or Chromium. label Feb 5, 2018
@amaitland
Copy link
Member

Thanks for the detailed bug report, the problem has already been reported upstream see https://bitbucket.org/chromiumembedded/cef/issues/2345/osr-detaching-closing-devtools-resets

Needs to be fixed in CEF.

@amaitland amaitland added this to the 64.0.0 milestone Mar 3, 2018
@amaitland
Copy link
Member

master has been upgraded and should include the fix for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream These issues require fixing in the Chromium Embedded Framework(CEF) or Chromium.
Projects
None yet
Development

No branches or pull requests

2 participants