Forms System Menu..? [done] fell like i've been talking to myself in here
How would I go about appending an item to a window forms system menu. In VB6 I would do this with the Win32 API does anyone know if it is easier now or if I will still need to bust out the API?
Last edited by Magiaus; Apr 11th, 2002 at 05:45 PM.
the menu i am speaking of is the one that comes up when you click the form's icom or it's taskbar button with Minimize, maximixe, restore and close in it
i am convering the code right now i will post it in about an hr if my son will let me work...
the code generates an exeption it is being cause by MENUITEMINFO but i am unsure of how to fix it
module
Code:
Public Module Magiaus
Public Declare Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As Long, ByVal bRevert As Integer) As Integer
Public Declare Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As Integer) As Integer
Public Declare Function InsertMenuItem Lib "user32.dll" Alias "InsertMenuItemA" (ByVal hMenu As Integer, ByVal uItem As Integer, ByVal fByPosition As Integer, lpmii As MENUITEMINFO) As Integer
Public Declare Function SetMenuItemInfo Lib "user32.dll" Alias "SetMenuItemInfoA" (ByVal hMenu As Integer, ByVal uItem As Integer, ByVal fByPosition As Integer, lpmii As MENUITEMINFO) As Integer
Public Structure MENUITEMINFO
Public cbSize As Integer
Public fMask As Integer
Public fType As Integer
Public fState As Integer
Public wID As Integer
Public hSubMenu As Integer
Public hbmpChecked As Integer
Public hbmpUnchecked As Integer
Public dwItemData As Integer
Public dwTypeData As String
Public cch As Integer
Public Sub New(cbSize As Integer, fMask As Integer, fType As Integer, fState As Integer, wID As Integer, hSubMenu As Integer, hbmpChecked As Integer, hbmpUnchecked As Integer, dwItemData As Integer, dwTypeData As String, cch As Integer )
Me.cbSize = cbSize
Me.fMask = fMask
Me.fType = fType
Me.fState = fState
Me.wID = wID
Me.hSubMenu = hSubMenu
Me.hbmpChecked = hbmpChecked
Me.hbmpUnchecked = hbmpUnchecked
Me.dwItemData = dwItemData
Me.dwTypeData = dwTypeData
Me.cch = cch
End Sub
End Structure
Public Const MIIM_STATE As Integer = &H1
Public Const MIIM_ID As Integer = &H2
Public Const MIIM_TYPE As Integer = &H10
Public Const MFT_SEPARATOR As Integer = &H800
Public Const MFT_STRING As Integer = &H0
Public Const MFS_ENABLED As Integer = &H0
Public Const MFS_CHECKED As Integer = &H8
End Module
form
Code:
Option Explicit On
Option Strict On
Option Compare Binary
Imports Microsoft.VisualBasic
Imports System
Imports System.Windows.Forms
Public Class SysMenu : Inherits Form
<STAThread()> _
Shared Sub Main()
System.Windows.Forms.Application.Run(New SysMenu())
End Sub
Public Sub New()
MyBase.New()
Dim hMenu As Integer = GetSystemMenu(Me.Handle.ToInt32, 0)
Dim ic As Integer = GetMenuItemCount(hMenu)
Dim mii As MENUITEMINFO
Dim iRet As Integer = 0
With mii
.cbsize = Len(mii)
.fMask = MIIM_ID Or MIIM_TYPE
.fType = MFT_SEPARATOR
.wID = 0
End With
iRet = InsertMenuItem(hMenu, ic, 1, mii) 'Add a seperator
With mii
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
.fType = MFT_STRING
.fState = MFS_ENABLED
.wID = 1
.dwTypeData = "&New Menu Item"
.cch = Len(.dwTypeData)
End With
iRet = InsertMenuItem(hMenu, ic + 1, 2, mii)
Me.Text = "Click Icon"
End Sub
End Class
I fixed it. Now all I have to do is add the MsgProc but AHHHHHHHHHHHHHH I want an inline debugger @#$% notepad
it wasn't even the Struct it was a error on my part.
ok so you guys who didn't know what i meant give this a run and click the form icon or its taskbar button
VB Code:
Imports System.Runtime.InteropServices
Public Module Magiaus
Public Declare Ansi Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As Integer, ByVal bRevert As Integer) As Integer
Public Declare Ansi Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As Integer) As Integer
Public Declare Ansi Function InsertMenuItem Lib "user32.dll" Alias "InsertMenuItemA" (ByVal hMenu As Integer, ByVal uItem As Integer, ByVal fByPosition As Integer,ByRef lpmii As MENUITEMINFO) As Integer
Public Declare Ansi Function SetMenuItemInfo Lib "user32.dll" Alias "SetMenuItemInfoA" (ByVal hMenu As Integer, ByVal uItem As Integer, ByVal fByPosition As Integer,ByRef lpmii As MENUITEMINFO) As Integer
The finished product. Complete with WindowProc. say ewww subclassing really weak subclassing
module:
VB Code:
Imports System.Runtime.InteropServices
Public Module Magiaus
Public Declare Ansi Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As Integer, ByVal bRevert As Integer) As Integer
Public Declare Ansi Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As Integer) As Integer
Public Declare Ansi Function InsertMenuItem Lib "user32.dll" Alias "InsertMenuItemA" (ByVal hMenu As Integer, ByVal uItem As Integer, ByVal fByPosition As Integer,ByRef lpmii As MENUITEMINFO) As Integer
Public Declare Ansi Function SetMenuItemInfo Lib "user32.dll" Alias "SetMenuItemInfoA" (ByVal hMenu As Integer, ByVal uItem As Integer, ByVal fByPosition As Integer,ByRef lpmii As MENUITEMINFO) As Integer
Public Declare Ansi Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Integer, ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Declare Ansi Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hWnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As ipWindowProc) As Integer