明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

API更改系統(tǒng)菜單條目

[摘要]一個運用API 更改系統(tǒng)菜單,復(fù)制系統(tǒng)菜單的例子。(需要注意的是:如果要響應(yīng)菜單項的單擊事件,可能要用到鉤子函數(shù)和更多的代碼)Option ExplicitPrivate Declare Function TrackPopupMenu Lib "user32" (ByVal h...
一個運用API 更改系統(tǒng)菜單,復(fù)制系統(tǒng)菜單的例子。(需要注意的是:如果要響應(yīng)菜單項的單擊事件,可能要用到鉤子函數(shù)和更多的代碼)

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, ByVal lprc As Any) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private 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
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Const TPM_LEFTALIGN = &H0&
Private hsysmenu As Long
Private Type POINTAPI
    x As Long
    y As Long
End Type




Function num(i As Integer) As String
num = Mid("    長恨歌 漢皇重色思傾國,御宇多年求不得。楊家有女初長成,養(yǎng)在深閨人未識。天生麗質(zhì)難自棄,一朝選在君王側(cè);仨恍Π倜纳鶎m粉黛無顏色。春寒賜浴華清池,溫泉水滑洗凝脂。侍兒扶起嬌無力,始是新承恩澤時。云鬢花顏金步搖,芙蓉帳暖度春宵。春宵苦短日高起,從此君王不早朝。", 8 * i + 1, 8)
End Function




Private Sub Form_Load()
Dim i As Integer
hsysmenu = GetSystemMenu(hwnd, False) '系統(tǒng)菜單句柄

InsertMenu hsysmenu, 0, &H800, &H0, "" '加入分割符
For i = 0 To 5
ModifyMenu hsysmenu, i, &H4410, 0, num(i+1) '改變菜單條目
Next

For i = 6 To 15

InsertMenu hsysmenu, i, &H400, &H0, num(i+1)   '插入菜單條目
Next 'i=6 對應(yīng)條目為關(guān)閉按鈕,如此設(shè)置是為了留下該條目


InsertMenu hsysmenu, 0, &H800, &H0, "" '加入分割符
InsertMenu hsysmenu, 0, &H400, &H0, num(0)

Me.Move Screen.Width / 2 - 3000, Screen.Height / 2 - 2000, 6000, 4000 ' 窗體居中
Me.Caption = "請用鼠標(biāo)右鍵調(diào)出系統(tǒng)菜單!!"
End Sub




Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim Pts As POINTAPI
GetCursorPos Pts '獲取鼠標(biāo)位置
    If Button = 2 Then TrackPopupMenu hsysmenu, TPM_LEFTALIGN, Pts.x, Pts.y, 0, Me.hwnd, ByVal 0&
'在鼠標(biāo)點擊處彈出更改后的系統(tǒng)菜單
End Sub