【问题标题】:How to set XtraForm title text in the middle without making buttons move?如何在不移动按钮的情况下在中间设置 XtraForm 标题文本?
【发布时间】:2014-10-06 07:56:22
【问题描述】:

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

当我单击窗口标题栏时,窗口按钮会转到一个位置,当我将鼠标移到按钮上时,它们会移动到另一个位置,相距几个像素。

【问题讨论】:

  • 我已经尝试过您提到的answer 的解决方案,并且一切正常。你试过to report this issue to DevExpress Support Team吗?
  • 还没有。你知道这种效果是否是由某些自定义皮肤设置引起的吗?
  • 很难说,因为我无法重现您描述的问题。此外,我们在此处讨论的自定义解决方案仅在绘画时更改文本位置,因此我根本无法想象它会如何影响按钮。
  • 我已经查看了 DevExpress 解决方案,并得出结论,理论上,他们的代码会影响按钮。请检查我的新答案,让我知道你的结果。

标签: c# winforms devexpress


【解决方案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());
        }
    }
}

【讨论】:

  • 解决了窗口按钮移动的问题。谢谢!
  • 很高兴听到问题已解决!我认为现在是就这个问题联系 DevExpress 团队并建议查看知识库文章的好时机。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多