From ba53fd0b5a23d5058a4f15e8877d63b414f1a3f5 Mon Sep 17 00:00:00 2001 From: "BitBase [ErgoZ] Riftbit" Date: Sun, 11 Aug 2019 21:25:57 +0300 Subject: [PATCH] resolve #22 User32 - InsertMenuItemW --- constants.go | 35 +++++++++++++++++++++++++++++++++++ typedef.go | 15 +++++++++++++++ user32.go | 19 ++++++++++++++++++- utils.go | 9 +++++++-- 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/constants.go b/constants.go index ebe26e2..a32f0a3 100644 --- a/constants.go +++ b/constants.go @@ -3162,3 +3162,38 @@ const ( MNS_NOCHECK = 0x80000000 MNS_NOTIFYBYPOS = 0x08000000 ) + +const ( + MIIM_BITMAP = 0x00000080 + MIIM_CHECKMARKS = 0x00000008 + MIIM_DATA = 0x00000020 + MIIM_FTYPE = 0x00000100 + MIIM_ID = 0x00000002 + MIIM_STATE = 0x00000001 + MIIM_STRING = 0x00000040 + MIIM_SUBMENU = 0x00000004 + MIIM_TYPE = 0x00000010 +) + +const ( + MFT_BITMAP = 0x00000004 + MFT_MENUBARBREAK = 0x00000020 + MFT_MENUBREAK = 0x00000040 + MFT_OWNERDRAW = 0x00000100 + MFT_RADIOCHECK = 0x00000200 + MFT_RIGHTJUSTIFY = 0x00004000 + MFT_RIGHTORDER = 0x00002000 + MFT_SEPARATOR = 0x00000800 + MFT_STRING = 0x00000000 +) + +const ( + MFS_CHECKED = 0x00000008 + MFS_DEFAULT = 0x00001000 + MFS_DISABLED = 0x00000003 + MFS_ENABLED = 0x00000000 + MFS_GRAYED = 0x00000003 + MFS_HILITE = 0x00000080 + MFS_UNCHECKED = 0x00000000 + MFS_UNHILITE = 0x00000000 +) diff --git a/typedef.go b/typedef.go index 92530da..0863cf9 100644 --- a/typedef.go +++ b/typedef.go @@ -1381,3 +1381,18 @@ type MENUINFO struct { ContextHelpID DWORD MenuData uintptr } + +type MENUITEMINFOW struct { + Size uint32 + Mask uint32 + Type uint32 + State uint32 + ID uint32 + hSubMenu HMENU + hbmpChecked HBITMAP + hbmpUnchecked HBITMAP + ItemData uintptr + TypeData *uint16 + cch uint32 + hbmpItem HBITMAP +} diff --git a/user32.go b/user32.go index fccd3ca..cab7bf4 100644 --- a/user32.go +++ b/user32.go @@ -153,6 +153,7 @@ var ( procSetMenuItemBitmaps = moduser32.NewProc("SetMenuItemBitmaps") procLoadImageW = moduser32.NewProc("LoadImageW") procSetMenuInfo = moduser32.NewProc("SetMenuInfo") + procInsertMenuItemW = moduser32.NewProc("InsertMenuItemW") ) func SendMessageTimeout(hwnd HWND, msg uint32, wParam, lParam uintptr, fuFlags, uTimeout uint32, lpdwResult uintptr) uintptr { @@ -1406,7 +1407,7 @@ func SetMenuItemBitmaps(hMenu HMENU, uPosition uint32, uFlags uint32, hBitmapUnc // LoadImageW Loads an icon, cursor, animated cursor, or bitmap. // See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadimagew -func LoadImageW(hInst HINSTANCE, name string, ltype uint32, cx int, cy int, fuLoad uint32) (HANDLE, error) { +func LoadImage(hInst HINSTANCE, name string, ltype uint32, cx int, cy int, fuLoad uint32) (HANDLE, error) { ret, _, err := procLoadImageW.Call( uintptr(hInst), uintptr(pointerStringWithoutError(name)), @@ -1434,3 +1435,19 @@ func SetMenuInfo(hMenu HMENU, menuInfo MENUINFO) (bool, error) { return ret != 0, nil } + +// InsertMenuItemW Inserts a new menu item at the specified position in a menu. +// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-insertmenuitemw +func InsertMenuItem(hMenu HMENU, item uint32, fByPosition bool, lpmi MENUITEMINFOW) (bool, error) { + ret, _, err := procInsertMenuItemW.Call( + uintptr(hMenu), + uintptr(item), + uintptr(BoolToBOOL(fByPosition)), + uintptr(unsafe.Pointer(&lpmi)), + ) + if !IsErrSuccess(err) { + return false, err + } + + return ret != 0, nil +} diff --git a/utils.go b/utils.go index 871c0e2..1a2f62f 100644 --- a/utils.go +++ b/utils.go @@ -242,12 +242,17 @@ func IsErrSuccess(err error) bool { return false } -func pointerStringWithError(data string) (unsafe.Pointer, error) { +func pointerString(data string) (unsafe.Pointer, error) { pp, err := syscall.UTF16PtrFromString(data) return unsafe.Pointer(pp), err } func pointerStringWithoutError(data string) unsafe.Pointer { pp, _ := syscall.UTF16PtrFromString(data) - return unsafe.Pointer(pp) + return pp +} + +func StringToPointerWithoutError(data string) *uint16 { + pp, _ := syscall.UTF16PtrFromString(data) + return pp }