Csharp/C Sharp by API/System.Windows.Forms

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

FontDialog.Apply

<source lang="csharp">

using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;

   public class ColorFontDialog : System.Windows.Forms.Form
   {
       private System.Windows.Forms.Button button1;
       private System.Windows.Forms.Button button2;
       private System.Windows.Forms.Label label1;
       public ColorFontDialog()
       {
           this.Text = "Font and Color Dialogs";
           this.button1.Text = "&Font";
           this.button2.Text = "&Color";
           this.label1.Text = "Change my FONT and COLOR!";
           this.button1 = new System.Windows.Forms.Button();
           this.button2 = new System.Windows.Forms.Button();
           this.label1 = new System.Windows.Forms.Label();
           this.SuspendLayout();
           this.button1.Location = new System.Drawing.Point(24, 8);
           this.button1.Text = "button1";
           this.button1.Click += new System.EventHandler(this.button1_Click_1);
           this.button2.Location = new System.Drawing.Point(120, 8);
           this.button2.Text = "button2";
           this.button2.Click += new System.EventHandler(this.button2_Click);
           this.label1.Location = new System.Drawing.Point(8, 48);
           this.label1.Size = new System.Drawing.Size(280, 48);
           this.label1.TabIndex = 2;
           this.label1.Text = "label1";
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
           this.ClientSize = new System.Drawing.Size(292, 101);
           this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                         this.label1,
                                                                         this.button2,
                                                                         this.button1});
           this.Name = "ColorFontDialog";
           this.Text = "ColorFontDialog";
           this.ResumeLayout(false);
       }
       static void Main() 
       {
           Application.Run(new ColorFontDialog());
       }
       private void button2_Click(object sender, System.EventArgs e)
       {
           ColorDialog cd = new ColorDialog();
           cd.AllowFullOpen = true;   // allow custom colors
           cd.FullOpen = true;   // shows custom colors automatically
           cd.Color = Color.DarkBlue;  // sets the custom color
           if(cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
               this.label1.ForeColor = cd.Color;
       }
       private void button1_Click_1(object sender, System.EventArgs e)
       {
           FontDialog fd = new FontDialog();
           fd.ShowColor = true;
           fd.Color = Color.Blue;
           fd.ShowApply = true;   // ColorDialog does not provide this option!!!
           fd.Apply += new EventHandler(ApplyFont);
           if(fd.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
               ChangeFont(fd);
       }
       private void ApplyFont(object o, EventArgs ea)
       {
           ChangeFont((FontDialog)o);
       }
       private void ChangeFont(FontDialog fd)
       {
           this.label1.Font = fd.Font;
           this.label1.ForeColor = fd.Color;
       }
   }


 </source>


FontDialog.Color

<source lang="csharp">


using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;

   public class ColorFontDialog : System.Windows.Forms.Form
   {
       private System.Windows.Forms.Button button1;
       private System.Windows.Forms.Button button2;
       private System.Windows.Forms.Label label1;
       public ColorFontDialog()
       {
           this.Text = "Font and Color Dialogs";
           this.button1.Text = "&Font";
           this.button2.Text = "&Color";
           this.label1.Text = "Change my FONT and COLOR!";
           this.button1 = new System.Windows.Forms.Button();
           this.button2 = new System.Windows.Forms.Button();
           this.label1 = new System.Windows.Forms.Label();
           this.SuspendLayout();
           this.button1.Location = new System.Drawing.Point(24, 8);
           this.button1.Text = "button1";
           this.button1.Click += new System.EventHandler(this.button1_Click_1);
           this.button2.Location = new System.Drawing.Point(120, 8);
           this.button2.Text = "button2";
           this.button2.Click += new System.EventHandler(this.button2_Click);
           this.label1.Location = new System.Drawing.Point(8, 48);
           this.label1.Size = new System.Drawing.Size(280, 48);
           this.label1.TabIndex = 2;
           this.label1.Text = "label1";
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
           this.ClientSize = new System.Drawing.Size(292, 101);
           this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                         this.label1,
                                                                         this.button2,
                                                                         this.button1});
           this.Name = "ColorFontDialog";
           this.Text = "ColorFontDialog";
           this.ResumeLayout(false);
       }
       static void Main() 
       {
           Application.Run(new ColorFontDialog());
       }
       private void button2_Click(object sender, System.EventArgs e)
       {
           ColorDialog cd = new ColorDialog();
           cd.AllowFullOpen = true;   // allow custom colors
           cd.FullOpen = true;   // shows custom colors automatically
           cd.Color = Color.DarkBlue;  // sets the custom color
           if(cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
               this.label1.ForeColor = cd.Color;
       }
       private void button1_Click_1(object sender, System.EventArgs e)
       {
           FontDialog fd = new FontDialog();
           fd.ShowColor = true;
           fd.Color = Color.Blue;
           fd.ShowApply = true;   // ColorDialog does not provide this option!!!
           fd.Apply += new EventHandler(ApplyFont);
           if(fd.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
               ChangeFont(fd);
       }
       private void ApplyFont(object o, EventArgs ea)
       {
           ChangeFont((FontDialog)o);
       }
       private void ChangeFont(FontDialog fd)
       {
           this.label1.Font = fd.Font;
           this.label1.ForeColor = fd.Color;
       }
   }


 </source>


FontDialog.ShowApply

<source lang="csharp">

using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;

   public class ColorFontDialog : System.Windows.Forms.Form
   {
       private System.Windows.Forms.Button button1;
       private System.Windows.Forms.Button button2;
       private System.Windows.Forms.Label label1;
       public ColorFontDialog()
       {
           this.Text = "Font and Color Dialogs";
           this.button1.Text = "&Font";
           this.button2.Text = "&Color";
           this.label1.Text = "Change my FONT and COLOR!";
           this.button1 = new System.Windows.Forms.Button();
           this.button2 = new System.Windows.Forms.Button();
           this.label1 = new System.Windows.Forms.Label();
           this.SuspendLayout();
           this.button1.Location = new System.Drawing.Point(24, 8);
           this.button1.Text = "button1";
           this.button1.Click += new System.EventHandler(this.button1_Click_1);
           this.button2.Location = new System.Drawing.Point(120, 8);
           this.button2.Text = "button2";
           this.button2.Click += new System.EventHandler(this.button2_Click);
           this.label1.Location = new System.Drawing.Point(8, 48);
           this.label1.Size = new System.Drawing.Size(280, 48);
           this.label1.TabIndex = 2;
           this.label1.Text = "label1";
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
           this.ClientSize = new System.Drawing.Size(292, 101);
           this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                         this.label1,
                                                                         this.button2,
                                                                         this.button1});
           this.Name = "ColorFontDialog";
           this.Text = "ColorFontDialog";
           this.ResumeLayout(false);
       }
       static void Main() 
       {
           Application.Run(new ColorFontDialog());
       }
       private void button2_Click(object sender, System.EventArgs e)
       {
           ColorDialog cd = new ColorDialog();
           cd.AllowFullOpen = true;   // allow custom colors
           cd.FullOpen = true;   // shows custom colors automatically
           cd.Color = Color.DarkBlue;  // sets the custom color
           if(cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
               this.label1.ForeColor = cd.Color;
       }
       private void button1_Click_1(object sender, System.EventArgs e)
       {
           FontDialog fd = new FontDialog();
           fd.ShowColor = true;
           fd.Color = Color.Blue;
           fd.ShowApply = true;   // ColorDialog does not provide this option!!!
           fd.Apply += new EventHandler(ApplyFont);
           if(fd.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
               ChangeFont(fd);
       }
       private void ApplyFont(object o, EventArgs ea)
       {
           ChangeFont((FontDialog)o);
       }
       private void ChangeFont(FontDialog fd)
       {
           this.label1.Font = fd.Font;
           this.label1.ForeColor = fd.Color;
       }
   }


 </source>


FontDialog.ShowColor

<source lang="csharp">

using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;

   public class ColorFontDialog : System.Windows.Forms.Form
   {
       private System.Windows.Forms.Button button1;
       private System.Windows.Forms.Button button2;
       private System.Windows.Forms.Label label1;
       public ColorFontDialog()
       {
           this.Text = "Font and Color Dialogs";
           this.button1.Text = "&Font";
           this.button2.Text = "&Color";
           this.label1.Text = "Change my FONT and COLOR!";
           this.button1 = new System.Windows.Forms.Button();
           this.button2 = new System.Windows.Forms.Button();
           this.label1 = new System.Windows.Forms.Label();
           this.SuspendLayout();
           this.button1.Location = new System.Drawing.Point(24, 8);
           this.button1.Text = "button1";
           this.button1.Click += new System.EventHandler(this.button1_Click_1);
           this.button2.Location = new System.Drawing.Point(120, 8);
           this.button2.Text = "button2";
           this.button2.Click += new System.EventHandler(this.button2_Click);
           this.label1.Location = new System.Drawing.Point(8, 48);
           this.label1.Size = new System.Drawing.Size(280, 48);
           this.label1.TabIndex = 2;
           this.label1.Text = "label1";
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
           this.ClientSize = new System.Drawing.Size(292, 101);
           this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                         this.label1,
                                                                         this.button2,
                                                                         this.button1});
           this.Name = "ColorFontDialog";
           this.Text = "ColorFontDialog";
           this.ResumeLayout(false);
       }
       static void Main() 
       {
           Application.Run(new ColorFontDialog());
       }
       private void button2_Click(object sender, System.EventArgs e)
       {
           ColorDialog cd = new ColorDialog();
           cd.AllowFullOpen = true;   // allow custom colors
           cd.FullOpen = true;   // shows custom colors automatically
           cd.Color = Color.DarkBlue;  // sets the custom color
           if(cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
               this.label1.ForeColor = cd.Color;
       }
       private void button1_Click_1(object sender, System.EventArgs e)
       {
           FontDialog fd = new FontDialog();
           fd.ShowColor = true;
           fd.Color = Color.Blue;
           fd.ShowApply = true;   // ColorDialog does not provide this option!!!
           fd.Apply += new EventHandler(ApplyFont);
           if(fd.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
               ChangeFont(fd);
       }
       private void ApplyFont(object o, EventArgs ea)
       {
           ChangeFont((FontDialog)o);
       }
       private void ChangeFont(FontDialog fd)
       {
           this.label1.Font = fd.Font;
           this.label1.ForeColor = fd.Color;
       }
   }


 </source>


FontDialog.ShowDialog

<source lang="csharp"> using System; using System.Drawing; using System.IO; using System.Windows.Forms; public class MenuDialog : Form {

 TextBox text = new TextBox();
 public MenuDialog() {
   Size = new Size(500,200);
   text.Size = new Size(490,190);
   text.Multiline = true;
   text.ScrollBars = ScrollBars.Both;
   text.WordWrap = false;
   text.Location = new Point(5,5);
   MenuItem fileMenu = new MenuItem("File");
   MenuItem open = new MenuItem("Open");
   open.Shortcut = Shortcut.CtrlO;
   MenuItem save = new MenuItem("Save");
   save.Shortcut = Shortcut.CtrlS;
   fileMenu.MenuItems.Add(open);
   fileMenu.MenuItems.Add(save);
   MenuItem formatMenu = new MenuItem("Format");
   MenuItem font = new MenuItem("Font");
   font.Shortcut = Shortcut.CtrlF;
   formatMenu.MenuItems.Add(font);
    
   MainMenu bar = new MainMenu();
   Menu = bar;
   bar.MenuItems.Add(fileMenu);
   bar.MenuItems.Add(formatMenu);
   Controls.Add(text);
   open.Click += new EventHandler(Open_Click);
   save.Click += new EventHandler(Save_Click);
   font.Click += new EventHandler(Font_Click); 
 }
 
 protected void Open_Click(Object sender, EventArgs e) {
   OpenFileDialog o = new OpenFileDialog();
   if(o.ShowDialog() == DialogResult.OK) {
     Stream file = o.OpenFile();
     StreamReader reader = new StreamReader(file);
     char[] data = new char[file.Length];
     reader.ReadBlock(data,0,(int)file.Length);
     text.Text = new String(data);  
     reader.Close();
   }
 }
 protected void Save_Click(Object sender, EventArgs e) {
   SaveFileDialog s = new SaveFileDialog();
   if(s.ShowDialog() == DialogResult.OK) {
     StreamWriter writer = new StreamWriter(s.OpenFile());
     writer.Write(text.Text);
     writer.Close();
   }
 }
 protected void Font_Click(Object sender, EventArgs e) {
   FontDialog f = new FontDialog();
   if(f.ShowDialog() == DialogResult.OK) 
     text.Font = f.Font;
 }
 public static void Main() {
   Application.Run(new MenuDialog());
 }

}


 </source>


FontDialog.ShowHelp

<source lang="csharp">

 using System;
 using System.Drawing;
 using System.Collections;
 using System.ruponentModel;
 using System.Windows.Forms;
 using System.Data;
 public class Form1 : System.Windows.Forms.Form
 {
   private System.ruponentModel.Container components;
   private System.Windows.Forms.FontDialog fontDlg;
   private Font currFont;
   public Form1()
   {
     InitializeComponent();
     CenterToScreen();
     fontDlg = new System.Windows.Forms.FontDialog();    
     fontDlg.ShowHelp = true;    
     Text = "Click on me to change the font";
     currFont = new Font("Times New Roman", 12);
   }
   private void InitializeComponent()
   {
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(292, 273);
     this.Text = "Form1";
     this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
     this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
   }
   static void Main() 
   {
     Application.Run(new Form1());
   }
   private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
   {
     Graphics g = e.Graphics;
     g.DrawString("www.nfex.ru...", currFont, 
       new SolidBrush(Color.Black), 0, 0);  
   }
   private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
   {
     if (fontDlg.ShowDialog() != DialogResult.Cancel)
     {
       currFont = fontDlg.Font;
       Invalidate();
     }
   }
 }
  
   
 </source>


new FontDialog()

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;

class FontMenuForm: Form {

    protected string strText = "Sample Text";
    protected Font font = new Font("Times New Roman", 24, FontStyle.Italic);
  
    public static void Main()
    {
         Application.Run(new FontMenuForm());
    }
    public FontMenuForm()
    {
         ResizeRedraw = true;
         Menu = new MainMenu();
         Menu.MenuItems.Add("&Font!", new EventHandler(MenuFontOnClick));
    }
    void MenuFontOnClick(object obj, EventArgs ea)
    {
         FontDialog dlg = new FontDialog();
         dlg.Font = font;
  
         if (dlg.ShowDialog() == DialogResult.OK)
         {
              font = dlg.Font;
              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)
    {
         SizeF sizef = grfx.MeasureString(strText, font);
         Brush brush = new SolidBrush(clr);
  
         grfx.DrawString(strText, font, brush, (cx - sizef.Width) / 2,
                                               (cy - sizef.Height) / 2);
    }
    public float GetAscent(Graphics grfx, Font font)
    {
         return font.GetHeight(grfx) * 
                   font.FontFamily.GetCellAscent(font.Style) /
                        font.FontFamily.GetLineSpacing(font.Style);
    }
    public float GetDescent(Graphics grfx, Font font)
    {
         return font.GetHeight(grfx) * 
                   font.FontFamily.GetCellDescent(font.Style) /
                        font.FontFamily.GetLineSpacing(font.Style);
    }
    public float PointsToPageUnits(Graphics grfx, Font font)
    {
         float fFontSize;
  
         if (grfx.PageUnit == GraphicsUnit.Display)
              fFontSize = 100 * font.SizeInPoints / 72;
         else
              fFontSize = grfx.DpiX * font.SizeInPoints / 72;
  
         return fFontSize;
    }

}


 </source>