Csharp/C Sharp/2D Graphics/LinearGradientBrush — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:41, 26 мая 2010

Enumerate LinearGradientMode

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
public class MainForm : Form {
    public MainForm() {
        CenterToScreen();
    }
    protected void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Rectangle r = new Rectangle(10, 10, 100, 100);
        LinearGradientBrush theBrush = null;
        int yOffSet = 10;
        Array obj = Enum.GetValues(typeof(LinearGradientMode));
        for (int x = 0; x < obj.Length; x++) {
            LinearGradientMode temp = (LinearGradientMode)obj.GetValue(x);
            theBrush = new LinearGradientBrush(r, Color.GreenYellow,
                                               Color.Blue, temp);
            g.DrawString(temp.ToString(), new Font("Times New Roman", 10),
                         new SolidBrush(Color.Black), 0, yOffSet);
            g.FillRectangle(theBrush, 150, yOffSet, 200, 50);
            yOffSet += 80;
        }
    }
}


LinearGradientMode.Horizontal, Vertical, BackwardDiagonal, ForwardDiagonal

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;  
public class Form1 : System.Windows.Forms.Form {
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Font f = new Font(new FontFamily("Times New Roman"), 10);
        Brush fb = new SolidBrush(Color.Black);
        LinearGradientBrush lGB;  
        Color cR = Color.Red, cW = Color.White;
        int w = 100, h = 70;
        g.DrawString("Horizontal", f, fb, 10, 5);
        Rectangle rec = new Rectangle(10, 20, w, h);
        LinearGradientMode lGM = LinearGradientMode.Horizontal;
        lGB = new LinearGradientBrush(rec, cR, cW, lGM);
        g.FillRectangle(lGB, rec);
        g.DrawString("Vertical", f, fb, w + 20, 5);
        rec.Offset(w + 10, 0);
        lGM = LinearGradientMode.Vertical;
        lGB = new LinearGradientBrush(rec, cR, cW, lGM);
        g.FillRectangle(lGB, rec);
        g.DrawString("ForwardDiagonal", f, fb, 10, h + 25);
        rec.Offset(-w - 10, h + 20);
        lGM = LinearGradientMode.ForwardDiagonal;
        lGB = new LinearGradientBrush(rec, cR, cW, lGM);
        g.FillRectangle(lGB, rec);
        g.DrawString("BackwardDiagonal", f, fb, w + 20, h + 25);
        rec.Offset(w + 10, 0);
        lGM = LinearGradientMode.BackwardDiagonal;
        lGB = new LinearGradientBrush(rec, cR, cW, lGM);
        g.FillRectangle(lGB, rec);
        fb.Dispose();
        g.Dispose();
    }
}


Rectangle Linear-Gradient Brush

 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class RectangleLinearGradientBrush: Form
{
     MenuItem miChecked;
   
     public static void Main()
     {
          Application.Run(new RectangleLinearGradientBrush());
     } 
     public RectangleLinearGradientBrush()
     {
          ResizeRedraw = true; 
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&Gradient-Mode");
   
          foreach (LinearGradientMode gm in 
                              Enum.GetValues(typeof(LinearGradientMode)))
          {
               MenuItem mi = new MenuItem();
               mi.Text     = gm.ToString(); 
               mi.Click   += new EventHandler(MenuGradientModeOnClick);
               Menu.MenuItems[0].MenuItems.Add(mi);
          }
          miChecked = Menu.MenuItems[0].MenuItems[0];
          miChecked.Checked = true;
     }
     void MenuGradientModeOnClick(object obj, EventArgs ea)
     {
          miChecked.Checked = false;
          miChecked = (MenuItem) obj;
          miChecked.Checked = true;
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }        
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Rectangle rectBrush = 
                         new Rectangle(cx / 4, cy / 4, cx / 2, cy / 2);
   
          LinearGradientBrush lgbrush = 
               new LinearGradientBrush(
                         rectBrush, Color.White, Color.Black,
                         (LinearGradientMode) miChecked.Index);
   
         grfx.FillRectangle(lgbrush, 0, 0, cx, cy);
         grfx.DrawRectangle(Pens.Black, rectBrush);
     }
}


Two-Point Linear Gradient Brush

 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class TwoPointLinearGradientBrush: Form
{
     public static void Main()
     {
          Application.Run(new TwoPointLinearGradientBrush());
     }
     TwoPointLinearGradientBrush()
     {
          ResizeRedraw = true; 
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }      
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          LinearGradientBrush lgbrush = 
               new LinearGradientBrush(
                         new Point(cx / 4, cy / 4), 
                         new Point(3 * cx / 4, 3 * cy / 4),
                         Color.White, Color.Black);
   
          grfx.FillRectangle(lgbrush, 0, 0, cx, cy);
     }
}