AutoCAD .Net 创建Ribbon界面(二)

来源:互联网 发布:陌生人社交软件 编辑:程序博客网 时间:2024/06/06 00:33

上一篇文章实现了简单的 Ribbon 页面,仅仅包含了文字按钮。
接下来,我们来实现如下图所示的包含图片的 Ribbon 控件。
这里写图片描述
首先,在项目中添加资源文件。在资源文件中添加两张图片:
* smiley_16x16.png
* smiley_32x32.png

大小分别是 16x16 和 32x32。
将上一篇中创建 Ribbon 按钮的代码替换为如下的代码。

// 按钮 Test1RibbonButton btn1 = new RibbonButton();btn1.Text = "Test1";btn1.ShowText = true;btn1.Image = ImageSourceForBitmap(Resource1.smiley_16x16);btn1.LargeImage = ImageSourceForBitmap(Resource1.smiley_32x32);btn1.ShowImage = true;btn1.Size = RibbonItemSize.Large;btn1.Orientation = System.Windows.Controls.Orientation.Horizontal;panelSrc.Items.Add(btn1);// 按钮 Test2RibbonButton btn2 = new RibbonButton();btn2.Text = "Test2";btn2.ShowText = true;btn2.Image = ImageSourceForBitmap(Resource1.smiley_16x16);btn2.LargeImage = ImageSourceForBitmap(Resource1.smiley_32x32);btn2.ShowImage = true;btn2.Size = RibbonItemSize.Large;btn2.Orientation = System.Windows.Controls.Orientation.Vertical;panelSrc.Items.Add(btn2);// 按钮 Test3RibbonButton btn3 = new RibbonButton();btn3.Text = "Test2";btn3.ShowText = true;btn3.Image = ImageSourceForBitmap(Resource1.smiley_16x16);btn3.LargeImage = ImageSourceForBitmap(Resource1.smiley_32x32);btn3.ShowImage = true;btn3.Size = RibbonItemSize.Standard;btn3.Orientation = System.Windows.Controls.Orientation.Vertical;panelSrc.Items.Add(btn3);

添加引用 System.Drawing。
还要添加如下的处理 Bitmap 的代码。

using System;using System.Text;using System.Runtime.InteropServices;using System.Drawing;using System.Windows;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Interop;
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")][return: MarshalAs(UnmanagedType.Bool)]public static extern bool DeleteObject([In] IntPtr hObject);public static ImageSource ImageSourceForBitmap(Bitmap bmp){    var handle = bmp.GetHbitmap();    try    {        return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());    }    finally { DeleteObject(handle); }}

RibbonButton 有两个属性 Image 和 LargeImage,分别存放小图片和大图片。
小图片大小应为 16x16。
大图片大小应为 32x32。
最终,显示小图片还是大图片,通过指定 Size 为 RibbonItemSize.Standard 还是 RibbonItemSize.Large 确定。
ShowText,是否显示文字。
ShowImage,是否显示图片。
Orientation,指定文字、图片的排布方式。

原创粉丝点击