Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Can`t get text using GetMenuItemInfo

$
0
0
Hi Everyone,
I need to get the menu text from an external window. I can get the Enabled/Disabled state and the Checked/Unchecked state without a problem but, when it comes to the text all i can get is something like this (並睥䌉牴⭬). I have been all over the net looking for answers and have tried everything i can think of with no success. The way i set the string buffer size may be the problem but,i am not sure. It works with the GetMenuString API but, that is obsolete. It seems to get the text length ok but, not the text. This is an example i made so anyone can test it by opening up notepad and running this. I am hoping i am just missing something stupid. Does anyone have any idea of what it could be ?

Module1.vb
Imports System.Runtime.InteropServices

Module Module1
    Public Const MFT_STRING = &H0
    Public Const MIIM_TYPE = &H10

    <DllImport("user32.dll")> Public Function GetMenu(ByVal hWnd As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll")> Public Function GetSubMenu(ByVal hMenu As IntPtr, ByVal nPos As Integer) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Public Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    Public Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (ByVal hMenu As IntPtr, ByVal unit As Integer, ByVal byPosition As Boolean, ByRef lpMenuItemInfo As MENUITEMINFO) As Int32

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    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
    End Structure
End Module



Form1.vb
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim hWnd As IntPtr = FindWindow(Nothing, "untitled - notepad")
        Dim hSubMenu As IntPtr = GetSubMenu(GetMenu(hWnd), 0)

        Dim menuinfo As New MENUITEMINFO
        menuinfo.cbSize = Len(menuinfo)
        menuinfo.fMask = MIIM_TYPE
        menuinfo.fType = MFT_STRING
        menuinfo.cch = 0
        menuinfo.dwTypeData = ""

        '1st call to get the text length into (cch)
        GetMenuItemInfo(hSubMenu, 0, True, menuinfo)
        MessageBox.Show(menuinfo.cch.ToString)

        'Set the length of the buffer to cch + 1
        menuinfo.dwTypeData = Space(menuinfo.cch + 1)

        '2nd call to get the text into (dwTypeData)
        GetMenuItemInfo(hSubMenu, 0, True, menuinfo)

        MessageBox.Show(menuinfo.dwTypeData)
    End Sub

End Class


Viewing all articles
Browse latest Browse all 51036

Trending Articles