source: trunk/eraser/Eraser.Util/ExtensionClasses/SplitButton.cs @ 2515

Revision 2515, 3.2 KB checked in by lowjoel, 3 years ago (diff)

Set svn:keywords and svn:eol-style on all the source files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Rev URL
Line 
1/*
2 * $Id$
3 * Copyright 2008-2010 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26
27using Eraser.Util;
28using System.Drawing;
29
30namespace System.Windows.Forms
31{
32    public class SplitButton : Button
33    {
34        /// <summary>
35        /// Constructor.
36        /// </summary>
37        public SplitButton()
38        {
39            FlatStyle = FlatStyle.System;
40        }
41
42        protected override CreateParams CreateParams
43        {
44            get
45            {
46                CreateParams createParams = base.CreateParams;
47                if (IsSupportedOnCurrentPlatform)
48                    createParams.Style |= NativeMethods.BS_SPLITBUTTON;
49                return createParams;
50            }
51        }
52
53        protected override void WndProc(ref Message m)
54        {
55            switch ((uint)m.Msg)
56            {
57                case 0x2000 | NativeMethods.WM_NOTIFY:
58                    //Reflected WM_NOTIFY from parent. Check that the handle is ours
59                    if (m.HWnd == Handle)
60                    {
61                        //Then check the code of the message
62                        NativeMethods.NMHDR nmHdr = (NativeMethods.NMHDR)
63                            m.GetLParam(typeof(NativeMethods.NMHDR));
64
65                        //Handle only BCN_DROPDOWN messages
66                        if (nmHdr.code == NativeMethods.BCN_DROPDOWN)
67                        {
68                            //The dropdown portion of the button is being pressed, show the menu
69                            if (ContextMenuStrip != null)
70                            {
71                                Point point = new Point(Width - ContextMenuStrip.Width, Height);
72                                ContextMenuStrip.Show(this, point, ToolStripDropDownDirection.BelowRight);
73                                ContextMenuStrip.Closed += OnMenuClosed;
74                            }
75                        }
76                    }
77                    break;
78
79                case NativeMethods.WM_PAINT:
80                    //Paint the control to have the dropdown portion as pressed when the
81                    //menu is shown.
82                    DropDownState = ContextMenuStrip != null && ContextMenuStrip.Visible;
83                    break;
84
85                case NativeMethods.WM_CONTEXTMENU:
86                    //Swallow all context menu clicks on Vista and later -- otherwise
87                    //we will also show the context menu when the user right-clicks
88                    //the button.
89                    if (!IsSupportedOnCurrentPlatform)
90                        return;
91                    break;
92            }
93
94            base.WndProc(ref m);
95        }
96
97        /// <summary>
98        /// Checks whether the current platform supports the control.
99        /// </summary>
100        private static bool IsSupportedOnCurrentPlatform
101        {
102            get
103            {
104                return Environment.OSVersion.Version.Major >= 6;
105            }
106        }
107
108        private bool DropDownState
109        {
110            set
111            {
112                NativeMethods.SendMessage(this.Handle, NativeMethods.BCM_SETDROPDOWNSTATE,
113                    new UIntPtr(value ? 1U : 0), IntPtr.Zero);
114            }
115        }
116
117        private void OnMenuClosed(object sender, EventArgs e)
118        {
119            DropDownState = false;
120            ContextMenuStrip.Closed -= OnMenuClosed;
121        }
122    }
123}
Note: See TracBrowser for help on using the repository browser.