2013-12-16 3 views
2

모든 양식의 시스템 메뉴에 '정보 ...'단추를 추가 할 수있는 클래스를 만들었습니다. 양식의 load 이벤트에 의해 버튼이 추가되었지만이 버튼의 클릭을 처리하려면 어떻게해야합니까? 감사. 여기에 SystemMenu 클래스입니다시스템 메뉴의 사용자 지정 단추 클릭 처리

Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    {More code....} 
    Dim SysMenu = New SystemMenu(Me) 
    {More code....} 

End Sub 

그리고 - - 여기

내가 버튼을 추가 해요 어떻게

Imports System.Windows.Forms 

Public Class SystemMenu 

    Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr 
    Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As IntPtr, ByVal uFlags As Int32, ByVal uIDNewItem As IntPtr, ByVal lpNewItem As String) As Boolean 
    Private Const MF_STRING As Integer = &H0 
    Private Const MF_SEPARATOR As Integer = &H800 

    Private m_hSysMenu As IntPtr 
    Private Property hSysMenu() As IntPtr 
     Get 
      Return Me.m_hSysMenu 
     End Get 
     Set(ByVal Value As IntPtr) 
      Me.m_hSysMenu = Value 
     End Set 
    End Property 

    '** 
    ' Constructor 
    '* 
    Protected Friend Sub New(ByRef Form As Form) 
     Me.hSysMenu = GetSystemMenu(Form.Handle, False) 
     AddAbout(Form) 
    End Sub 

    '** 
    ' Add an 'About' button to the system menu of the given form 
    '* 
    Private Sub AddAbout(ByRef Form As Form) 
     AppendMenu(Me.hSysMenu, MF_SEPARATOR, 1000, Nothing) 
     AppendMenu(Me.hSysMenu, MF_STRING, 1001, "About...") 
    End Sub 

End Class 
+0

http://www.codeproject.com/ 기사/6122/서브 클래 싱 시스템 - 시스템 메뉴 – Jeff

+0

이전에 발견했습니다. 'Imports System.Windows.Forms.NativeWindow'가 포함 된 경우에도 여전히 'Type'SubclassedSystemMenu '가 정의되지 않았습니다.'오류가 발생합니다. 감사. –

답변

1

Check this out. 여기

Private Sub sysMenu_LaunchDialog() Handles sysMenu.LaunchDialog 
    Dim f as New frmAbout 
    f.ShowDialog(Me) 
End Sub 

그리고는 SubclassedSystemMenu 클래스

Public Class SubclassedSystemMenu 
Inherits System.Windows.Forms.NativeWindow 
Implements IDisposable 

Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Int32, _ 
                ByVal bRevert As Boolean) As Int32 

Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Int32, _ 
                     ByVal wFlags As Int32, _ 
                     ByVal wIDNewItem As Int32, _ 
                     ByVal lpNewItem As String) As Int32 

Private Const MF_STRING As Int32 = &H0  ' Menu string format 
Private Const MF_SEPARATOR As Int32 = &H800 ' Menu separator 
Private Const WM_SYSCOMMAND As Int32 = &H112 ' System menu 
Private Const ID_ABOUT As Int32 = 1000  ' Our ID for the new menu item 

Private mintSystemMenu As Int32 = 0     ' Parent system menu handle 
Private mintHandle As Int32 = 0      ' Local parent window handle 
Private mstrMenuItemText As String = String.Empty ' New menu item text 

Public Event LaunchDialog() 

Public Sub New(ByVal intWindowHandle As Int32, _ 
       ByVal strMenuItemText As String) 

    Me.AssignHandle(New IntPtr(intWindowHandle)) 

    mintHandle = intWindowHandle 
    mstrMenuItemText = strMenuItemText 

    ' Retrieve the system menu handle 
    mintSystemMenu = GetSystemMenu(mintHandle, 0) 

    If AddNewSystemMenuItem() = False Then 
     Throw New Exception("Unable to add new system menu items") 
    End If 

End Sub 

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) 

    Select Case m.Msg 
     Case WM_SYSCOMMAND 

      MyBase.WndProc(m) 

      If m.WParam.ToInt32 = ID_ABOUT Then 
       If mintSystemMenu <> 0 Then 
        RaiseEvent LaunchDialog() 
       End If 
      End If 

     Case Else 
      MyBase.WndProc(m) 
    End Select 

End Sub 

Public Sub Dispose() Implements System.IDisposable.Dispose 

    If Not Me.Handle.Equals(IntPtr.Zero) Then 
     Me.ReleaseHandle() 
    End If 

End Sub 

Private Function AddNewSystemMenuItem() As Boolean 
    Try 
     ' Append the extra system menu items 
     Return AppendToSystemMenu(mintSystemMenu, mstrMenuItemText) 

    Catch ex As Exception 
     Return False 
    End Try 
End Function 

Private Function AppendToSystemMenu(ByVal intHandle As Int32, _ 
            ByVal strText As String) As Boolean 

    Try 
     ' Add the seperator menu item 
     Dim intRet As Int32 = AppendMenu(intHandle, MF_SEPARATOR, 0, String.Empty) 

     ' Add the About... menu item 
     intRet = AppendMenu(intHandle, MF_STRING, ID_ABOUT, strText) 

     If intRet = 1 Then 
      Return True 
     Else 
      Return False 
     End If 

    Catch ex As Exception 
     Return False 
    End Try 
End Function 
있는 양식을 프로젝트에 SubclassedSystemMenu.vb 파일을 추가하고 기본 양식

Private WithEvents sysMenu As SubclassedSystemMenu 

Protected Overrides Sub OnLoad(e As System.EventArgs) 
    sysMenu = New SubclassedSystemMenu(Me.Handle.ToInt32, "&About...") 
End Sub 

이를 추가 한 다음 그 LaunchDialog 이벤트에 등록하고 열

최종 클래스

+0

코드를 보내 주셔서 감사합니다. 위의 주석에서 언급했듯이 여전히 오류가 발생합니다. 'Type'SubclassedSystemMenu '가 정의되지 않았습니다 .' 감사합니다. –

+0

아 ... 그리워. 어디서 오류가 발생 했습니까? 방금 기존 winforms 프로젝트에 추가했습니다. 오류가 없습니다. – Jeff

+0

'Private WithEvents sysMenu As SubclassedSystemMenu' 및'sysMenu = New SubclassedSystemMenu ... '모두에서 오류가 발생합니다. 기사에서 필자가 말했듯이'System.Windows.Forms.NativeWindow'를 가져오고 있습니다. 내가 놓친 다른 것이 있습니까? –