Transparent form

image_pdfimage_print


   

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Transparensy
{
    /// <summary>
    /// Summary description for Transparensy.
    /// </summary>
    public class Transparensy : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Transparensy()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            
            this.Text = "Transparency";
            //this.Opacity = 0.5;
            //this.TransparencyKey = System.Drawing.Color.Yellow;

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // Transparensy
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(248, 78);
            this.Name = "Transparensy";
            this.Text = "Transparensy";

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Transparensy());
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g;
            g = Graphics.FromHwnd(this.Handle);

            g.FillRectangle(new SolidBrush(Color.Red), 10, 10, 210, 50);
    
            // Five yellow squares with different alpha values
            Rectangle r = new Rectangle(40, 20, 30, 30);
            Color c = Color.FromArgb(255, 255, 255, 0);
            g.FillRectangle(new SolidBrush(c), r);
    
            r.Offset(30, 0);
            c = Color.FromArgb(200, 255, 255, 0);
            g.FillRectangle(new SolidBrush(c), r);
    
            r.Offset(30, 0);
            c = Color.FromArgb(150, 255, 255, 0);
            g.FillRectangle(new SolidBrush(c), r);
    
            r.Offset(30, 0);
            c = Color.FromArgb(100, 255, 255, 0);
            g.FillRectangle(new SolidBrush(c), r);
    
            r.Offset(30, 0);
            c = Color.FromArgb(50, 255, 255, 0);
            g.FillRectangle(new SolidBrush(c), r);

            g.Dispose();        
        }
    }
}