You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.6 KiB
83 lines
2.6 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Apewer.WinForm
|
|
{
|
|
|
|
/// <summary>Windows 窗体工具。</summary>
|
|
public static class Extensions
|
|
{
|
|
|
|
static void MenuItemEventHandler(object sender, EventArgs e)
|
|
{
|
|
#if NETFRAMEWORK
|
|
var mi = sender as System.Windows.Forms.MenuItem;
|
|
if (mi != null)
|
|
{
|
|
var tag = mi.Tag as MenuItem;
|
|
if (tag != null && tag.Action != null) tag.Action.Invoke(tag);
|
|
}
|
|
#endif
|
|
|
|
var tsmi = sender as System.Windows.Forms.ToolStripMenuItem;
|
|
if (tsmi != null)
|
|
{
|
|
var tag = tsmi.Tag as MenuItem;
|
|
if (tag != null && tag.Action != null) tag.Action.Invoke(tag);
|
|
}
|
|
}
|
|
|
|
#if NETFRAMEWORK
|
|
|
|
/// <summary>生成 <see cref="System.Windows.Forms.ContextMenu" /> 实例。</summary>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
public static ContextMenu ContextMenu(this IEnumerable<MenuItem> items)
|
|
{
|
|
if (items == null) throw new ArgumentNullException(nameof(items));
|
|
|
|
var cm = new ContextMenu();
|
|
foreach (var item in items)
|
|
{
|
|
var isLine = item == null || item.Text == null || item.Text == "" || item.Text == "-";
|
|
var text = isLine ? "-" : item?.Text;
|
|
var mi = new System.Windows.Forms.MenuItem(text, MenuItemEventHandler);
|
|
mi.Enabled = !isLine && item.Action != null;
|
|
mi.Tag = item;
|
|
|
|
cm.MenuItems.Add(mi);
|
|
}
|
|
|
|
return cm;
|
|
}
|
|
|
|
#endif
|
|
|
|
/// <summary>生成 <see cref="ContextMenuStrip" /> 实例。</summary>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
public static ContextMenuStrip ContextMenuStrip(this IEnumerable<MenuItem> items)
|
|
{
|
|
if (items == null) throw new ArgumentNullException(nameof(items));
|
|
|
|
var cms = new ContextMenuStrip();
|
|
foreach (var item in items)
|
|
{
|
|
if (item == null) continue;
|
|
|
|
var isLine = item.Text == null || item.Text == "" || item.Text == "-";
|
|
var text = isLine ? "-" : item.Text;
|
|
var tsmi = new ToolStripMenuItem(text, item.Image, MenuItemEventHandler);
|
|
tsmi.AutoSize = true;
|
|
tsmi.Height = isLine ? 19 : 30;
|
|
tsmi.Enabled = item.Action != null && item.Action != null;
|
|
tsmi.Tag = item;
|
|
|
|
cms.Items.Add(tsmi);
|
|
}
|
|
|
|
return cms;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|