1. 程式人生 > >建立非矩形窗體

建立非矩形窗體

實現效果:

  

 知識運用:

  通過重寫窗體的OnPaint方法 在其中對窗體進行重繪 並使用透明色將窗體設定為透明來實現

  OnPaint方法: 用來從新繪製窗體影象 

  protected override void OnPaint(PaintEventArgs e) //painteventargs為Paint事件提供資料

  Bitmap類的MakeTransparent方法: 使用指定顏色對點陣圖進行透明  

     public void MakeTransparent(Color transparentColor)  //transparentColor:Color結構 表是使之透明的顏色

  this.BackColor = System.Drawing.SystemColors.Control;

  this.TransparencyKey = System.Drawing.SystemColors.Control;

實現程式碼:  

        private void Form1_Load(object sender, EventArgs e)
        {
            bit = new Bitmap(Properties.Resources.bit);//從指定的影象初始化Bitmap物件
            bit.MakeTransparent(Color.Black);       //使用指定的顏色對Bitmap點陣圖透明
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawImage(bit, new Point(0, 0));   //在窗體上繪製圖片
        }