using System; using System.Drawing; using System.Windows.Forms; class BoldAndItalic : Form { public static void Main() { Application.Run(new BoldAndItalic()); } public BoldAndItalic() { Text = "Bold and Italic Text"; 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) { float x = 0; float y = 0; grfx.DrawString("text", new Font(this.Font, FontStyle.Bold), new SolidBrush(Color.AliceBlue), x, y); } }
Author: coder
Text on Baseline
using System; using System.Drawing; using System.Windows.Forms; class TextOnBaseline: Form{ public static void Main() { Application.Run(new TextOnBaseline()); } public TextOnBaseline() { 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) { float yBaseline = cy / 2; Pen pen = new Pen(clr); grfx.DrawLine(pen, 0, yBaseline, cx, yBaseline); Font font = new Font("Times New Roman", 144); float cyLineSpace = font.GetHeight(grfx); int iCellSpace = font.FontFamily.GetLineSpacing(font.Style); int iCellAscent = font.FontFamily.GetCellAscent(font.Style); float cyAscent = cyLineSpace * iCellAscent / iCellSpace; grfx.DrawString("Baseline", font, new SolidBrush(clr), 0, yBaseline - cyAscent); } }
Font: SizeInPoints
using System; using System.Drawing; using System.Windows.Forms; class HowdyWorld: Form { public static void Main() { Application.Run(new HowdyWorld()); } public HowdyWorld() { ResizeRedraw = true; MinimumSize = SystemInformation.MinimumWindowSize + new Size(0,1); } 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) { Font font = new Font("Times New Roman", 10, FontStyle.Italic); SizeF sizef = grfx.MeasureString(Text, font); float fScale = Math.Min(cx / sizef.Width, cy / sizef.Height); font = new Font(font.Name, fScale * font.SizeInPoints, font.Style); sizef = grfx.MeasureString(Text, font); grfx.DrawString(Text, font, new SolidBrush(clr), (cx - sizef.Width ) / 2, (cy - sizef.Height) / 2); } }
Font with different size
using System; using System.Drawing; using System.Windows.Forms; class TwentyFourPointPrinterFonts: Form { public static void Main() { Application.Run(new TwentyFourPointPrinterFonts()); } public TwentyFourPointPrinterFonts() { Text = "Twenty-Four Point Printer Fonts"; 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); float y = 0; Font font; string strFamily = "Times New Roman"; font = new Font(strFamily, 24); grfx.DrawString("No GraphicsUnit, 24 points", font, brush, 0, y); y += font.GetHeight(grfx); font = new Font(strFamily, 24, GraphicsUnit.Point); grfx.DrawString("GraphicsUnit.Point, 24 units", font, brush, 0, y); y += font.GetHeight(grfx); font = new Font(strFamily, 1/ 3f, GraphicsUnit.Inch); grfx.DrawString("GraphicsUnit.Inch, 1/3 units", font, brush, 0, y); y += font.GetHeight(grfx); font = new Font(strFamily, 25.4f / 3, GraphicsUnit.Millimeter); grfx.DrawString("GraphicsUnit.Millimeter, 25.4/3 units", font, brush, 0, y); y += font.GetHeight(grfx); font = new Font(strFamily, 100, GraphicsUnit.Document); grfx.DrawString("GraphicsUnit.Document, 100 units", font, brush, 0, y); y += font.GetHeight(grfx); font = new Font(strFamily, 100f / 3, GraphicsUnit.Pixel); grfx.DrawString("GraphicsUnit.Pixel, " + 100f / 3 + " units", font, brush, 0, y); y += font.GetHeight(grfx); font = new Font(strFamily, 100f / 3, GraphicsUnit.World); grfx.DrawString("GraphicsUnit.World, " + 100f / 3 + " units", font, brush, 0, y); } }
Control Docking
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace Docking { /// <summary> /// Summary description for FormDocking. /// </summary> public class FormDocking : System.Windows.Forms.Form { private System.Windows.Forms.StatusBar statusBar1; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public FormDocking() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.statusBar1 = new System.Windows.Forms.StatusBar(); this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // statusBar1 // this.statusBar1.Location = new System.Drawing.Point(0, 253); this.statusBar1.Name = "statusBar1"; this.statusBar1.Size = new System.Drawing.Size(292, 20); this.statusBar1.TabIndex = 9; this.statusBar1.Text = "statusBar1"; // // textBox1 // this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill; this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(292, 273); this.textBox1.TabIndex = 2; this.textBox1.Text = "Top Bottom Left Right"; // // label1 // this.label1.Dock = System.Windows.Forms.DockStyle.Top; this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(292, 16); this.label1.TabIndex = 10; this.label1.Text = "Docking Sample"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // FormDocking // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label1, this.statusBar1, this.textBox1}); this.Name = "FormDocking"; this.Text = "FormDocking"; this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new FormDocking()); } } }
Image Drop
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Windows.Forms; class ImageDrop : Form { bool bIsTarget; Image image; public static void Main() { Application.Run(new ImageDrop()); } public ImageDrop() { AllowDrop = true; } protected override void OnDragOver(DragEventArgs dea) { if (dea.Data.GetDataPresent(DataFormats.FileDrop) || dea.Data.GetDataPresent(typeof(Metafile)) || dea.Data.GetDataPresent(typeof(Bitmap))) { if ((dea.AllowedEffect & DragDropEffects.Move) != 0) dea.Effect = DragDropEffects.Move; if (((dea.AllowedEffect & DragDropEffects.Copy) != 0) && ((dea.KeyState & 0x08) != 0)) // Ctrl key dea.Effect = DragDropEffects.Copy; } } protected override void OnDragDrop(DragEventArgs dea) { if (dea.Data.GetDataPresent(DataFormats.FileDrop)) { string[] astr = (string[])dea.Data.GetData(DataFormats.FileDrop); image = Image.FromFile(astr[0]); Invalidate(); } else { if (dea.Data.GetDataPresent(typeof(Metafile))) image = (Image)dea.Data.GetData(typeof(Metafile)); else if (dea.Data.GetDataPresent(typeof(Bitmap))) image = (Image)dea.Data.GetData(typeof(Bitmap)); bIsTarget = true; Invalidate(); } } protected override void OnMouseDown(MouseEventArgs mea) { if (image != null) { bIsTarget = false; DragDropEffects dde = DoDragDrop(image,DragDropEffects.Copy | DragDropEffects.Move); if (dde == DragDropEffects.Move && !bIsTarget) image = null; } } }
Drag and drop inside a container
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form { internal System.Windows.Forms.Label lblDragger; public Form1() { InitializeComponent(); } private void InitializeComponent() { this.lblDragger = new System.Windows.Forms.Label(); this.SuspendLayout(); // // lblDragger // this.lblDragger.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblDragger.Image = new Bitmap("winter.jpg"); this.lblDragger.Location = new System.Drawing.Point(110, 105); this.lblDragger.Name = "lblDragger"; this.lblDragger.Size = new System.Drawing.Size(72, 56); this.lblDragger.TabIndex = 2; this.lblDragger.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseUp); this.lblDragger.MouseMove += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseMove); this.lblDragger.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseDown); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.lblDragger}); this.Name = "Form1"; this.Text = "Fake Drag And Drop"; this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new Form1()); } private bool isDragging = false; private int clickOffsetX, clickOffsetY; private void lblDragger_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e) { isDragging = true; clickOffsetX = e.X; clickOffsetY = e.Y; } private void lblDragger_MouseUp(System.Object sender, System.Windows.Forms.MouseEventArgs e) { isDragging = false; } private void lblDragger_MouseMove(System.Object sender, System.Windows.Forms.MouseEventArgs e) { if (isDragging == true) { lblDragger.Left = e.X + lblDragger.Left - clickOffsetX; lblDragger.Top = e.Y + lblDragger.Top - clickOffsetY; } } }