tangxu
2024-10-14 6cd995b71dfc74d4d96347d0bc535fddf36fa9df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// *********************************
// Message from Original Author:
//
// 2008 Jose Menendez Poo
// Please give me credit if you use this code. It's all I ask.
// Contact me for more info: menendezpoo@gmail.com
// *********************************
//
// Original project from http://ribbon.codeplex.com/
// Continue to support and maintain by http://officeribbon.codeplex.com/
 
 
using System.ComponentModel;
using System.Drawing;
 
namespace System.Windows.Forms
{
    public sealed class RibbonSeparator : RibbonItem
    {
        public RibbonSeparator()
        {
            DropDownWidth = RibbonSeparatorDropDownWidth.Partial;
        }
 
        public RibbonSeparator(string text)
            : this()
        {
            Text = text;
        }
 
        public override void OnPaint(object sender, RibbonElementPaintEventArgs e)
        {
            if ((Owner == null || !DrawBackground) && !Owner.IsDesignMode())
                return;
 
            Owner.Renderer.OnRenderRibbonItem(new RibbonItemRenderEventArgs(
            Owner, e.Graphics, e.Clip, this));
 
            if (!string.IsNullOrEmpty(Text))
            {
                Owner.Renderer.OnRenderRibbonItemText(new RibbonTextEventArgs(
                        Owner, e.Graphics, e.Clip, this,
                        Rectangle.FromLTRB(
                            Bounds.Left + Owner.ItemMargin.Left,
                            Bounds.Top + Owner.ItemMargin.Top,
                            Bounds.Right - Owner.ItemMargin.Right,
                            Bounds.Bottom - Owner.ItemMargin.Bottom), Text, FontStyle.Bold));
            }
        }
 
        public override void SetBounds(Rectangle bounds)
        {
            base.SetBounds(bounds);
        }
 
        public override Size MeasureSize(object sender, RibbonElementMeasureSizeEventArgs e)
        {
            if (e.SizeMode == RibbonElementSizeMode.DropDown)
            {
                // A horizontal separator on a menu
                if (string.IsNullOrEmpty(Text))
                {
                    SetLastMeasuredSize(new Size(1, 2));
                }
                else
                {
                    Size sz = e.Graphics.MeasureString(Text, new Font(Owner.Font, FontStyle.Bold)).ToSize();
                    SetLastMeasuredSize(new Size(sz.Width + Owner.ItemMargin.Horizontal + 1, sz.Height + Owner.ItemMargin.Vertical));
                }
            }
            else
            {
                // A vertical separator on a Panel or the QAT
                if (OwnerPanel == null)
                {
                    // A vertical separator on the QAT
                    SetLastMeasuredSize(new Size(7, Owner.QuickAccessToolbar.ContentBounds.Height - Owner.QuickAccessToolbar.Padding.Vertical));
                }
                else
                {
                    // A vertical separator on a Panel
                    SetLastMeasuredSize(new Size(4, OwnerPanel.ContentBounds.Height - Owner.ItemPadding.Vertical - Owner.ItemMargin.Vertical));
                }
            }
 
            return LastMeasuredSize;
        }
 
        /// <summary>
        /// Gets or sets a value indicating if the separator should draw the divider lines
        /// </summary>
        [DefaultValue(true)]
        [Category("Appearance")]
        [Description("Background drawing should be avoided when group contains only TextBoxes and ComboBoxes")]
        public bool DrawBackground { get; set; } = true;
 
        /// <summary>
        /// The width of the Separator bar when displayed on a drop down
        /// </summary>
        [DefaultValue(0)]
        [Category("Appearance")]
        [Description("The width of the Separator bar when displayed on a drop down")]
        public RibbonSeparatorDropDownWidth DropDownWidth { get; set; }
 
    }
}