创建非矩形窗体

Posted on 2018-12-12 23:23  努力成长静待花开  阅读(114)  评论(0编辑  收藏  举报

实现效果:

  

 知识运用:

  通过重写窗体的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));   //在窗体上绘制图片
        }