C# WINFORM 控件 透明控件
本帖最后由 bai123love 于 2012-11-01 21:47:50 编辑
            using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace WindowsFormsApplication1
{
    public class ControlExample : Control
    {
        private const int Alpha = 111;
        public ControlExample()
        {
            this.SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
            base.CreateControl();
        }
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                //开启 WS_EX_TRANSPARENT, 使控件支持透明
                cp.ExStyle |= 0x00000020;
                return cp;
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            float width;
            float height;
            Pen srcPen;
            SolidBrush srcBrush;
            Color srcColor = Color.FromArgb(Alpha, this.BackColor);
            srcPen = new Pen(srcColor, 0);
            srcBrush = new SolidBrush(srcColor);
            base.OnPaint(e);
            width = this.Size.Width;
            height = this.Size.Height;
            e.Graphics.DrawRectangle(srcPen, 0, 0, width, height);
            e.Graphics.FillRectangle(srcBrush, 0, 0, width, height);
            e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), new Rectan