Je veux montrer une étiquette pivotée à 90 degrés (donc je peux en mettre un tas en haut d'une table comme les rubriques). Y a-t-il un moyen facile de faire ceci?Comment faire pivoter une étiquette en C#?
Répondre
Vous devrez écrire le vôtre ou utiliser un contrôle personnalisé.
Un article que vous pouvez commencer est Customized Text - Orientated Controls in C# - Part I (Label Control). Cela contient des fonctionnalités supplémentaires, vous devriez donc être en mesure de le réduire si vous le souhaitez.
Et voici un code de ce qui est d'intérêt:
/// <summary>
/// This is a lable, in which you can set the text in any direction/angle
/// </summary>
#region Orientation
//Orientation of the text
public enum Orientation
{
Circle,
Arc,
Rotate
}
public enum Direction
{
Clockwise,
AntiClockwise
}
#endregion
public class OrientedTextLabel : System.Windows.Forms.Label
{
#region Variables
private double rotationAngle;
private string text;
private Orientation textOrientation;
private Direction textDirection;
#endregion
#region Constructor
public OrientedTextLabel()
{
//Setting the initial condition.
rotationAngle = 0d;
textOrientation = Orientation.Rotate;
this.Size = new Size(105,12);
}
#endregion
#region Properties
[Description("Rotation Angle"),Category("Appearance")]
public double RotationAngle
{
get
{
return rotationAngle;
}
set
{
rotationAngle = value;
this.Invalidate();
}
}
[Description("Kind of Text Orientation"),Category("Appearance")]
public Orientation TextOrientation
{
get
{
return textOrientation;
}
set
{
textOrientation = value;
this.Invalidate();
}
}
[Description("Direction of the Text"),Category("Appearance")]
public Direction TextDirection
{
get
{
return textDirection;
}
set
{
textDirection = value;
this.Invalidate();
}
}
[Description("Display Text"),Category("Appearance")]
public override string Text
{
get
{
return text;
}
set
{
text = value;
this.Invalidate();
}
}
#endregion
#region Method
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.Trimming = StringTrimming.None;
Brush textBrush = new SolidBrush(this.ForeColor);
//Getting the width and height of the text, which we are going to write
float width = graphics.MeasureString(text,this.Font).Width;
float height = graphics.MeasureString(text,this.Font).Height;
//The radius is set to 0.9 of the width or height, b'cos not to
//hide and part of the text at any stage
float radius = 0f;
if (ClientRectangle.Width<ClientRectangle.Height)
{
radius = ClientRectangle.Width *0.9f/2;
}
else
{
radius = ClientRectangle.Height *0.9f/2;
}
//Setting the text according to the selection
switch (textOrientation)
{
case Orientation.Arc:
{
//Arc angle must be get from the length of the text.
float arcAngle = (2*width/radius)/text.Length;
if(textDirection == Direction.Clockwise)
{
for (int i=0; i<text.Length; i++)
{
graphics.TranslateTransform(
(float)(radius*(1 - Math.Cos(arcAngle*i + rotationAngle/180 * Math.PI))),
(float)(radius*(1 - Math.Sin(arcAngle*i + rotationAngle/180*Math.PI))));
graphics.RotateTransform((-90 + (float)rotationAngle + 180*arcAngle*i/(float)Math.PI));
graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
graphics.ResetTransform();
}
}
else
{
for (int i=0; i<text.Length; i++)
{
graphics.TranslateTransform(
(float)(radius*(1 - Math.Cos(arcAngle*i + rotationAngle/180*Math.PI))),
(float)(radius*(1 + Math.Sin(arcAngle*i + rotationAngle/180*Math.PI))));
graphics.RotateTransform((-90 - (float)rotationAngle - 180*arcAngle*i/(float)Math.PI));
graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
graphics.ResetTransform();
}
}
break;
}
case Orientation.Circle:
{
if (textDirection == Direction.Clockwise)
{
for(int i=0;i<text.Length;i++)
{
graphics.TranslateTransform(
(float)(radius*(1 - Math.Cos((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))),
(float)(radius*(1 - Math.Sin((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))));
graphics.RotateTransform(-90 + (float)rotationAngle + (360/text.Length)*i);
graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
graphics.ResetTransform();
}
}
else
{
for(int i=0;i<text.Length;i++)
{
graphics.TranslateTransform(
(float)(radius*(1 - Math.Cos((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))),
(float)(radius*(1 + Math.Sin((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))));
graphics.RotateTransform(-90 - (float)rotationAngle - (360/text.Length)*i);
graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
graphics.ResetTransform();
}
}
break;
}
case Orientation.Rotate:
{
//For rotation, who about rotation?
double angle = (rotationAngle/180)*Math.PI;
graphics.TranslateTransform(
(ClientRectangle.Width+(float)(height*Math.Sin(angle))-(float)(width*Math.Cos(angle)))/2,
(ClientRectangle.Height-(float)(height*Math.Cos(angle))-(float)(width*Math.Sin(angle)))/2);
graphics.RotateTransform((float)rotationAngle);
graphics.DrawString(text,this.Font,textBrush,0,0);
graphics.ResetTransform();
break;
}
}
}
#endregion
}
Vous pouvez également jeter un oeil à la configuration de Windows ToolStrip. Il dispose d'une option pour TextDirection qui peut être définie sur Vertical90 ou Vertical270 et cela fera pivoter votre texte Label dans la direction appropriée.
Vue d'ensemble: ToolStrip Class (MSDN)
courte vidéo sur son utilisation: Pour http://msdn.microsoft.com/en-us/vstudio/bb798042 rotation voir 03h05 pour la rotation spécifique.
- 1. Comment faire pivoter une image sur l'iPhone?
- 2. Je dois faire pivoter une image
- 3. Faire pivoter des images en Javascript
- 4. Comment faire pivoter un tableau 2D d'entiers
- 5. Comment faire pivoter une sous-classe NSView soutenue par CALayer
- 6. comment faire pivoter UIIMageVIew avec point fixe?
- 7. faire pivoter un ensemble d'images dans une ellipse
- 8. Faire pivoter une page de 180 degrés dans LaTeX
- 9. Faire pivoter un point par un autre point en 2D
- 10. Comment faire une TrackBar continue en C#?
- 11. Faire pivoter UIView dans Cocoa Touch
- 12. Comment "faire pivoter" une couche/vue (par exemple, tout comme vous le feriez en énigmo)
- 13. Comment puis-je faire pivoter une image de la flèche en appuyant sur cette image?
- 14. Glisser pour faire pivoter dans l'application iPhone
- 15. Faire pivoter un objet AS3 dynamiquement créé
- 16. Faire pivoter une image dans la programmation Windows Mobile
- 17. Comment retourner/faire pivoter un document PrintDocument dans .NET?
- 18. Méthode rapide pour faire pivoter l'image dans .NET Compact Framework en C#
- 19. Comment faire pivoter l'image dans le compteur de vitesse?
- 20. comment envelopper une étiquette html autour d'une étiquette d'image?
- 21. Comment faire pivoter l'image (degré) avec le curseur jQuery?
- 22. Comment ajouter une marque à une étiquette
- 23. En P4V, comment créer une branche à partir d'une étiquette?
- 24. Comment pivoter dynamiquement
- 25. comment faire pivoter du texte dans HSSFCell dans apache POI?
- 26. J'ai besoin d'aide pour faire pivoter une image avec OpenGL en utilisant SDL
- 27. La meilleure façon de faire pivoter une image en utilisant SDL?
- 28. Comment faire pivoter une ligne de texte à chaque actualisation de page?
- 29. Faire pivoter la vue d'un PDF sur un formulaire WinForm
- 30. Comment utiliser la fonction getControlPointAtIndex pour faire pivoter un UILabel en demi-cercle dans l'iPhone
Le deuxième lien semble être rompu. –
@PeterMortensen devrait être utilisable maintenant. –
+1 pour être extrêmement facile. – adam