samedi 30 janvier 2016

Toggle checkboxes in native Listview in external application

i am currently facing the problem of changing the state of checkboxes in an external application via .Net.

I successfully can obtain the handle of the control, and just need set the state of an element of a given index.

I found this old thread with an example in VB6, but fail to get it working in VB.net: http://ift.tt/1KisvmS

I hope somebody is able to help me out, thanks in advance :)

My Translated version which neither works, nor throws any error:

Public NotInheritable Class Win32
    Private Const LVM_FIRST As Long = &H1000
    Private Const LVIS_SELECTED = &H2
    Private Const LVM_SETITEMSTATE = (LVM_FIRST + 43)
    Private Const LVIF_STATE = &H8&
    Private Const LVIS_STATEIMAGEMASK As Long = &HF000

    <StructLayoutAttribute(LayoutKind.Sequential)> _
    Private Structure LVITEM
        Public mask As UInteger
        Public iItem As Integer
        Public iSubItem As Integer
        Public state As UInteger
        Public stateMask As UInteger
        Public pszText As IntPtr
        Public cchTextMax As Integer
        Public iImage As Integer
        Public lParam As IntPtr
    End Structure

    Public Shared Sub ListView_SetItemState(ByVal hWndLV As IntPtr, ByVal i As Long, ByVal nData As UInteger, ByVal mask As UInteger)
        Dim LVI As New LVITEM
        LVI.stateMask = mask
        LVI.state = nData
        LVI.mask = LVIF_STATE

        Dim ptrLvi As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(LVI))
        Marshal.StructureToPtr(LVI, ptrLvi, False)

        SendMessageA(hWndLV, LVM_SETITEMSTATE, New IntPtr(i), ptrLvi)
    End Sub

    Public Shared Sub ListView_SetCheckState(ByVal hWndLV As IntPtr, ByVal i As Long, ByVal fCheck As Integer)
        ListView_SetItemState(hWndLV, i, CUInt(&H1000& * (1 - fCheck)), LVIS_STATEIMAGEMASK)
    End Sub

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessageA(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function
End Class

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    Dim p As Process = System.Diagnostics.Process.GetProcessesByName("OBS")(0)
    Dim notepadHwnd As IntPtr = p.MainWindowHandle
    Dim editHwnd2 As IntPtr = Win32.FindWindowEx(notepadHwnd, IntPtr.Zero, "SysListView32", Nothing)


    Win32.ListView_SetCheckState(editHwnd2, -1, 1)
End Sub




Aucun commentaire:

Enregistrer un commentaire