label.Font = new Font(label.Font, FontStyle.Underline);
private Point m_curPos = Point.Empty; protected override void OnPaint(PaintEventArgs pe) { // StringFormat définissant toute la chaine comme etant à mesurer StringFormat strFormat = new StringFormat(); strFormat.SetMeasurableCharacterRanges(new CharacterRange[]{new CharacterRange(0, this.Text.Length)}); // récupération du Region de notre chaine Region textRegion = pe.Graphics.MeasureCharacterRanges(this.Text, this.Font, this.ClientRectangle, strFormat)[0]; // si le curseur est dans le controle et qu'il se trouve sur notre texte if ( m_curPos != Point.Empty && textRegion.GetRegionScans(pe.Graphics.Transform)[0].Contains(m_curPos) ) { this.Font = new Font(this.Font, this.Font.Style | FontStyle.Underline); } else { this.Font = new Font(this.Font, this.Font.Style & ~FontStyle.Underline); } // Appel à la classe de base OnPaint base.OnPaint(pe); } protected override void OnMouseMove(MouseEventArgs e) { // enregistrement des coordonnées m_curPos.X = e.X; m_curPos.Y = e.Y; base.OnMouseMove (e); // demande d'actualisation de l'affichage this.Invalidate(); } protected override void OnMouseLeave(EventArgs e) { // RAZ des coordonnées m_curPos = Point.Empty; base.OnMouseLeave (e); // demande d'actualisation de l'affichage this.Invalidate(); }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionthis.Font = new Font(this.Font, this.Font.Style | FontStyle.Underline);
this.Font = new Font(this.Font, this.Font.Style & ~FontStyle.Underline);