| 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 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.Linq; |
|---|
| 25 | using System.Text; |
|---|
| 26 | |
|---|
| 27 | using Eraser.Util; |
|---|
| 28 | using System.Drawing; |
|---|
| 29 | |
|---|
| 30 | namespace 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 | createParams.Style |= NativeMethods.BS_SPLITBUTTON; |
|---|
| 48 | return createParams; |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | protected override void WndProc(ref Message m) |
|---|
| 53 | { |
|---|
| 54 | switch ((uint)m.Msg) |
|---|
| 55 | { |
|---|
| 56 | case 0x2000 | NativeMethods.WM_NOTIFY: |
|---|
| 57 | //Reflected WM_NOTIFY from parent. Check that the handle is ours |
|---|
| 58 | if (m.HWnd == Handle) |
|---|
| 59 | { |
|---|
| 60 | //Then check the code of the message |
|---|
| 61 | NativeMethods.NMHDR nmHdr = (NativeMethods.NMHDR) |
|---|
| 62 | m.GetLParam(typeof(NativeMethods.NMHDR)); |
|---|
| 63 | |
|---|
| 64 | //Handle only BCN_DROPDOWN messages |
|---|
| 65 | if (nmHdr.code == NativeMethods.BCN_DROPDOWN) |
|---|
| 66 | { |
|---|
| 67 | //The dropdown portion of the button is being pressed, show the menu |
|---|
| 68 | if (ContextMenuStrip != null) |
|---|
| 69 | { |
|---|
| 70 | Point point = new Point(Width - ContextMenuStrip.Width, Height); |
|---|
| 71 | ContextMenuStrip.Show(this, point, ToolStripDropDownDirection.BelowRight); |
|---|
| 72 | ContextMenuStrip.Closed += OnMenuClosed; |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | break; |
|---|
| 77 | |
|---|
| 78 | case NativeMethods.WM_PAINT: |
|---|
| 79 | //Paint the control to have the dropdown portion as pressed when the |
|---|
| 80 | //menu is shown. |
|---|
| 81 | DropDownState = ContextMenuStrip != null && ContextMenuStrip.Visible; |
|---|
| 82 | break; |
|---|
| 83 | |
|---|
| 84 | case NativeMethods.WM_CONTEXTMENU: |
|---|
| 85 | //Swallow all context menu clicks on Vista and later -- otherwise |
|---|
| 86 | //we will also show the context menu when the user right-clicks |
|---|
| 87 | //the button. |
|---|
| 88 | if (Environment.OSVersion.Version.Major >= 6) |
|---|
| 89 | return; |
|---|
| 90 | break; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | base.WndProc(ref m); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | private bool DropDownState |
|---|
| 97 | { |
|---|
| 98 | set |
|---|
| 99 | { |
|---|
| 100 | NativeMethods.SendMessage(this.Handle, NativeMethods.BCM_SETDROPDOWNSTATE, |
|---|
| 101 | new UIntPtr(value ? 1U : 0), IntPtr.Zero); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | private void OnMenuClosed(object sender, EventArgs e) |
|---|
| 106 | { |
|---|
| 107 | DropDownState = false; |
|---|
| 108 | ContextMenuStrip.Closed -= OnMenuClosed; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } |
|---|