Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
resolve #22 User32 - InsertMenuItemW
Browse files Browse the repository at this point in the history
  • Loading branch information
ergoz committed Aug 11, 2019
1 parent 00bcd35 commit ba53fd0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
35 changes: 35 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
15 changes: 15 additions & 0 deletions typedef.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 18 additions & 1 deletion user32.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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
}
9 changes: 7 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ba53fd0

Please sign in to comment.