程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 透明控件(WinForm)

C# 透明控件(WinForm)

編輯:C#入門知識

public partial class tspControl : UserControl
    {
        public tspControl()
        {
            InitializeComponent();
            SetStyle(ControlStyles.SupportsTransparentBackColor
              | ControlStyles.UserPaint
              | ControlStyles.AllPaintingInWmPaint
              | ControlStyles.Opaque, true);
            this.BackColor = Color.Transparent;
        }       

        private Image img;
        public Image Image
        {
            get
            {
                return img;
            }
            set
            {
                img = value;
            }
        }

        protected override void OnLocationChanged(EventArgs e)
        {
            //base.OnLocationChanged(e);
            Visible = false;
            Visible = true;
        }

        protected override CreateParams CreateParams
        {
            get
            {
                //return base.CreateParams;
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
                return cp;
            }
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            if (img != null)
            {
                base.OnPaint(pe);
                pe.Graphics.DrawImage(img, 0, 0);
            }
            else
            {

            }
        }
    }

//注:如果是手動改變圖片,最好在刷新一下界面(this.Refresh();),記著圖片要是透明的才能實現透明

    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved