vb打造超酷個性化菜單(二)
其實,漂亮的界面都是“畫”出來的,菜單當然也不例外。既然是“畫”出來的,就需要有窗體來接收“畫”菜單這個消息,后面我們會看到,實際上不僅僅是“畫”這個消息,一切關于這個菜單的消息都要有一個窗體來接收。如果你對消息不太了解,可以看看網(wǎng)上其它一些關于windows消息機制的文章。不了解也沒有關系,只要會使用就可以了,后面的文章給出了完整的源代碼,而且文章的最后還給出了源代碼的下載地址。
下面我們來創(chuàng)建接收消息的窗體:打開上次建好的工程,添加一個窗體,并將其名稱設置為frmmenu(注意:這一步是必須的)。還記得上篇文章的最后一幅圖嗎?菜單左邊那個黑底色的附加條,為了方便,將frmmenu的picture屬性設置成那幅圖。到此,這個窗體就算ok了!對了,就這樣,因為這個窗體僅僅是為了處理消息和存儲那個黑底色的風格條,我們將會對它進行子類處理,處理消息的代碼全部都放在了將在下一篇中詳細介紹的標準模塊中。
接下來添加一個類模塊,并將其名稱設置為cmenu,代碼如下:
'**************************************************************************************************************
'* 本類模塊是一個菜單類, 提供了各種樣式的菜單的制作方案
'*
'* 版權(quán): 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 ' 菜單項狀態(tài)
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
' 創(chuàng)建彈出式菜單
public sub createmenu()
premenuwndproc = setwindowlong(frmmenu.hwnd, gwl_wndproc, addressof menuwndproc)
hmenu = createpopupmenu()
me.style = style_windows
end sub
' 插入菜單項并保存自定義菜單項數(shù)組, 設置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
' 將菜單項數(shù)據(jù)存入動態(tài)數(shù)組
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
' 獲得菜單項數(shù)據(jù)
with iteminfo
.cbsize = lenb(iteminfo)
.fmask = miim_data or miim_id or miim_type
end with
getmenuiteminfo hmenu, id, false, iteminfo
' 設置菜單項數(shù)據(jù)
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
' 設置菜單項狀態(tài)
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
' 獲得菜單項狀態(tài)
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
這個類模塊中包含了各種屬性和方法及關于菜單的一些枚舉類型,我想強調(diào)的有以下幾點:
1、在createmenu方法中用setwindowlong重新定義了frmmenu的窗口入口函數(shù)的地址,menuwndproc是標準模塊中的一個函數(shù),就是處理消息的那個函數(shù)。
2、additem這個方法是添加菜單項的,使用一個叫做myiteminfo的動態(tài)數(shù)組存儲菜單項的內(nèi)容,在“畫”菜單項的時候要用到它。在additem方法的最后,將菜單項的ftype設置成了mft_ownerdraw,也就是物主繪圖,這一步最關鍵,因為將菜單項設置成了owner draw,windows將不會替我們寫字,不會替我們畫圖標,一切都由我們自己來。
3、在popupmenu方法中,調(diào)用了api函數(shù)中的trackpopupmenu,看到第6個參數(shù)了嗎?將處理菜單消息的窗口設置成了frmmenu,而我們又對frmmenu進行了子類處理,一切都在我們的掌握之中。
4、記得要在class_terminate中還原frmmenu的窗口入口函數(shù)的地址,并釋放和菜單相關的資源。
好了,類模塊已經(jīng)ok了,大家可能對這個菜單類有了更多的了解,也看到了它的屬性和方法。怎么樣?還算比較豐富吧。如果覺得不夠豐富的話,自己加就好了,呵呵。不過,最核心的部分還不在這里,而是在那個處理消息的函數(shù),也就是menuwndproc,它將完成復雜地“畫”菜單的任務以及處理各種菜單事件。看看右邊的滾動條,已經(jīng)夠窄了,下一篇再討論吧。 :)
(待續(xù))