-
Notifications
You must be signed in to change notification settings - Fork 19
/
ContextMenuItem.cs
140 lines (127 loc) · 4.18 KB
/
ContextMenuItem.cs
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
139
140
using System;
using System.Collections.Generic;
using Microsoft.Win32;
namespace Ketarin.Forms
{
/// <summary>
/// Represents a custom item within a native context menu.
/// </summary>
public class ContextMenuItem
{
/// <summary>
/// Delegate for a function, which handles the click event for a menu item.
/// </summary>
/// <param name="menuItem">Item which has been clicked</param>
public delegate void ItemSelectHandler(ContextMenuItem menuItem);
private readonly short m_Id;
private int m_Position = -1;
private string m_Text;
private readonly List<ContextMenuItem> m_Items = new List<ContextMenuItem>();
#region Properties
/// <summary>
/// User defined object which belongs to the menu item.
/// </summary>
public object Tag { get; set; }
/// <summary>
/// Gets the list of sub menu items.
/// </summary>
public List<ContextMenuItem> MenuItems
{
get
{
return m_Items;
}
}
/// <summary>
/// Gets the ID of the menu item. The value can be used
/// to identify a menu item.
/// </summary>
public short Id
{
get
{
return m_Id;
}
}
/// <summary>
/// Gets or sets the position within the menu.
/// -1 is at the end, 0 at the start.
/// </summary>
public int Position
{
get
{
return m_Position;
}
set
{
m_Position = value;
}
}
/// <summary>
/// Gets or sets the textual representation of the menu item.
/// An empty string represents a separator.
/// </summary>
public string Text
{
get
{
return m_Text;
}
set
{
m_Text = value;
}
}
/// <summary>
/// Gets or sets the function, which is executed when the
/// item is clicked.
/// </summary>
public ItemSelectHandler EventHandler { get; set; }
#endregion
/// <summary>
/// Creates a new context menu item with the given text and given position.
/// </summary>
public ContextMenuItem(string text, int position) : this(text)
{
m_Position = position;
}
/// <summary>
/// Creates a new context menu item with the given text. Inserted at the end.
/// </summary>
public ContextMenuItem(string text)
{
m_Text = text;
m_Id = ContextMenuCustomiser.GetNextId();
}
/// <summary>
/// Inserts this menu item and all sub items into the given menu.
/// </summary>
/// <param name="hmenu">Handle of a Win32 context menu</param>
internal void InsertMenuItem(IntPtr hmenu)
{
if (string.IsNullOrEmpty(Text) || Text == "-")
{
// Separator
User32.InsertMenu(hmenu, Position, (uint)(User32.MenuFlags.MF_BYPOSITION | User32.MenuFlags.MF_SEPARATOR), new IntPtr(User32.WM_USER + Id), null);
}
else
{
// Do we have submenus?
if (m_Items.Count > 0)
{
IntPtr subMenuItem = User32.CreatePopupMenu();
foreach (ContextMenuItem item in m_Items)
{
item.InsertMenuItem(subMenuItem);
}
User32.InsertMenu(hmenu, Position, (uint)(User32.MenuFlags.MF_BYPOSITION | User32.MenuFlags.MF_POPUP), subMenuItem, Text);
}
else
{
User32.InsertMenu(hmenu, Position, (uint)User32.MenuFlags.MF_BYPOSITION, new IntPtr(User32.WM_USER + Id), Text);
}
}
}
}
}