用C#和VB.NET实现VS.NET或Office XP风格的菜单(三)

用C#和VB.NET实现VS.NET或Office XP风格的菜单

小气的神 2001.08.18

3. “MenuItemStyle” 接口和 VS.NET 风格的菜单项

这个 Project 又将切换到 C #语言。我是这样想的:先针对普通菜单、 Office200 风格、 VS.NET 风格三种情况定义一个统一的接口( interface ),其中包括画 Icon ( DrawIcon )、画分割条( DrawSeparator )、画菜单背景( DrawBackground )、写菜单项的文字( DrawMenuText )等功能;普通、 Office2000 和 VS.NET 根据各自不同的情况实现这个接口的 Drawxxx 的功能。然后从 MenuItem 继承一个子类,象第二部分讲的那样 Overrides 菜单项的两个函数: OnMeasureItem 和 OnDrawItem ,根据不同的风格调用上面实现的接口中的 DrawXXX 函数就可以了。最后我把这部分都分隔出来放在一个 .CS 文件中,单独编译成一个 VSNET.Menu.DLL ,你只用 using VSNET.Menu ; 然后就可以象在第一部分那样象使用普通的 MenuItem 那样来用了, Demo 源代码中你还可以看到我定义了 IconMenuItem 的类,它有一个方法: MenuItemCreator ( VSNET.Menu.IconMenuStyle sType , String sText , Bitmap bmp , System.EventHandler eh )可以完成生成需要的 MenuItem 。本来我想用资源文件或将图片 Icon 等资源放在一个专门的文件中,然后由这个类来负责从资源文件或外部的类中获得资源 CreateMenuItem 。但是是第一版,你会看到例程中我仍然用原始的 New Bitmap ()的方式直接从硬盘拿资源。当我看到它 show 出来时,先是很开心,然后发现还有许多要改进,想想其实做一个专业的菜单也需要花许多心思。

好吧让我们看一下有关 VS.NET 风格菜单项这部分主要的实现代码:

public class VSNetStyle : MenuItemStyleDrawer

{

static Color bgcolor = Color.FromArgb(246, 246, 246);

static Color ibgcolor = Color.FromArgb(202, 202, 202);

static Color sbcolor = Color.FromArgb(173, 173, 209);

static Color sbbcolor = Color.FromArgb( 0, 0, 128);

static int TEXTSTART = 20;

public void DrawCheckmark(Graphics g, Rectangle bounds, bool selected)

{

ControlPaint.DrawMenuGlyph(g, new Rectangle(bounds.X + 2, bounds.Y + 2, 14, 14), MenuGlyph.Checkmark);

}

public void DrawIcon(Graphics g, Image icon, Rectangle bounds, bool selected, bool enabled, bool ischecked)

{

if (enabled)

{

if (selected)

{

ControlPaint.DrawImageDisabled(g, icon, bounds.Left + 2, bounds.Top + 2, Color.Black);

g.DrawImage(icon, bounds.Left + 1, bounds.Top + 1);

}

else

{

g.DrawImage(icon, bounds.Left + 2, bounds.Top + 2);

}

}

else

ControlPaint.DrawImageDisabled(g, icon, bounds.Left + 2, bounds.Top + 2, SystemColors.HighlightText);

}

public void DrawSeparator(Graphics g, Rectangle bounds)

{

int y = bounds.Y + bounds.Height / 2;

g.DrawLine(new Pen(SystemColors.ControlDark), bounds.X + SystemInformation.SmallIconSize.Width + 7, y, bounds.X + bounds.Width - 2, y);

}

public void DrawBackground(Graphics g, Rectangle bounds, DrawItemState state, bool toplevel, bool hasicon)

{

bool selected = (state & DrawItemState.Selected) > 0;

if (selected || ((state & DrawItemState.HotLight) > 0))

{

if (toplevel && selected)

{ // draw toplevel, selected menuitem

g.FillRectangle(new SolidBrush(ibgcolor), bounds);

ControlPaint.DrawBorder3D(g, bounds.Left, bounds.Top, bounds.Width, bounds.Height, Border3DStyle.Flat, Border3DSide.Top | Border3DSide.Left | Border3DSide.Right);

}

else

{ // draw menuitem, selected OR toplevel, hotlighted

g.FillRectangle(new SolidBrush(sbcolor), bounds);

g.DrawRectangle(new Pen(sbbcolor), bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);

}

}

else

{

if (!toplevel)

{ // draw menuitem, unselected

g.FillRectangle(new SolidBrush(ibgcolor), bounds);

bounds.X += SystemInformation.SmallIconSize.Width + 5;

bounds.Width -= SystemInformation.SmallIconSize.Width + 5;

g.FillRectangle(new SolidBrush(bgcolor), bounds);

}

else

{

// draw toplevel, unselected menuitem

g.FillRectangle(SystemBrushes.Menu, bounds);

}

}

}

public void DrawMenuText(Graphics g, Rectangle bounds, string text, string shortcut, bool enabled, bool toplevel, DrawItemState state)

{

StringFormat stringformat = new StringFormat();

stringformat.HotkeyPrefix = ((state & DrawItemState.NoAccelerator) > 0) ? HotkeyPrefix.Hide : HotkeyPrefix.Show;

int textwidth = (int)(g.MeasureString(text, SystemInformation.MenuFont).Width);

int x = toplevel ? bounds.Left + (bounds.Width - textwidth) / 2: bounds.Left + TEXTSTART;

int y = bounds.Top + 2;

Brush brush = null;

if (!enabled)

brush = new SolidBrush(Color.FromArgb(120, SystemColors.MenuText));

else

brush = new SolidBrush(Color.Black);

g.DrawString(text, SystemInformation.MenuFont, brush, x, y, stringformat);

g.DrawString(shortcut, SystemInformation.MenuFont, brush, bounds.Left + 130, bounds.Top + 2, stringformat);

}

}

MenuItemStyleDrawer 就是那个公用的接口类,无论普通风格、 Office2000 还是 VS.NET 风格都要实现自己方式的接口,这个接口包括 DrawCheckmark 、 DrawIcon 、 DrawMenuText 、 DrawBackground 、 DrawSeparator 等函数,可以实现菜单项需要的各种函数。完成这部分后可以从 MenuItem 继承一个子类来象第二部分一样处理了。看下面的代码,具体考察一下熟悉的 OnMeasureItem 和 OnDrawItem :

protected override void OnMeasureItem(MeasureItemEventArgs e)

{

base.OnMeasureItem(e);

// make shortcut text 省略这部分代码。

if (menustyle != IconMenuStyle.Standard)

{

if (Text == "-")

{

e.ItemHeight = 8;

e.ItemWidth = 4;

return;

}

int textwidth = (int)(e.Graphics.MeasureString(Text + shortcuttext, SystemInformation.MenuFont).Width);

e.ItemHeight = SystemInformation.MenuHeight;

if (Parent == Parent.GetMainMenu())

e.ItemWidth = textwidth - 5; // 5 is a magic number

else

e.ItemWidth = Math.Max(160, textwidth + 50);

}

}

IconMenuStyle.Standard 是个 enum 表明是普通风格、 Office2000 或是 VS 。 NET 的风格。这部分和我们第二部分看到的没有什么不同。

protected override void OnDrawItem(DrawItemEventArgs e)

{

base.OnDrawItem(e);

Graphics g = e.Graphics;

Rectangle bounds = e.Bounds;

bool selected = (e.State & DrawItemState.Selected) > 0;

bool toplevel = (Parent == Parent.GetMainMenu());

bool hasicon = Icon != null;

style.DrawBackground(g, bounds, e.State, toplevel, hasicon);

if (hasicon)

style.DrawIcon(g, Icon, bounds, selected, Enabled, Checked);

else

if (Checked)

style.DrawCheckmark(g, bounds, selected);

if (Text == "-")

{

style.DrawSeparator(g, bounds);

}

else

{

style.DrawMenuText(g, bounds, Text, shortcuttext, Enabled, toplevel, e.State);

}

}

刚刚我们说的 MenuItemStyleDrawer 接口的好处在这里显示出来,整个过程显得简单明了,具体实现得代码不是很多。当这个类完成后,剩下来的就是使用了它了,这部分象第一部分所述,你可以在一个顶级菜单项的子菜单项声明成 IconMenu 类型的也就是我们实现的继承 MenuItem 的类,简单的代码象下面这样:

private System.Windows.Forms.MenuItem mItems1 ; System.Drawing.Bitmap Bitmap1 = new Bitmap( BMPPATHSTR + "Open.bmp") ;

mItems1 = iMenuItem.MenuItemCreator( MenuStyle , "&Open" , Bitmap1,

这个 mItem1 就是一个 VS.NET 风格的菜单项了。具体的可以看附带的 Project 和屏幕截图。

至此我们完成了用 VB.NET 或 C# 完成一个有 VS.NET 或 Office XP 风格的菜单。三个部分是渐进的,如果你以前进行或实验过第二部分讨论的问题,那么第三部分唯一让人感兴趣的是 MenuItemStyleDrawer 接口的思路。对于整个新的 .NET 的编程方式上说,原来的 VB 用户可能会经历一个痛苦的过程 , 他们的第一反应是 Sub Class 、 Hook 或是终极的 API ,而接触过 C ++、 MFC 、 DELPHI 甚至 VJ ++的用户会很容易想到继承,特别时 DELPHI 和 VJ 的用户入手应当最快了。想想会开始怀念以前 VB 的时光,因为对于这样的问题, VB 用户总是拿着大锤,直接敲个大洞,然后拿到结果;而 C ++、 MFC 、 DEPHI 用户则拿着一本说明书,一步一步按指示找到结果;结果可能一样,但两者的方式是截然不同的。好了,这些是题外话了。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus