Csharp/CSharp Tutorial/2D/GraphicsUnit — различия между версиями

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

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

Configure graphics unit

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class GraphicUnitSetting : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  public GraphicUnitSetting()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(408, 273);
    this.Name = "GraphicUnitSetting";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "GDI+ Coordinate";
    this.Resize += new System.EventHandler(this.OnResize);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new GraphicUnitSetting());
  }
  protected void OnPaint (object sender, System.Windows.Forms.PaintEventArgs e)
  {
      GraphicsUnit gUnit = GraphicsUnit.Pixel;
      Point renderingOrgPt = new Point(0,0);
    Graphics g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.PageUnit = gUnit;
    g.TranslateTransform(renderingOrgPt.X,renderingOrgPt.Y);
    g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, 100, 100);
  
    this.Text = string.Format("PageUnit: {0}, Origin: {1}",  gUnit, renderingOrgPt.ToString());
  }
  protected void OnResize (object sender, System.EventArgs e)
  {
    Invalidate();
  }
}

GraphicsUnit.Display

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class WhatSize: Form
{
     public static void Main()
     {
          Application.Run(new WhatSize());
     }
     public WhatSize()
     {
          Text = "What Size?";
          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)
     {
          Brush brush = new SolidBrush(clr);
          int   y     = 0;
   
          DoIt(grfx, brush, ref y, GraphicsUnit.Display);
     }
     void DoIt(Graphics grfx, Brush brush, ref int y, GraphicsUnit gu)
     {
          GraphicsState gs = grfx.Save();
   
          grfx.PageUnit  = gu;
          grfx.PageScale = 1;
   
          SizeF sizef = grfx.VisibleClipBounds.Size;
   
          grfx.Restore(gs);
   
          grfx.DrawString(gu+ ": " + sizef, Font, brush, 0, y);
          y += (int) Math.Ceiling(Font.GetHeight(grfx));
     }
}

GraphicsUnit: Display, Millimeter, Inch, Pixel

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class GraphicUnitDisplay : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  public GraphicUnitDisplay()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(408, 273);
    this.Name = "GraphicUnitDisplay";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "GDI+ Coordinate";
    this.Resize += new System.EventHandler(this.OnResize);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new GraphicUnitDisplay());
  }
  protected void OnPaint (object sender, System.Windows.Forms.PaintEventArgs e)
  {
      GraphicsUnit gUnit = GraphicsUnit.Pixel;
      Point renderingOrgPt = new Point(0,0);
    Graphics g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    
    gUnit = GraphicsUnit.Display;
/*
    
    gUnit = GraphicsUnit.Millimeter;
    gUnit = GraphicsUnit.Inch;
    gUnit = GraphicsUnit.Pixel;
*/
    
    g.PageUnit = gUnit;
    g.TranslateTransform(renderingOrgPt.X,renderingOrgPt.Y);
    g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, 100, 100);
  
    this.Text = string.Format("PageUnit: {0}, Origin: {1}",  gUnit, renderingOrgPt.ToString());
  }
  protected void OnResize (object sender, System.EventArgs e)
  {
    Invalidate();
  }
}

GraphicsUnit Document

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class GraphicUnitDocument : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  public GraphicUnitDocument()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(408, 273);
    this.Name = "GraphicUnitDocument";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "GDI+ Coordinate";
    this.Resize += new System.EventHandler(this.OnResize);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new GraphicUnitDocument());
  }
  protected void OnPaint (object sender, System.Windows.Forms.PaintEventArgs e)
  {
      GraphicsUnit gUnit = GraphicsUnit.Pixel;
      Point renderingOrgPt = new Point(0,0);
    Graphics g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    
    gUnit = GraphicsUnit.Document;
    
    g.PageUnit = gUnit;
    g.TranslateTransform(renderingOrgPt.X,renderingOrgPt.Y);
    g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, 100, 100);
  
    this.Text = string.Format("PageUnit: {0}, Origin: {1}",  gUnit, renderingOrgPt.ToString());
  }
  protected void OnResize (object sender, System.EventArgs e)
  {
    Invalidate();
  }
}

GraphicsUnit.Inch

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class WhatSize: Form
{
     public static void Main()
     {
          Application.Run(new WhatSize());
     }
     public WhatSize()
     {
          Text = "What Size?";
          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)
     {
          Brush brush = new SolidBrush(clr);
          int   y     = 0;
   
          DoIt(grfx, brush, ref y, GraphicsUnit.Inch);
     }
     void DoIt(Graphics grfx, Brush brush, ref int y, GraphicsUnit gu)
     {
          GraphicsState gs = grfx.Save();
   
          grfx.PageUnit  = gu;
          grfx.PageScale = 1;
   
          SizeF sizef = grfx.VisibleClipBounds.Size;
   
          grfx.Restore(gs);
   
          grfx.DrawString(gu+ ": " + sizef, Font, brush, 0, y);
          y += (int) Math.Ceiling(Font.GetHeight(grfx));
     }
}

GraphicsUnit.Millimeter

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class WhatSize: Form
{
     public static void Main()
     {
          Application.Run(new WhatSize());
     }
     public WhatSize()
     {
          Text = "What Size?";
          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)
     {
          Brush brush = new SolidBrush(clr);
          int   y     = 0;
   
          DoIt(grfx, brush, ref y, GraphicsUnit.Millimeter);
     }
     void DoIt(Graphics grfx, Brush brush, ref int y, GraphicsUnit gu)
     {
          GraphicsState gs = grfx.Save();
   
          grfx.PageUnit  = gu;
          grfx.PageScale = 1;
   
          SizeF sizef = grfx.VisibleClipBounds.Size;
   
          grfx.Restore(gs);
   
          grfx.DrawString(gu+ ": " + sizef, Font, brush, 0, y);
          y += (int) Math.Ceiling(Font.GetHeight(grfx));
     }
}

GraphicsUnit Point

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class GraphicUnitPoint : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  public GraphicUnitPoint()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(408, 273);
    this.Name = "GraphicUnitPoint";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "GDI+ Coordinate";
    this.Resize += new System.EventHandler(this.OnResize);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new GraphicUnitPoint());
  }
  protected void OnPaint (object sender, System.Windows.Forms.PaintEventArgs e)
  {
      GraphicsUnit gUnit = GraphicsUnit.Pixel;
      Point renderingOrgPt = new Point(0,0);
    Graphics g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    
    gUnit = GraphicsUnit.Point;
    
    g.PageUnit = gUnit;
    g.TranslateTransform(renderingOrgPt.X,renderingOrgPt.Y);
    g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, 100, 100);
  
    this.Text = string.Format("PageUnit: {0}, Origin: {1}",  gUnit, renderingOrgPt.ToString());
  }
  protected void OnResize (object sender, System.EventArgs e)
  {
    Invalidate();
  }
}