創(chuàng)建位圖菜單
發(fā)表時(shí)間:2023-08-03 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]----創(chuàng)建位圖菜單其實(shí)非常簡(jiǎn)單,它需要用到Windows應(yīng)用程序編程接口(API)的一些菜單函數(shù)和位圖函數(shù),你需要將這些函數(shù)的聲明包含在你的應(yīng)用程序的標(biāo)準(zhǔn)模塊中,具體的內(nèi)容請(qǐng)參見(jiàn)樣例程序。步驟如下...
----創(chuàng)建位圖菜單其實(shí)非常簡(jiǎn)單,它需要用到Windows應(yīng)用程序編程接口(API)的
一些菜單函數(shù)和位圖函數(shù),你需要將這些函數(shù)的聲明包含在你的應(yīng)用程序的標(biāo)準(zhǔn)模
塊中,具體的內(nèi)容請(qǐng)參見(jiàn)樣例程序。步驟如下:
使用函數(shù)GetSubMenu來(lái)提取子菜單項(xiàng)的句柄,并通過(guò)使用函數(shù)CreateCompatibleDC
來(lái)創(chuàng)建一個(gè)兼容的設(shè)備環(huán)境描述表;
在一個(gè)循環(huán)過(guò)程中通過(guò)使用CreateCompatibleBitmap函數(shù),SelectObject函數(shù)以及
BitBlt函數(shù)來(lái)分別將針對(duì)各個(gè)菜單項(xiàng)所載入的位圖選入到兼容設(shè)備環(huán)境中;
通過(guò)ModifyMenu函數(shù)繪制真正的位圖菜單選項(xiàng);
使用DeleteDC函數(shù)來(lái)釋放設(shè)備環(huán)境,以便其他的程序可以使用它們。
----提取位圖可以有多種方法,在本樣例程序中在窗體上設(shè)置了四個(gè)圖形框控件,
使用它們載入4個(gè)預(yù)設(shè)的圖標(biāo)來(lái)作為菜單選項(xiàng)位圖的源文件,當(dāng)然你也可以使用其
他的方法,例如使用LoadPicture函數(shù)來(lái)從磁盤(pán)裝載位圖。
樣例程序
在Visual Basic中開(kāi)始一個(gè)新的工程,采用缺省的方法建立Form1。
創(chuàng)建一個(gè)新的模塊,采用缺省的方法建立Module1.Bas。
將如下的聲明語(yǔ)句和常量添加到Module1.Bas模塊中:
Option Explicit
Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long,
ByVal nPos As Long) As Long
Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long,
ByVal nPos As Long) As Long
Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA"
(ByVal hMenu As Long,ByVal nPosition As Long, ByVal wFlags As Long,
ByVal wIDNewItem As Long, ByVal lpString As Any) As Long
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As
Long
Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function CreateCompatibleBitmap Lib "gdi32"
(ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long,
ByVal hObject As Long) As Long
Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As
Long,
ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal
hSrcDC
As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As
Long
Public Const SRCCOPY = &HCC0020
Public Const MF_BYPOSITION = &H400&
Public Const MF_BITMAP = &H4&
注意上面的聲明語(yǔ)句需要書(shū)寫(xiě)在一行內(nèi)。
在Form1上添加4個(gè)圖形框控件,將它們的Name屬性設(shè)置為Picture1,將它們的
Index屬性依次設(shè)置為0,1,2,3,將它們的AutoRedrew屬性設(shè)置為T(mén)rue,將它們
的AutoResize屬性設(shè)置為T(mén)ure,以及將它們的Visable屬性設(shè)置為False。
將上面的4個(gè)圖形框控件的Picture屬性依次設(shè)置為Face1.ico,F(xiàn)ace2.ico,F(xiàn)ace3.
ico,F(xiàn)ace4.ico。
在Form1上添加第一個(gè)菜單項(xiàng),將它的標(biāo)題設(shè)置為“[&F]文件”,名稱(chēng)設(shè)置為
mnuFile。在其下添加一個(gè)子菜單項(xiàng),將它的標(biāo)題設(shè)置為“[&E]退出”,名稱(chēng)設(shè)置
為mnuExit。
在Form1上添加第二個(gè)菜單項(xiàng),將它的標(biāo)題設(shè)置為“[&A]臉譜”,名稱(chēng)設(shè)置為
mnuFace。在其下添加4個(gè)子菜單項(xiàng),分別將改4個(gè)子菜單項(xiàng)的名稱(chēng)設(shè)置為“[N]正!
,“[&S]微笑”,“ [&L]大笑”,以及“[&O]悲傷”。將它們的名稱(chēng)設(shè)置為“
mnuFaceSel”,并相應(yīng)將這4個(gè)子菜單項(xiàng)的索引設(shè)置為0,1,2,3。
將如下的代碼添加到Form1的Form_Load事件中:
Private Sub Form_Load()
Dim nLoopCtr As Integer
Dim lResult As Long
Dim hTempDC As Long
Dim nWidth As Integer
Dim nHeight As Integer
Dim lTempID As Long
Dim hMenuID As Long
Dim lItemCount As Long
Dim hBitmap As Long
nWidth = Picture1(nLoopCtr).Width \ Screen.TwipsPerPixelX
nHeight = Picture1(nLoopCtr).Height \ Screen.TwipsPerPixelY
hMenuID = GetSubMenu(GetMenu((Me.hwnd)), 1)
hTempDC = CreateCompatibleDC(Picture1(nLoopCtr).hdc)
For nLoopCtr = 0 To 3
hBitmap = CreateCompatibleBitmap(Picture1(nLoopCtr).hdc, nWidth,
nHeight)
lTempID = SelectObject(hTempDC, hBitmap)
lResult = BitBlt(hTempDC, 0, 0, nWidth, nHeight, (Picture1(
nLoopCtr).hdc), 0, 0, SRCCOPY)
lTempID = SelectObject(hTempDC, lTempID)
nuFaceSel(nLoopCtr).Caption = ""
lResult = ModifyMenu(hMenuID, nLoopCtr, MF_BYPOSITION Or
MF_BITMAP,
GetMenuItemID(hMenuID, nLoopCtr), hBitmap)
Next nLoopCtr
lResult = DeleteDC(hTempDC)
End Sub
將如下的代碼添加到“退出”子菜單的單擊事件中:
Private Sub mnuExit_Click(Index As Integer)
Select Case Index
Case 0
Unload Me
End Select
End Sub
運(yùn)行該樣例程序,單擊“臉譜”菜單,則會(huì)看到由4個(gè)臉譜圖標(biāo)所形成的位圖子菜
單項(xiàng),單擊“文件”\“退出”菜單可退出應(yīng)用程序。