Csharp/C Sharp by API/System.Drawing/StringAlignment

Материал из .Net Framework эксперт
Версия от 12:11, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

StringAlignment.Center

  

using System;
using System.Drawing;
using System.Windows.Forms;
   
class ClipText: Form
{
     string   strText = "Sample text";
     MenuItem miCut, miCopy, miPaste;
   
     public static void Main()
     {
          Application.Run(new ClipText());
     }
     public ClipText()
     {
          ResizeRedraw = true;
   
          Menu = new MainMenu();
          MenuItem mi = new MenuItem("&Edit");
          mi.Popup += new EventHandler(MenuEditOnPopup);
          Menu.MenuItems.Add(mi);
          miCut = new MenuItem("Cu&t");
          miCut.Click += new EventHandler(MenuEditCutOnClick);
          miCut.Shortcut = Shortcut.CtrlX;
          Menu.MenuItems[0].MenuItems.Add(miCut);
   
          miCopy = new MenuItem("&Copy");
          miCopy.Click += new EventHandler(MenuEditCopyOnClick);
          miCopy.Shortcut = Shortcut.CtrlC;
          Menu.MenuItems[0].MenuItems.Add(miCopy);
   
          miPaste = new MenuItem("&Paste");
          miPaste.Click += new EventHandler(MenuEditPasteOnClick);
          miPaste.Shortcut = Shortcut.CtrlV;
          Menu.MenuItems[0].MenuItems.Add(miPaste);
     }
     void MenuEditOnPopup(object obj, EventArgs ea)
     {
          miCut.Enabled = miCopy.Enabled = strText.Length > 0;
          miPaste.Enabled = Clipboard.GetDataObject().GetDataPresent(typeof(string));
     }
     void MenuEditCutOnClick(object obj, EventArgs ea)
     {
          MenuEditCopyOnClick(obj, ea);
          strText = "";
          Invalidate();
     }
     void MenuEditCopyOnClick(object obj, EventArgs ea)
     {
          Clipboard.SetDataObject(strText, true);
     }
     void MenuEditPasteOnClick(object obj, EventArgs ea)
     {
          IDataObject data = Clipboard.GetDataObject();
   
          if (data.GetDataPresent(typeof(string)))
               strText = (string) data.GetData(typeof(string));
   
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
          StringFormat strfmt = new StringFormat();
          strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
   
          grfx.DrawString(strText, Font, new SolidBrush(ForeColor),
                          ClientRectangle, strfmt);
     }
}


StringAlignment.Far

  

using System;
using System.Drawing;
using System.Windows.Forms;
   
class FourCorners: Form
{
     public static void Main()
     {
          Application.Run(new FourCorners());
     }
     public FourCorners()
     {
          Text = "Four Corners Text Alignment";
          BackColor = SystemColors.Window;
          ForeColor = SystemColors.WindowText;
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics     graphics   = pea.Graphics;
          Brush        brush  = new SolidBrush(ForeColor);
          StringFormat strfmt = new StringFormat();
   
          strfmt.Alignment     = StringAlignment.Far;
          strfmt.LineAlignment = StringAlignment.Far;
          graphics.DrawString("Lower right corner", Font, brush, ClientSize.Width, ClientSize.Height, strfmt);
     }
}


StringAlignment.Near

  
using System;
using System.Drawing;
using System.Windows.Forms;
   
class FourCorners: Form
{
     public static void Main()
     {
          Application.Run(new FourCorners());
     }
     public FourCorners()
     {
          Text = "Four Corners Text Alignment";
          BackColor = SystemColors.Window;
          ForeColor = SystemColors.WindowText;
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics     graphics   = pea.Graphics;
          Brush        brush  = new SolidBrush(ForeColor);
          StringFormat strfmt = new StringFormat();
   
          strfmt.Alignment     = StringAlignment.Near;
          strfmt.LineAlignment = StringAlignment.Near;
          graphics.DrawString("Upper left corner", Font, brush, 0, 0, strfmt);
   
     }
}