將About加入系統選單

40Star發表於2020-04-04

在VB.net中想要實現這個也很簡單,不過需要過載視窗的WndProc過程才能處理WM_SYSCOMMAND訊息

具體程式碼:
    Private Declare Function GetSystemMenu Lib "user32" Alias "GetSystemMenu" (ByVal hwnd As Int32, ByVal bRevert As Int32) As Int32
    Private Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Int32, ByVal nPosition As Int32, ByVal wFlags As Int32, ByVal wIDNewItem As Int32, ByVal lpNewItem As String) As Int32
    Private Const MF_BYCOMMAND = &H0&
    Private Const MF_BYPOSITION = &H400&
    Private Const MF_STRING = &H0&
    Private Const MF_SEPARATOR = &H800&
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InsertMenu(GetSystemMenu(Me.Handle.ToInt32, False), 6, MF_BYPOSITION Or MF_STRING, 8888, "關於我...  :)")
        InsertMenu(GetSystemMenu(Me.Handle.ToInt32, False), 7, MF_BYPOSITION Or MF_SEPARATOR, 8889, "")
    End Sub

   
    Private Const WM_SYSCOMMAND As Integer = 274
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_SYSCOMMAND And m.WParam.ToInt32 = 8888 Then
            MsgBox(System.Environment.MachineName)
        End If
        MyBase.WndProc(m)
    End Sub

相關文章