none
Sendmessage - wie mache ich es richtig? RRS feed

  • Frage

  • Ich mache gerade meine ersten Gehversuche mit VB.NET (bin Experte in VBA) und hab mir ein Mini-Programm (siehe unten) zusammengebastelt, was auch so funktioniert und läuft, aber die Deklaration bzw. der DllImport bereitet mir Schwierigkeiten und ist wohl auch falsch, was ich sonst so lese, z.B. bei http://www.pinvoke.net/default.aspx/user32/SendMessage.html

    Da steht u.a. "2) NEVER use "int" or "integer" as lParam." aber wenn ich den als IntPtr deklariere, wie kriege ich dann eine einfache Konstante in den Aufruf ohne das es einen RTE gibt?

    Bezüglich GetAsyncKeyState hab ich den Krams einfach mal aus VBA rüberkopiert und war erstaunt das es scheinbar auch ohne den (aufwendigen) Dllimport geht... wieso??

    Kann mir da mal jemand auf die Sprünge helfen wie es richtig gemacht wird? Wie kriege ich eine Typkonvertierung in VB.NET hin?

    In VBA war das nie ein Problem, da ging das alles ganz easy, aber VB.NET ist da etwas nervig.

    Andreas.

    Imports System.Runtime.InteropServices

    Module Module1

      <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
      Private 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 Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
      End Function

      Private Const BM_CLICK = &HF5
      Private Const BM_GETCHECK = &HF0
      Private Const BM_SETCHECK = &HF1
      Private Const BST_UNCHECKED = &H0
      Private Const BST_CHECKED = &H1

      Property Checked(ByVal hWnd As IntPtr) As Boolean
        Get
          Checked = SendMessage(hWnd, BM_GETCHECK, 0, 0)
        End Get
        Set(Value As Boolean)
          SendMessage(hWnd, BM_SETCHECK, IIf(Value, BST_CHECKED, BST_UNCHECKED), 0)
        End Set
      End Property


      Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Long

      Function ShiftPressed() As Boolean
        'True wenn die Shift-Taste gedrückt ist
        Const VK_SHIFT = &H10 ' Shift Taste
        ShiftPressed = GetAsyncKeyState(VK_SHIFT) And &H8000 <> 0
      End Function

      Function CtrlPressed() As Boolean
        'True wenn die Strg-Taste gedrückt ist
        Const VK_CONTROL = &H11 ' STRG Taste
        CtrlPressed = GetAsyncKeyState(VK_CONTROL) And &H8000 <> 0
      End Function

      Function AltPressed() As Boolean
        'True wenn die Alt-Taste gedrückt ist
        Const VK_MENU = &H12 ' Alt Taste
        AltPressed = GetAsyncKeyState(VK_MENU) And &H8000 <> 0
      End Function


      Sub Main()
        Dim hWndMain As IntPtr, hWnd As IntPtr

        If My.Application.CommandLineArgs.Count = 0 Then
          MsgBox("Aufruf: FilterOnOff hWnd")
          End
        End If

        hWndMain = My.Application.CommandLineArgs(0)
        hWnd = FindWindowEx(hWndMain, 0, "#32770", "Filter")
        If hWnd = 0 Then
          MsgBox("Childwindow '#32770 - Filter' nicht gefunden", MsgBoxStyle.Critical)
          End
        End If
        hWnd = FindWindowEx(hWnd, 0, "Button", "Filter eingeschaltet")
        If hWnd = 0 Then
          MsgBox("Childwindow 'Button - Filter eingeschaltet' nicht gefunden", MsgBoxStyle.Critical)
          End
        End If

        If AltPressed() Then
          'Filter immmer an setzen
          Checked(hWnd) = True
        ElseIf CtrlPressed() Then
          'Filter toggeln
          Checked(hWnd) = Not Checked(hWnd)
          'Fenster zu
          hWnd = FindWindowEx(hWndMain, 0, "Button", "Ok")
          If hWnd = 0 Then
            MsgBox("Childwindow 'Button - Ok' nicht gefunden", MsgBoxStyle.Critical)
            End
          End If
          SendMessage(hWnd, BM_CLICK, 0, 0)
        End If
      End Sub

    End Module

    Freitag, 11. Oktober 2013 07:34