tangxu
2024-10-22 6a07c4c846ffbb1e93afdf0260e123e4c145f419
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// *********************************
// 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.Diagnostics;
using System.ComponentModel;
using System.Security.Permissions;
using System.Windows.Forms.RibbonHelpers;
 
namespace System.Windows.Forms
{
    public class RibbonForm
        : Form, IRibbonForm
    {
 
        #region Fields
        private bool? _isopeninvisualstudiodesigner;
        #endregion
 
        #region Ctor
 
        public RibbonForm():
            base()
        {
            if (!IsOpenInVisualStudioDesigner())
            {
                if (WinApi.IsWindows && !WinApi.IsGlassEnabled)
                {
                    FormBorderStyle = FormBorderStyle.None;
                    SetStyle(ControlStyles.ResizeRedraw, true);
                    SetStyle(ControlStyles.Opaque, WinApi.IsGlassEnabled);
                    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                    DoubleBuffered = true;
                }
                Helper = new RibbonFormHelper(this);
            }
        }
        #endregion
 
        #region Overrides
        protected bool IsOpenInVisualStudioDesigner()
        {
            if (!_isopeninvisualstudiodesigner.HasValue)
            {
                _isopeninvisualstudiodesigner = LicenseManager.UsageMode == LicenseUsageMode.Designtime ||
                                                this.DesignMode;
                if (!_isopeninvisualstudiodesigner.Value)
                {
                    try
                    {
                        using (var process = Process.GetCurrentProcess())
                        {
                            _isopeninvisualstudiodesigner = process.ProcessName.ToLowerInvariant().Contains("devenv");
                        }
                    }
                    catch
                    {
                    }
                }
            }
            return _isopeninvisualstudiodesigner.Value;
        }
 
        /// <summary>
        /// Just for debugging messages
        /// </summary>
        /// <param name="m"></param>
        protected override void OnNotifyMessage(Message m)
        {
            base.OnNotifyMessage(m);
        }
 
        /// <summary>
        /// Overrides the WndProc funciton
        /// </summary>
        /// <param name="m"></param>
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        protected override void WndProc(ref Message m)
        {
            if (IsOpenInVisualStudioDesigner())
            {
                base.WndProc(ref m);
            }
            else if (!Helper.WndProc(ref m))
            {
                base.WndProc(ref m);
            }
        }
 
        protected override CreateParams CreateParams
        {
            [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
            get
            {
                CreateParams cp = base.CreateParams;
                if (!IsOpenInVisualStudioDesigner())
                {
                    if (WinApi.IsWindows && !WinApi.IsGlassEnabled)
                    {
                        cp.Style |= 0x20000 | 0x80000 | 0x40000; //WS_MINIMIZEBOX | WS_SYSMENU | WS_SIZEBOX;
                                                                 //cp.ClassStyle |= 0x8 | 0x20000; //CS_DBLCLKS | CS_DROPSHADOW;
                    }
                }
                return cp;
            }
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            if (IsOpenInVisualStudioDesigner())
            {
                base.OnPaint(e);
            }
            else
            {
                // override OnPaint and do NOT call base, otherwise problems as MDI parent occur
                Helper.Form_Paint(this, e);
            }
        }
 
        #endregion
 
        #region IRibbonForm Members
 
        /// <summary>
        /// Gets the helper for making the form a ribbon form
        /// </summary>
        public RibbonFormHelper Helper { get; }
 
        #endregion
    }
}