[UserControl - TextBox] 2 format de texte
Bonjour,
je crèè une surcharge du TextBox pour le rendre, pour l'instant, plus design
par la suite j'ajouterais peut etre d'autre possibilitè
mon problème est que lorsue je tape tu texte, il s'écrit en gras et Font standard
une fois sortis du TextBox surcharger le text prend sa place avec le Font definie dans la PropertGrid
j'utilise 2 fonction non definie ici:
Hauteur_Optimal => Calcul la hauteur optimal du TextBox en fonction du Font
GetRoundedRect => fonction trouver sur internet qui genere des rectangle avec les coins en arrondis, rendus parfait!
voici mon code:
Code:
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
| public partial class TextBox_TBR : TextBox
{
public TextBox_TBR()
{
// Le control se peint lui même
// Le control utilise un double buffer (anti-scintillement
// Le control supporte la transparence
// Le control ignore le messsage WM_ERASEBKGND pour reduire le scintillement
this.SetStyle(ControlStyles.UserPaint |
ControlStyles.DoubleBuffer |
ControlStyles.SupportsTransparentBackColor |
ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
this.BorderStyle = BorderStyle.FixedSingle;
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
pevent.Graphics.FillRectangle(new SolidBrush(this.Parent.BackColor),
new Rectangle(0, 0, this.Width, this.Height));
}
protected override void OnPaint(PaintEventArgs e)
{
HauteurOptimal();
// Fond
GraphicsPath RRBackground = this.GetRoundedRect(new Rectangle(0, 0, this.Width - 1, this.Height - 1), 3F);
e.Graphics.FillPath(new SolidBrush(this.BackColor), RRBackground);
e.Graphics.DrawPath(new Pen(Color.FromArgb(183, 202, 234)), RRBackground);
// Texte
StringFormat sf = StringFormat.GenericTypographic;
sf.LineAlignment = StringAlignment.Far;
string _textResize = this.Text;
if (e.Graphics.MeasureString(_textResize, this.Font).Width > (this.Width - 3))
_textResize += "...";
while (e.Graphics.MeasureString(_textResize, this.Font).Width > (this.Width - 3))
_textResize = _textResize.Substring(0, _textResize.Length - 4) + "...";
e.Graphics.DrawString(_textResize, this.Font, new SolidBrush(this.ForeColor),
new Rectangle(5, -5, this.Width, this.Height),
sf);
sf.Dispose();
} |