Displaying Rotated Text on Form

Depending on the design of developed application, it may sometimes be required to display vertically aligned text or text rotated for a certain angle instead of simple horizontal text labels. Standard components of form designer do not allow for executing such a task. To execute the task, use the ImageButton component. To do this, set up its visual view according to text label and load an image with rotated text. If text must be rotated for a random angle, it is required to previously edit images in a graphic editor. If text is required to rotate for the angle that is multiple to 90, it is available to dynamically create images by means of resources of the Drawing assembly:

Sub ChangeAgleForText(Text: String; ImgButton: ImageButton);
Var
    Graph: IGxGraphics;
    TextSize: IGxRectF;
    Img: IGxImage;
Begin
    Graph := GxGraphicsClass.FromImage(ImgButton.GetImage);
    //Set up a button that looks as the Label component
    ImgButton.BorderStyle := ControlBorderStyle.None;
    //Calculate the size of the text that is located in the component
    TextSize := Graph.MeasureTextRF(Text, ImgButton.NormalLook.Font.GxFont, New GxRectF.Create(0000), Null);
    //Set size and text of the button, get its image
    ImgButton.Height := Double.RoundInt(TextSize.Height);
    ImgButton.Width := Double.RoundInt(TextSize.Width);
    ImgButton.Text := Text;
    Img := ImgButton.GetImage;
    //Rotate image
    Img.RotateFlip(GxRotateFlipType.Rotate90FlipNone);
    //Rotate button, delete text and set new image
    ImgButton.Height := Double.RoundInt(TextSize.Width);
    ImgButton.Width := Double.RoundInt(TextSize.Height);
    ImgButton.Text := "";
    ImgButton.DisabledLook.Background := Img;
    //Deny access to the button in order it works in text label mode
    ImgButton.Enabled := False;
End Sub ChangeAgleForText;

This procedure dynamically creates an image based on the passed text and sets it to the passed component. The procedure can be called, for example, in the event of the OnCreate form:

Sub TESTFormOnCreate(Sender: Object; Args: IEventArgs);
Begin
    ChangeAgleForText("Text", ImageButton1);
End Sub TESTFormOnCreate;

See also:

Examples