如何在不设置按钮的情况下在中间设置XtraForm标题文本?

时间:2014-10-06 07:56:23

标签: c# winforms devexpress

如何将XtraForm标题文字设置为标题栏的中间?
我尝试过来自other answer的示例,但它会导致窗口按钮根据焦点移动几个像素。

当我点击窗口标题栏窗口按钮转到一个位置时,当我将鼠标移到按钮上时,它们会移动到其他几个像素的位置。

1 个答案:

答案 0 :(得分:1)

尝试以下方法(基于XtraForm - How to center-align a header caption text示例)。就DevExpress团队提供的解决方案而言,它对我有用,但不包含理论上影响表单按钮位置的Buttons.CalcButtons方法调用:

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());
        }
    }
}