欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 实现颜色渐变窗体控件详细讲解

程序员文章站 2022-06-24 09:42:56
1.建议设置窗体为双缓冲绘图,可有效避免界面刷时引起的闪烁this.setstyle(controlstyles.allpaintinginwmpaint | controlstyles.optimi...

1.建议设置窗体为双缓冲绘图,可有效避免界面刷时引起的闪烁

this.setstyle(controlstyles.allpaintinginwmpaint | controlstyles.optimizeddoublebuffer, true); 

2、代码实现

 private color color1 = color.gray; //起始颜色
 private color color2 = color.white ; //目标颜色
 private float changeangle = 0f;    //渐变角度

3.窗体绘制函数

 private void form1_paint(object sender, painteventargs e)
 {
      graphics g = e.graphics;
      rectangle grounrect = new rectangle(0, 0, this.width, this.height);
      system.drawing.drawing2d.lineargradientbrush background = new system.drawing.drawing2d.lineargradientbrush(grounrect, color1, color2, changeangle);
      g.fillrectangle(background, grounrect);
      background.dispose();
}

补充:wps中 lineargradientbrush线性渐变的使用

1、颜色列排列

注:

(1)列排列的起始坐标为(0,0.5)终止坐标为(1,0.5)

(2)其中offset放置的位置参数是需要计算的

​ 例如:一共四个颜色,那么就是1/4=0.25;表示一个颜色0.25,第一个颜色为0.25,第二个就是再加上0.25=0.5,第三个就是0.75,第四个就是1

public mainwindow()
    {
      initializecomponent();
  //实例化一个border控件,来设置这个背景线性渐变
      border bord1 = new border();
      bord1.width = bord1.height=200;
      indext.children.add(bord1);
  //线性渐变设置开始
      lineargradientbrush brush = new lineargradientbrush();//实例化线性渐变对象
  //列排列的起始坐标为(0,0.5)终止坐标为(1,0.5)
      brush.startpoint = new point(0, 0.5);//设置线性渐变的二维起始坐标
      brush.endpoint=new point(1,0.5);//设置线性渐变的二维终止坐标
      brush.gradientstops.add(new gradientstop(color: colors.pink,offset:0.25));
  //gradientstops表示设置渐变的终止点
  //gradientstop第一个参数color是设置颜色,第二个参数offset是设置的位置
      brush.gradientstops.add(new gradientstop(color: colors.indianred,offset:0.50));
      brush.gradientstops.add(new gradientstop(color: colors.lightsteelblue,offset:0.75));
      brush.gradientstops.add(new gradientstop(color: colors.lightseagreen,offset:1.0));
      bord1.background = brush;
  //最后将设置好的渐变背景赋值给border控件
    }

2、颜色行排列

注:

行排列的时候,起始位置和终止位置只是改变了位置

列排列的起始坐标为(0.5,0)终止坐标为(0.5,1)

public mainwindow()
    {
      initializecomponent();
      border bord1 = new border();
      bord1.width = bord1.height=200;
      indext.children.add(bord1);
      lineargradientbrush brush = new lineargradientbrush();
   //颜色行排列位置改变
      brush.startpoint = new point(0.5,0);
      brush.endpoint=new point(0.5,1);
      brush.gradientstops.add(new gradientstop(color: colors.pink,offset:0.25));
      brush.gradientstops.add(new gradientstop(color: colors.indianred,offset:0.50));
      brush.gradientstops.add(new gradientstop(color: colors.lightsteelblue,offset:0.75));
      brush.gradientstops.add(new gradientstop(color: colors.lightseagreen,offset:1.0));
      bord1.background = brush;
    }

3、左上角到右下角斜着排列

注:

如果说要斜着排列,那么它的起始位置和终止位置不用设置计算,默认排列,只需要计算offset的位置大小

 public mainwindow()
    {
      initializecomponent();
      border bord1 = new border();
      bord1.width = bord1.height=200;
      indext.children.add(bord1);
      lineargradientbrush brush = new lineargradientbrush();
      brush.gradientstops.add(new gradientstop(color: colors.pink,offset:0.25));
      brush.gradientstops.add(new gradientstop(color: colors.indianred,offset:0.50));
      brush.gradientstops.add(new gradientstop(color: colors.lightsteelblue,offset:0.75));
      brush.gradientstops.add(new gradientstop(color: colors.lightseagreen,offset:1.0));
      bord1.background = brush;
    }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。