1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Text;
namespace WinCustomShape
{
public partial class CustomShape : Control
{
private Pen MonStylo;
public CustomShape()
{
InitializeComponent();
text = "1+1";
this.Location = new Point(10, 10);
this.Size = new Size(110, 110);
this.ColorStylo = Color.Black;
this.SetStyle(
ControlStyles.AllPaintingInWmPaint
| ControlStyles.UserPaint
| ControlStyles.OptimizedDoubleBuffer
| ControlStyles.ResizeRedraw, true);
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Bitmap bm = new Bitmap(50, 50);
MonStylo = new Pen(ColorStylo, 3);
Font font = new Font("Comic Sans MS", 12, GraphicsUnit.Point);
Graphics g = pe.Graphics;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
StringFormat stringFormat = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Near
};
Rectangle rect = new Rectangle(0, 0, bm.Width, bm.Height);
// measure how large the text is on the Graphics object with the current font size
SizeF s = g.MeasureString(text, font);
// calculate how to scale the font to make the text fit
float fontScale = Math.Max(s.Width / rect.Width, s.Height / rect.Height);
using (Font fontForDrawing = new Font(font.FontFamily, font.SizeInPoints / fontScale, GraphicsUnit.Point))
{
g.DrawString(text, fontForDrawing, Brushes.Black, 15,15);
g.DrawEllipse(MonStylo, 5, 5, 100, 100);
}
MonStylo.Dispose();
bm.Dispose();
font.Dispose();
}
[Browsable (true)]
[Category ("Cercle")]
public Color ColorStylo { get; set; }
[Browsable(true)]
[Category("Cercle")]
public string text { get; set; }
}
} |
Partager