新增一个类,继承RibbonControl
[C#] 纯文本查看 复制代码 using System;
using System.Drawing;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Ribbon.ViewInfo;
using DevExpress.Utils;
using System.ComponentModel;
namespace testSpace
{
public class Mribbon : RibbonControl
{
public Mribbon() : base() { }
protected override RibbonViewInfo CreateViewInfo() { return new MribbonViewInfo(this); }
#region 属性
private HorzAlignment _FormCaptionAlignment = HorzAlignment.Default;
[CategoryAttribute("MribbonSettings"), Description("窗体标题对齐方式"), DefaultValue(typeof(HorzAlignment), "Default")]
public HorzAlignment FormCaptionAlignment
{
get { return _FormCaptionAlignment; }
set { _FormCaptionAlignment = value; LayoutChanged(); }
}
bool _ShowGroupCaption = true;
[CategoryAttribute("MribbonSettings"), Description("是否显示Group标题"), DefaultValue(true)]
public bool ShowGroupCaption
{
get { return _ShowGroupCaption; }
set { if (_ShowGroupCaption != value) _ShowGroupCaption = value; }
}
#endregion
}
public class MribbonViewInfo : RibbonViewInfo
{
public MribbonViewInfo(RibbonControl ribbon) : base(ribbon) { }
protected override RibbonCaptionViewInfo CreateCaptionInfo()
{
return new MribbonCaptionViewInfo(this);
}
protected override RibbonPanelViewInfo CreatePanelInfo()
{
return new MribbonPanelViewInfo(this);
}
}
#region 设置窗体标题对其模式
public class MribbonCaptionViewInfo : RibbonCaptionViewInfo
{
public MribbonCaptionViewInfo(RibbonViewInfo viewInfo)
: base(viewInfo)
{
}
public HorzAlignment FormCaptionAlignment
{
get { return (ViewInfo.Ribbon as Mribbon).FormCaptionAlignment; }
}
protected override Rectangle CalcTextBounds()
{
var centerTextBounds = base.CalcTextBounds();
if (FormCaptionAlignment == HorzAlignment.Near)
{
centerTextBounds.Offset(ContentBounds.X - centerTextBounds.X, 0);
}
if (FormCaptionAlignment == HorzAlignment.Far)
{
centerTextBounds.Offset(ContentBounds.Right - centerTextBounds.Right, 0);
}
return centerTextBounds;
}
}
#endregion
public class MribbonPageGroupViewInfo : RibbonPageGroupViewInfo
{
public MribbonPageGroupViewInfo(RibbonViewInfo viewInfo, RibbonPageGroup group) : base(viewInfo, group) { }
//设置Group右对齐
public override void CalcViewInfo(Rectangle bounds)
{
if (object.Equals(PageGroup.Tag, "AlignRight"))
{
int offset = ViewInfo.Bounds.Right - bounds.Right;
if (offset > 5)
bounds.Offset(offset, 0);
}
base.CalcViewInfo(bounds);
}
//是否显示群组标题
protected override DevExpress.Utils.Drawing.GroupObjectInfoArgs SetupDrawArgs()
{
DevExpress.Utils.Drawing.GroupObjectInfoArgs args = base.SetupDrawArgs();
args.ShowCaption = (base.Ribbon as Mribbon).ShowGroupCaption;
return args;
}
}
public class MribbonPanelViewInfo : RibbonPanelViewInfo
{
public MribbonPanelViewInfo(RibbonViewInfo viewInfo) : base(viewInfo) { }
protected override RibbonPageGroupViewInfo CreateGroupViewInfo(RibbonPageGroup group)
{
return new MribbonPageGroupViewInfo(ViewInfo, group);
}
}
}
|