0

Hii everyone I am developing a window application in which I want to show a web page over Layered window containing .PNG in c#, I am able to show two layered window with image over one of them as shown in screen shot enter image description here

But i want to render web page over layered window . If someone have any solution regarding this will be really appreciated. MainForm.cs

#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CSWinFormLayeredWindow.Properties;
#endregion


namespace CSWinFormLayeredWindow
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        PerPixelAlphaForm alphaWindow = null;
        PerPixelAlphaForm alphaWindow1 = null;

        private void btnShowAlphaWindow_Click(object sender, EventArgs e)
        {
            if (!OSFeature.Feature.IsPresent(OSFeature.LayeredWindows))
            {
                MessageBox.Show("Layered window is not supported in the current system");
                return;
            }

            if (alphaWindow == null || alphaWindow1== null)
            {
                alphaWindow = new PerPixelAlphaForm();
                alphaWindow1 = new PerPixelAlphaForm();
            }

            alphaWindow.Show();
            alphaWindow.SelectBitmap(Resources.Ring, this.trackBarOpacity.Value);

        }


        private void trackBarOpacity_ValueChanged(object sender, EventArgs e)
        {
            this.lbOpacity.Text = this.trackBarOpacity.Value.ToString();

            if (alphaWindow != null)
            {
                alphaWindow.SelectBitmap(Resources.Rin, this.trackBarOpacity.Value);

            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

            if (!OSFeature.Feature.IsPresent(OSFeature.LayeredWindows))
            {
                MessageBox.Show("Layered window is not supported in the current system");
                return;
            }

            if (alphaWindow == null || alphaWindow1==null)
            {
                alphaWindow = new PerPixelAlphaForm();
                alphaWindow1 = new PerPixelAlphaForm();
            }

            alphaWindow.Show();

            alphaWindow.SelectBitmap(Resources.Rin, this.trackBarOpacity.Value);
            alphaWindow1.Show();
            alphaWindow1.SelectBitmap(Resources.Ring, this.trackBarOpacity.Value);


        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!OSFeature.Feature.IsPresent(OSFeature.LayeredWindows))
            {
                MessageBox.Show("Layered window is not supported in the current system");
                return;
            }

            if (alphaWindow == null)
            {
                alphaWindow = new PerPixelAlphaForm();
            }

            alphaWindow.Show();
            alphaWindow.SelectBitmap(Resources.Ring, this.trackBarOpacity.Value);       
        }
    }
}

PerPixelFormAlpha.cs

#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
#endregion


namespace CSWinFormLayeredWindow
{
    public partial class PerPixelAlphaForm : Form
    {
        public PerPixelAlphaForm()
        {
            InitializeComponent();
        }


        protected override CreateParams CreateParams
        {
            get
            {
                // Add the layered extended style (WS_EX_LAYERED) to this window.
                CreateParams createParams = base.CreateParams;
                createParams.ExStyle |= WS_EX_LAYERED;
                return createParams;
            }
        }


        /// <summary>
        /// Let Windows drag this window for us (thinks its hitting the title 
        /// bar of the window)
        /// </summary>
        /// <param name="message"></param>
        protected override void WndProc(ref Message message)
        {
            if (message.Msg == WM_NCHITTEST)
            {
                // Tell Windows that the user is on the title bar (caption)
                message.Result = (IntPtr)HTCAPTION;
            }
            else
            {
                base.WndProc(ref message);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="bitmap"></param>
        public void SelectBitmap(Bitmap bitmap)
        {
            SelectBitmap(bitmap, 255);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="bitmap">
        /// 
        /// </param>
        /// <param name="opacity">
        /// Specifies an alpha transparency value to be used on the entire source 
        /// bitmap. The SourceConstantAlpha value is combined with any per-pixel 
        /// alpha values in the source bitmap. The value ranges from 0 to 255. If 
        /// you set SourceConstantAlpha to 0, it is assumed that your image is 
        /// transparent. When you only want to use per-pixel alpha values, set 
        /// the SourceConstantAlpha value to 255 (opaque).
        /// </param>
        public void SelectBitmap(Bitmap bitmap, int opacity)
        {
            // Does this bitmap contain an alpha channel?
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
            }

            // Get device contexts
            IntPtr screenDc = GetDC(IntPtr.Zero);
            IntPtr memDc = CreateCompatibleDC(screenDc);
            IntPtr hBitmap = IntPtr.Zero;
            IntPtr hOldBitmap = IntPtr.Zero;

            try
            {
                // Get handle to the new bitmap and select it into the current 
                // device context.
                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                hOldBitmap = SelectObject(memDc, hBitmap);

                // Set parameters for layered window update.
                Size newSize = new Size(bitmap.Width, bitmap.Height);
                Point sourceLocation = new Point(0, 0);
                Point newLocation = new Point(this.Left, this.Top);
                BLENDFUNCTION blend = new BLENDFUNCTION();
                blend.BlendOp = AC_SRC_OVER;
                blend.BlendFlags = 0;
                blend.SourceConstantAlpha = (byte)opacity;
                blend.AlphaFormat = AC_SRC_ALPHA;

                // Update the window.
                UpdateLayeredWindow(
                    this.Handle,     // Handle to the layered window
                    screenDc,        // Handle to the screen DC
                    ref newLocation, // New screen position of the layered window
                    ref newSize,     // New size of the layered window
                    memDc,           // Handle to the layered window surface DC
                    ref sourceLocation, // Location of the layer in the DC
                    0,               // Color key of the layered window
                    ref blend,       // Transparency of the layered window
                    ULW_ALPHA        // Use blend as the blend function
                    );
            }
            finally
            {
                // Release device context.
                ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    SelectObject(memDc, hOldBitmap);
                    DeleteObject(hBitmap);
                }
                DeleteDC(memDc);
            }
        }


        #region Native Methods and Structures

        const Int32 WS_EX_LAYERED = 0x80000;
        const Int32 HTCAPTION = 0x02;
        const Int32 WM_NCHITTEST = 0x84;
        const Int32 ULW_ALPHA = 0x02;
        const byte AC_SRC_OVER = 0x00;
        const byte AC_SRC_ALPHA = 0x01;

        [StructLayout(LayoutKind.Sequential)]
        struct Point
        {
            public Int32 x;
            public Int32 y;

            public Point(Int32 x, Int32 y)
            { this.x = x; this.y = y; }
        }

        [StructLayout(LayoutKind.Sequential)]
        struct Size
        {
            public Int32 cx;
            public Int32 cy;

            public Size(Int32 cx, Int32 cy)
            { this.cx = cx; this.cy = cy; }
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        struct ARGB
        {
            public byte Blue;
            public byte Green;
            public byte Red;
            public byte Alpha;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        struct BLENDFUNCTION
        {
            public byte BlendOp;
            public byte BlendFlags;
            public byte SourceConstantAlpha;
            public byte AlphaFormat;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst,
            ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc,
            Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr CreateCompatibleDC(IntPtr hDC);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr GetDC(IntPtr hWnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool DeleteDC(IntPtr hdc);

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

        [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool DeleteObject(IntPtr hObject);

        #endregion

        private void PerPixelAlphaForm_Load(object sender, EventArgs e)
        {

        }
    }
}
Sanjeev Sangral
  • 1,385
  • 2
  • 25
  • 37
  • 1
    To avoid pointless answers about basic properties of the web browser control: what have you tried? – Richard Apr 08 '15 at 08:05
  • @Richard : I have tried using webbrowser contorl on the layered window..It doesnot show any controls..Any technique to render web content /web page on another layered window.. i have been using following links: http://msdn.microsoft.com/en-us/library/ms632599.aspx#layered http://www.codeproject.com/KB/GDI-plus/perpxalpha_sharp.aspx http://www.codeproject.com/kb/gdi/pxalphablend.aspx – Sanjeev Sangral Apr 08 '15 at 08:22
  • I mean: show the code you have tried *in the question* (if this means creating a short sample app then do so (and show you have tried WinForms existing transparency support). – Richard Apr 08 '15 at 08:26
  • @Richard: I have pdated the ques with the code I am using for craeting layered window and creating alpha transaprency.. – Sanjeev Sangral Apr 08 '15 at 08:33
  • So what's wrong with [`Form.Opacity`](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.opacity.aspx)? That does not appear to be a minimal sample of showing a web browser control over an image: I would suggest KISS and have a form with the image and over that a form (which is a control) with opacity set containing the web browser. (I suspect there is a high chance that a browser control does not support opacity, but without definitive documentation start by trying: it might work.) – Richard Apr 08 '15 at 08:47
  • @Richard: Actually my requirement is much like TV STB functionality where we can see TV menu on the top of the video being played in the background which is transparent...and in windows this can only be acheived using layered windows..i hope this would give you a clearer idea.. – Sanjeev Sangral Apr 08 '15 at 09:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74717/discussion-between-sanjeev-sangral-and-richard). – Sanjeev Sangral Apr 08 '15 at 10:01
  • 1. The Opacity function of WinForms is using Layered Windows underneath (look for information from when that feature was added); 2. don't have time for chat (at work :-)) – Richard Apr 08 '15 at 10:24

0 Answers0