在VB中要實(shí)現(xiàn)這一功能還是比較方便的,下面舉例說(shuō)明。
實(shí)現(xiàn)菜單分割這一功能需要使用API函數(shù),思路是:首先獲得菜單句柄,從而可得到其子菜單項(xiàng)數(shù),然后將子菜單項(xiàng)數(shù)除以2,若子菜單項(xiàng)數(shù)為奇數(shù),則將多余的一項(xiàng)劃到第一部分,然后利用API函數(shù)SetMenuItemInfo重新設(shè)定菜單顯示形式即可。
下面是一個(gè)例子。首先用菜單生成器制做一個(gè)包括9個(gè)子菜單項(xiàng)的菜單,然后,在總體聲明部分聲明用到的API函數(shù)和常數(shù),這些可以從API說(shuō)明文件中復(fù)制。
Option Explicit
′菜單信息結(jié)構(gòu)說(shuō)明
Private Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As String
cch As Long
End Type
'所需的API函數(shù)和常數(shù)
Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) _
As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, _
ByVal nPos As Long) As Long
Private Declare Function GetMenuItemInfo Lib "user32" Alias _
"GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, _
ByVal b As Boolean, lpmii As MENUITEMINFO) As Long
Private Declare Function SetMenuItemInfo Lib "user32" Alias _
"SetMenuItemInfoA" (ByVal hMenu As Long, ByVal uItem As Long, _
ByVal fByPosition As Long, lpmii As MENUITEMINFO) As Long
Const MIIM_TYPE = &H10
Const RGB_STARTNEWCOLUMNWITHVERTBAR = &H20&
Const MFT_STRING = &H0&
在窗體中添加一個(gè)命令按鈕,Caption=“分割菜單”,雙擊寫(xiě)如下代碼:
Private Sub Command1_Click()
Dim rv As Long
Dim hSubMenu As Long
Dim mnuItemCount As Long
Dim mInfo As MENUITEMINFO
Dim pad As Long
'獲取菜單項(xiàng)句柄和子菜單項(xiàng)數(shù)
hSubMenu=GetSubMenu(GetMenu(Me.hwnd), 0)
mnuItemCount=GetMenuItemCount(hSubMenu)
'將子菜單項(xiàng)分成兩部分
If mnuItemCount Mod 2<>0 Then pad=1
'取得當(dāng)前菜單信息
mInfo.cbSize=Len(mInfo)
mInfo.fMask=MIIM_TYPE
mInfo.fType=MFT_STRING
mInfo.dwTypeData=Space$(256)
mInfo.cch=Len(mInfo.dwTypeData)
rv=GetMenuItemInfo(hSubMenu, (mnuItemCount\2)+pad, True, mInfo)
'按新格式顯示菜單
mInfo.fType=RGB_STARTNEWCOLUMNWITHVERTBAR
mInfo.fMask=MIIM_TYPE
rv=SetMenuItemInfo(hSubMenu, (mnuItemCount\2)+pad, True, mInfo)
If rv Then MsgBox "分割完畢"
End Sub
運(yùn)行后按命令按鈕,將發(fā)現(xiàn)子菜單項(xiàng)分成了兩部分,左邊五項(xiàng),右邊四項(xiàng)。
程序運(yùn)行環(huán)境:中文WIN95,VB 5.0。
相關(guān)推薦:2010年9月計(jì)算機(jī)等級(jí)考試精華備考資料匯總北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |