PiotrR
PiotrR

Reputation: 105

How to set XtraForm title text in the middle without making buttons move?

How can I set XtraForm title text to middle of title bar?
I have tried example from other answer, but it causes window buttons to move few pixels depending on focus.

When I click window title bar window buttons go to one position, when I move mouse over buttons they move to other position few pixels off.

Upvotes: 2

Views: 1840

Answers (1)

DmitryG
DmitryG

Reputation: 17850

Try the following approach (based on XtraForm - How to center-align a header caption text example). It works to me as far as the solution provided by DevExpress team but does not contains Buttons.CalcButtons method call that can theoretically affect form's buttons position:

public partial class Form1 : XtraForm {
    static Form1() {
        SkinManager.EnableFormSkins();
    }
    public Form1() {
        InitializeComponent();
    }
    protected override FormPainter CreateFormBorderPainter() {
        return new CustomFormPainter(this, LookAndFeel);
    }
}
public class CustomFormPainter : FormPainter {
    public CustomFormPainter(Control owner, DevExpress.Skins.ISkinProvider provider)
        : base(owner, provider) {
    }
    protected override void DrawText(DevExpress.Utils.Drawing.GraphicsCache cache) {
        string text = Text;
        if(text == null || text.Length == 0 || TextBounds.IsEmpty) return;
        using(AppearanceObject appearance = new AppearanceObject(GetDefaultAppearance())) {
            appearance.TextOptions.Trimming = Trimming.EllipsisCharacter;
            appearance.TextOptions.HAlignment = HorzAlignment.Center;
            if(AllowHtmlDraw) {
                DrawHtmlText(cache, appearance);
                return;
            }
            Rectangle r = RectangleHelper.GetCenterBounds(TextBounds, new Size(TextBounds.Width, CalcTextHeight(cache.Graphics, appearance)));
            DrawTextShadow(cache, appearance, r);
            cache.DrawString(text, appearance.Font, appearance.GetForeBrush(cache), r, appearance.GetStringFormat());
        }
    }
}

Upvotes: 1

Related Questions