首页 > 开发 > 综合 > 正文

VB打造超酷个性化菜单(二)

2024-07-21 02:20:58
字体:
来源:转载
供稿:网友
vb打造超酷个性化菜单(二)


其实,漂亮的界面都是“画”出来的,菜单当然也不例外。既然是“画”出来的,就需要有窗体来接收“画”菜单这个消息,后面我们会看到,实际上不仅仅是“画”这个消息,一切关于这个菜单的消息都要有一个窗体来接收。如果你对消息不太了解,可以看看网上其它一些关于windows消息机制的文章。不了解也没有关系,只要会使用就可以了,后面的文章给出了完整的源代码,而且文章的最后还给出了源代码的下载地址。

下面我们来创建接收消息的窗体:打开上次建好的工程,添加一个窗体,并将其名称设置为frmmenu(注意:这一步是必须的)。还记得上篇文章的最后一幅图吗?菜单左边那个黑底色的附加条,为了方便,将frmmenu的picture属性设置成那幅图。到此,这个窗体就算ok了!对了,就这样,因为这个窗体仅仅是为了处理消息和存储那个黑底色的风格条,我们将会对它进行子类处理,处理消息的代码全部都放在了将在下一篇中详细介绍的标准模块中。

接下来添加一个类模块,并将其名称设置为cmenu,代码如下:

'**************************************************************************************************************

'* 本类模块是一个菜单类, 提供了各种样式的菜单的制作方案

'*

'* 版权: lpp软件工作室

'* 作者: 卢培培(goodname008)

'* (******* 复制请保留以上信息 *******)

'**************************************************************************************************************



option explicit



private declare function trackpopupmenu lib "user32" (byval hmenu as long, byval wflags as long, byval x as long, byval y as long, byval nreserved as long, byval hwnd as long, lprc as any) as long



public enum menuuserstyle ' 菜单总体风格

style_windows

style_xp

style_shade

style_3d

style_colorful

end enum



public enum menuseparatorstyle ' 菜单分隔条风格

mss_solid

mss_dash

mss_dot

mss_dasdot

mss_dashdotdot

mss_none

mss_default

end enum



public enum menuitemselectfillstyle ' 菜单项背景填充风格

isfs_none

isfs_solidcolor

isfs_horizontalcolor

isfs_verticalcolor

end enum



public enum menuitemselectedgestyle ' 菜单项边框风格

ises_solid

ises_dash

ises_dot

ises_dasdot

ises_dashdotdot

ises_none

ises_sunken

ises_raised

end enum



public enum menuitemiconstyle ' 菜单项图标风格

iis_none

iis_sunken

iis_raised

iis_shadow

end enum



public enum menuitemselectscope ' 菜单项高亮条的范围

iss_text = &h1

iss_icon_text = &h2

iss_leftbar_icon_text = &h4

end enum



public enum menuleftbarstyle ' 菜单附加条风格

lbs_none

lbs_solidcolor

lbs_horizontalcolor

lbs_verticalcolor

lbs_image

end enum



public enum menuitemtype ' 菜单项类型

mit_string = &h0

mit_checkbox = &h200

mit_separator = &h800

end enum



public enum menuitemstate ' 菜单项状态

mis_enabled = &h0

mis_disabled = &h2

mis_checked = &h8

mis_unchecked = &h0

end enum



public enum popupalign ' 菜单弹出对齐方式

popup_leftalign = &h0& ' 水平左对齐

popup_centeralign = &h4& ' 水平居中对齐

popup_rightalign = &h8& ' 水平右对齐

popup_topalign = &h0& ' 垂直上对齐

popup_vcenteralign = &h10& ' 垂直居中对齐

popup_bottomalign = &h20& ' 垂直下对齐

end enum



' 释放类

private sub class_terminate()

setwindowlong frmmenu.hwnd, gwl_wndproc, premenuwndproc

erase myiteminfo

destroymenu hmenu

end sub



' 创建弹出式菜单

public sub createmenu()

premenuwndproc = setwindowlong(frmmenu.hwnd, gwl_wndproc, addressof menuwndproc)

hmenu = createpopupmenu()

me.style = style_windows

end sub



' 插入菜单项并保存自定义菜单项数组, 设置owner_draw自绘菜单

public sub additem(byval itemalias as string, byval itemicon as stdpicture, byval itemtext as string, byval itemtype as menuitemtype, optional byval itemstate as menuitemstate)

static id as long, i as long

dim iteminfo as menuiteminfo

' 插入菜单项

with iteminfo

.cbsize = lenb(iteminfo)

.fmask = miim_string or miim_ftype or miim_state or miim_submenu or miim_id or miim_data

.ftype = itemtype

.fstate = itemstate

.wid = id

.dwitemdata = true

.cch = lstrlen(itemtext)

.dwtypedata = itemtext

end with

insertmenuitem hmenu, id, false, iteminfo



' 将菜单项数据存入动态数组

redim preserve myiteminfo(id) as mymenuiteminfo



for i = 0 to ubound(myiteminfo)

if myiteminfo(i).itemalias = itemalias then

class_terminate

err.raise vbobjecterror + 513, "cmenu", "菜单项别名相同."

end if

next i



with myiteminfo(id)

set .itemicon = itemicon

.itemtext = itemtext

.itemtype = itemtype

.itemstate = itemstate

.itemalias = itemalias

end with



' 获得菜单项数据

with iteminfo

.cbsize = lenb(iteminfo)

.fmask = miim_data or miim_id or miim_type

end with

getmenuiteminfo hmenu, id, false, iteminfo



' 设置菜单项数据

with iteminfo

.fmask = .fmask or miim_type

.ftype = mft_ownerdraw

end with

setmenuiteminfo hmenu, id, false, iteminfo



' 菜单项id累加

id = id + 1



end sub



' 删除菜单项

public sub deleteitem(byval itemalias as string)

dim i as long

for i = 0 to ubound(myiteminfo)

if myiteminfo(i).itemalias = itemalias then

deletemenu hmenu, i, 0

exit for

end if

next i

end sub



' 弹出菜单

public sub popupmenu(byval x as long, byval y as long, byval align as popupalign)

trackpopupmenu hmenu, align, x, y, 0, frmmenu.hwnd, byval 0

end sub



' 设置菜单项图标

public sub setitemicon(byval itemalias as string, byval itemicon as stdpicture)

dim i as long

for i = 0 to ubound(myiteminfo)

if myiteminfo(i).itemalias = itemalias then

set myiteminfo(i).itemicon = itemicon

exit for

end if

next i

end sub



' 获得菜单项图标

public function getitemicon(byval itemalias as string) as stdpicture

dim i as long

for i = 0 to ubound(myiteminfo)

if myiteminfo(i).itemalias = itemalias then

set getitemicon = myiteminfo(i).itemicon

exit for

end if

next i

end function



' 设置菜单项文字

public sub setitemtext(byval itemalias as string, byval itemtext as string)

dim i as long

for i = 0 to ubound(myiteminfo)

if myiteminfo(i).itemalias = itemalias then

myiteminfo(i).itemtext = itemtext

exit for

end if

next i

end sub



' 获得菜单项文字

public function getitemtext(byval itemalias as string) as string

dim i as long

for i = 0 to ubound(myiteminfo)

if myiteminfo(i).itemalias = itemalias then

getitemtext = myiteminfo(i).itemtext

exit for

end if

next i

end function



' 设置菜单项状态

public sub setitemstate(byval itemalias as string, byval itemstate as menuitemstate)

dim i as long

for i = 0 to ubound(myiteminfo)

if myiteminfo(i).itemalias = itemalias then

myiteminfo(i).itemstate = itemstate

dim iteminfo as menuiteminfo

with iteminfo

.cbsize = len(iteminfo)

.fmask = miim_string or miim_ftype or miim_state or miim_submenu or miim_id or miim_data

end with

getmenuiteminfo hmenu, i, false, iteminfo

with iteminfo

.fstate = .fstate or itemstate

end with

setmenuiteminfo hmenu, i, false, iteminfo

exit for

end if

next i

end sub



' 获得菜单项状态

public function getitemstate(byval itemalias as string) as menuitemstate

dim i as long

for i = 0 to ubound(myiteminfo)

if myiteminfo(i).itemalias = itemalias then

getitemstate = myiteminfo(i).itemstate

exit for

end if

next i

end function



' 属性: 菜单句柄

public property get hwnd() as long

hwnd = hmenu

end property



public property let hwnd(byval nvalue as long)



end property



' 属性: 菜单附加条宽度

public property get leftbarwidth() as long

leftbarwidth = barwidth

end property



public property let leftbarwidth(byval nbarwidth as long)

if nbarwidth >= 0 then

barwidth = nbarwidth

end if

end property



' 属性: 菜单附加条风格

public property get leftbarstyle() as menuleftbarstyle

leftbarstyle = barstyle

end property



public property let leftbarstyle(byval nbarstyle as menuleftbarstyle)

if nbarstyle >= 0 and nbarstyle <= 4 then

barstyle = nbarstyle

end if

end property



' 属性: 菜单附加条图像(只有当 leftbarstyle 设置为 lbs_image 时才有效)

public property get leftbarimage() as stdpicture

set leftbarimage = barimage

end property



public property let leftbarimage(byval nbarimage as stdpicture)

set barimage = nbarimage

end property



' 属性: 菜单附加条过渡色起始颜色(只有当 leftbarstyle 设置为 lbs_horizontalcolor 或 lbs_verticalcolor 时才有效)

' 当 leftbarstyle 设置为 lbs_solidcolor (实色填充)时以 leftbarstartcolor 颜色为准

public property get leftbarstartcolor() as long

leftbarstartcolor = barstartcolor

end property



public property let leftbarstartcolor(byval nbarstartcolor as long)

barstartcolor = nbarstartcolor

end property



' 属性: 菜单附加条过渡色终止颜色(只有当 leftbarstyle 设置为 lbs_horizontalcolor 或 lbs_verticalcolor 时才有效)

' 当 leftbarstyle 设置为 lbs_solidcolor (实色填充)时以 leftbarstartcolor 颜色为准

public property get leftbarendcolor() as long

leftbarendcolor = barendcolor

end property



public property let leftbarendcolor(byval nbarendcolor as long)

barendcolor = nbarendcolor

end property



' 属性: 菜单项高亮条的范围

public property get itemselectscope() as menuitemselectscope

itemselectscope = selectscope

end property



public property let itemselectscope(byval nselectscope as menuitemselectscope)

selectscope = nselectscope

end property



' 属性: 菜单项可用时文字颜色

public property get itemtextenabledcolor() as long

itemtextenabledcolor = textenabledcolor

end property



public property let itemtextenabledcolor(byval ntextenabledcolor as long)

textenabledcolor = ntextenabledcolor

end property



' 属性: 菜单项不可用时文字颜色

public property get itemtextdisabledcolor() as long

itemtextdisabledcolor = textdisabledcolor

end property



public property let itemtextdisabledcolor(byval ntextdisabledcolor as long)

textdisabledcolor = ntextdisabledcolor

end property



' 属性: 菜单项选中时文字颜色

public property get itemtextselectcolor() as long

itemtextselectcolor = textselectcolor

end property



public property let itemtextselectcolor(byval ntextselectcolor as long)

textselectcolor = ntextselectcolor

end property



' 属性: 菜单项图标风格

public property get itemiconstyle() as menuitemiconstyle

itemiconstyle = iconstyle

end property



public property let itemiconstyle(byval niconstyle as menuitemiconstyle)

iconstyle = niconstyle

end property



' 属性: 菜单项边框风格

public property get itemselectedgestyle() as menuitemselectedgestyle

itemselectedgestyle = edgestyle

end property



public property let itemselectedgestyle(byval nedgestyle as menuitemselectedgestyle)

edgestyle = nedgestyle

end property



' 属性: 菜单项边框颜色

public property get itemselectedgecolor() as long

itemselectedgecolor = edgecolor

end property



public property let itemselectedgecolor(byval nedgecolor as long)

edgecolor = nedgecolor

end property



' 属性: 菜单项背景填充风格

public property get itemselectfillstyle() as menuitemselectfillstyle

itemselectfillstyle = fillstyle

end property



public property let itemselectfillstyle(byval nfillstyle as menuitemselectfillstyle)

fillstyle = nfillstyle

end property



' 属性: 菜单项过渡色起始颜色(只有当 itemselectfillstyle 设置为 isfs_horizontalcolor 或 isfs_verticalcolor 时才有效)

' 当 itemselectfillstyle 设置为 isfs_solidcolor (实色填充)时以 itemselectfillstartcolor 颜色为准

public property get itemselectfillstartcolor() as long

itemselectfillstartcolor = fillstartcolor

end property



public property let itemselectfillstartcolor(byval nfillstartcolor as long)

fillstartcolor = nfillstartcolor

end property



' 属性: 菜单项过渡色终止颜色(只有当 itemselectfillstyle 设置为 isfs_horizontalcolor 或 isfs_verticalcolor 时才有效)

' 当 itemselectfillstyle 设置为 isfs_solidcolor (实色填充)时以 itemselectfillstartcolor 颜色为准

public property get itemselectfillendcolor() as long

itemselectfillendcolor = fillendcolor

end property



public property let itemselectfillendcolor(byval nfillendcolor as long)

fillendcolor = nfillendcolor

end property



' 属性: 菜单背景颜色

public property get backcolor() as long

backcolor = bkcolor

end property



public property let backcolor(byval nbkcolor as long)

bkcolor = nbkcolor

end property



' 属性: 菜单分隔条风格

public property get separatorstyle() as menuseparatorstyle

separatorstyle = sepstyle

end property



public property let separatorstyle(byval nsepstyle as menuseparatorstyle)

sepstyle = nsepstyle

end property



' 属性: 菜单分隔条颜色

public property get separatorcolor() as long

separatorcolor = sepcolor

end property



public property let separatorcolor(byval nsepcolor as long)

sepcolor = nsepcolor

end property



' 属性: 菜单总体风格

public property get style() as menuuserstyle

style = menustyle

end property



public property let style(byval nmenustyle as menuuserstyle)

menustyle = nmenustyle

select case nmenustyle

case style_windows ' windows 默认风格

set barimage = loadpicture()

barwidth = 20

barstyle = lbs_none

barstartcolor = getsyscolor(color_menu)

barendcolor = barstartcolor

selectscope = iss_icon_text

textenabledcolor = getsyscolor(color_menutext)

textdisabledcolor = getsyscolor(color_graytext)

textselectcolor = getsyscolor(color_highlighttext)

iconstyle = iis_none

edgestyle = ises_solid

edgecolor = getsyscolor(color_highlight)

fillstyle = isfs_solidcolor

fillstartcolor = edgecolor

fillendcolor = fillstartcolor

bkcolor = getsyscolor(color_menu)

sepcolor = textdisabledcolor

sepstyle = mss_default

case style_xp ' xp 风格

set barimage = loadpicture()

barwidth = 20

barstyle = lbs_none

barstartcolor = getsyscolor(color_menu)

barendcolor = barstartcolor

selectscope = iss_icon_text

textenabledcolor = getsyscolor(color_menutext)

textdisabledcolor = getsyscolor(color_graytext)

textselectcolor = textenabledcolor

iconstyle = iis_shadow

edgestyle = ises_solid

edgecolor = rgb(49, 106, 197)

fillstyle = isfs_solidcolor

fillstartcolor = rgb(180, 195, 210)

fillendcolor = fillstartcolor

bkcolor = getsyscolor(color_menu)

sepcolor = rgb(192, 192, 192)

sepstyle = mss_solid

case style_shade ' 渐变风格

set barimage = loadpicture()

barwidth = 20

barstyle = lbs_verticalcolor

barstartcolor = vbblack

barendcolor = vbwhite

selectscope = iss_icon_text

textenabledcolor = getsyscolor(color_menutext)

textdisabledcolor = getsyscolor(color_graytext)

textselectcolor = getsyscolor(color_highlighttext)

iconstyle = iis_none

edgestyle = ises_none

edgecolor = getsyscolor(color_highlight)

fillstyle = isfs_horizontalcolor

fillstartcolor = vbblack

fillendcolor = vbwhite

bkcolor = getsyscolor(color_menu)

sepcolor = textdisabledcolor

sepstyle = mss_default

case style_3d ' 3d 立体风格

set barimage = loadpicture()

barwidth = 20

barstyle = lbs_none

barstartcolor = getsyscolor(color_menu)

barendcolor = barstartcolor

selectscope = iss_text

textenabledcolor = getsyscolor(color_menutext)

textdisabledcolor = getsyscolor(color_graytext)

textselectcolor = vbblue

iconstyle = iis_raised

edgestyle = ises_sunken

edgecolor = getsyscolor(color_highlight)

fillstyle = isfs_none

fillstartcolor = edgecolor

fillendcolor = fillstartcolor

bkcolor = getsyscolor(color_menu)

sepcolor = textdisabledcolor

sepstyle = mss_default

case style_colorful ' 炫彩风格

set barimage = frmmenu.picture

barwidth = 20

barstyle = lbs_image

barstartcolor = getsyscolor(color_menu)

barendcolor = barstartcolor

selectscope = iss_icon_text

textenabledcolor = vbblue

textdisabledcolor = rgb(49, 106, 197)

textselectcolor = vbred

iconstyle = iis_none

edgestyle = ises_dot

edgecolor = vbblack

fillstyle = isfs_verticalcolor

fillstartcolor = vbyellow

fillendcolor = vbgreen

bkcolor = rgb(230, 230, 255)

sepcolor = vbmagenta

sepstyle = mss_dashdotdot

end select

end property



这个类模块中包含了各种属性和方法及关于菜单的一些枚举类型,我想强调的有以下几点:

1、在createmenu方法中用setwindowlong重新定义了frmmenu的窗口入口函数的地址,menuwndproc是标准模块中的一个函数,就是处理消息的那个函数。

2、additem这个方法是添加菜单项的,使用一个叫做myiteminfo的动态数组存储菜单项的内容,在“画”菜单项的时候要用到它。在additem方法的最后,将菜单项的ftype设置成了mft_ownerdraw,也就是物主绘图,这一步最关键,因为将菜单项设置成了owner draw,windows将不会替我们写字,不会替我们画图标,一切都由我们自己来。

3、在popupmenu方法中,调用了api函数中的trackpopupmenu,看到第6个参数了吗?将处理菜单消息的窗口设置成了frmmenu,而我们又对frmmenu进行了子类处理,一切都在我们的掌握之中。

4、记得要在class_terminate中还原frmmenu的窗口入口函数的地址,并释放和菜单相关的资源。



好了,类模块已经ok了,大家可能对这个菜单类有了更多的了解,也看到了它的属性和方法。怎么样?还算比较丰富吧。如果觉得不够丰富的话,自己加就好了,呵呵。不过,最核心的部分还不在这里,而是在那个处理消息的函数,也就是menuwndproc,它将完成复杂地“画”菜单的任务以及处理各种菜单事件。看看右边的滚动条,已经够窄了,下一篇再讨论吧。 :)



(待续)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表