127 lines
4.6 KiB
C#
127 lines
4.6 KiB
C#
|
|
|
|||
|
|
using System;
|
|||
|
|
using System.Drawing;
|
|||
|
|
using System.Windows;
|
|||
|
|
using System.Windows.Interop;
|
|||
|
|
using System.Windows.Media.Imaging;
|
|||
|
|
|
|||
|
|
using Autodesk.Revit.UI;
|
|||
|
|
|
|||
|
|
namespace Szmedi.RevitToolkit.Approval.Extensions
|
|||
|
|
{
|
|||
|
|
public static class RibbonExtensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 帮助文档链接
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="url">帮助文档链接或路径</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static void CreateContextualHelpUrl(this RibbonItem ribbonItem, string url)
|
|||
|
|
{
|
|||
|
|
//".\\*.*"中的“.”表示通用类库文件所在的目录(Debug),“..”表示的是上级的目录(bin),“..”\\..,引号表示再上一级目录
|
|||
|
|
var contextualHelp = new ContextualHelp(ContextualHelpType.Url, url);
|
|||
|
|
ribbonItem.SetContextualHelp(contextualHelp);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 把指定名称的Tab放到第一个
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="tabName"></param>
|
|||
|
|
internal static void InsertToFirst(string tabName)
|
|||
|
|
{
|
|||
|
|
var ribbon = Autodesk.Windows.ComponentManager.Ribbon;
|
|||
|
|
Autodesk.Windows.RibbonTab rt = null;
|
|||
|
|
foreach (var tab in ribbon.Tabs)
|
|||
|
|
{
|
|||
|
|
if (tab.Name == tabName)
|
|||
|
|
{
|
|||
|
|
rt = tab;
|
|||
|
|
ribbon.Tabs.Remove(tab);
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
ribbon.Tabs.Insert(0, rt);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 添加一个新的RibbonButton
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="panel"></param>
|
|||
|
|
/// <param name="buttonData"></param>
|
|||
|
|
public static void AddPushButton<T>(this RibbonPanel panel, string nameText, string tooltip = null, Bitmap smallImage = null, Bitmap largeImage = null) where T : IExternalCommand
|
|||
|
|
{
|
|||
|
|
PushButtonData idCodeData =
|
|||
|
|
new($"{typeof(T).Name}_{nameText}", nameText, typeof(RibbonExtensions).Assembly.Location, typeof(T).FullName)
|
|||
|
|
{
|
|||
|
|
Image = smallImage?.ToBitmapSource() ?? null,
|
|||
|
|
LargeImage = largeImage?.ToBitmapSource() ?? null,
|
|||
|
|
ToolTip = tooltip ?? nameText,
|
|||
|
|
};
|
|||
|
|
panel.AddItem(idCodeData);
|
|||
|
|
}
|
|||
|
|
private static BitmapSource ToBitmapSource(this System.Drawing.Bitmap bitmap)
|
|||
|
|
{
|
|||
|
|
return Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 单选框
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="panel"></param>
|
|||
|
|
/// <param name="groupName"></param>
|
|||
|
|
/// <param name="toggleButtonData"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static RadioButtonGroup AddRadioButtonGroup(this RibbonPanel panel, string groupName, params ToggleButtonData[] toggleButtonData)
|
|||
|
|
{
|
|||
|
|
RadioButtonGroupData groupData = new(groupName);
|
|||
|
|
var radioButtonGroup = (RadioButtonGroup)panel.AddItem(groupData);
|
|||
|
|
radioButtonGroup.AddItems(toggleButtonData);
|
|||
|
|
return radioButtonGroup;
|
|||
|
|
}
|
|||
|
|
public static ToggleButton AddToggleButton(this RadioButtonGroup group, string name, string assemblyName, string className)
|
|||
|
|
{
|
|||
|
|
var toggleData = new ToggleButtonData(name, name, assemblyName, className);
|
|||
|
|
return group.AddItem(toggleData);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 创建一个分割记忆按钮。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="pnl"></param>
|
|||
|
|
/// <param name="splitButton"></param>
|
|||
|
|
/// <param name="array"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static SplitButton AddSplitButton(this RibbonPanel pnl, SplitButtonData splitButton, params PushButtonData[] array)
|
|||
|
|
{
|
|||
|
|
if (pnl is null)
|
|||
|
|
{
|
|||
|
|
throw new ArgumentNullException(nameof(pnl));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (splitButton is null)
|
|||
|
|
{
|
|||
|
|
throw new ArgumentNullException(nameof(splitButton));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (array is null)
|
|||
|
|
{
|
|||
|
|
throw new ArgumentNullException(nameof(array));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (pnl.AddItem(splitButton) is not SplitButton result)
|
|||
|
|
{
|
|||
|
|
throw new InvalidCastException();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
result.ToolTip = splitButton.ToolTip;
|
|||
|
|
|
|||
|
|
result.LongDescription = splitButton.LongDescription;
|
|||
|
|
|
|||
|
|
result.LargeImage = splitButton.LargeImage;
|
|||
|
|
|
|||
|
|
foreach (var buttonData in array)
|
|||
|
|
{
|
|||
|
|
result.AddPushButton(buttonData);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|