Fremdanwendung steuern?

  • VB.NET

Es gibt 18 Antworten in diesem Thema. Der letzte Beitrag () ist von doci.

    Fremdanwendung steuern?

    Huhu,

    ich versuche über meine Anwendung, eine zweite Anwendung(welche nicht von mir ist) zu steuern, sprich, zum Beispiel die Position des angezeigten Fensters verändern. Also ich will in "meinem" Programm jetz einfach mal als Position 0*0 angeben, und die andere Anwendung wird dann bei Buttonklick auf die Position 0*0 gesetzt.

    Tjoa, kleiner Tipp, wie ich überhaupt an die fremde Anwendung herankomme, wäre hilfreich.

    mfg doci
    Hallo Kevin,

    ich denke, das müsste auch ohne Api gehen.
    Einfach den Prozess auswerten.

    Zum setzen einer neuen Position brauchst du hingegen wieder eine Api

    SetWindowPos (Beispiel gibt es u.a. auf meiner Seite)

    Gruss

    mikeb69
    hm, i-wie klappt das nicht so recht,
    also:

    VB.NET-Quellcode

    1. Dim test = FindWindow(vbNullString, "Notepad++ - new 1")


    liefert mir als rückgabe den wert
    1956532576001263532

    soweit so gut oO

    VB.NET-Quellcode

    1. Dim test2 = SetWindowPos(test, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE)


    sollte doch dann mein fenster verschieben oder? versteh die setwindowapi noch nich so ganz
    1. parameter: das handle des zu verschiebenden fensters, also sprich im endeffekt das fenster selbst
    3/4 param.: neue position
    5/6 param.: größe

    jedoch 2 und 7sind mir noch unklar, was diese tun, bzw warum die da sind etc...also eigentlich will ich doch nur ein geöffnetes fenster in die obere linke ecke verschieben oO also 0,0
    Hallo doci,

    der erste Parameter enthält das WindowHandle (Process -> MainWindowHandle)

    Hier mal ein funktionierendes Beispiel

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Declare Function SetWindowPos Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Int32, _
    3. ByVal x As Int32, ByVal y As Int32, ByVal cx As Int32, _
    4. ByVal cy As Int32, ByVal wFlags As Int32) As Int32
    5. Const SWP_NOSIZE As Int32 = &H1
    6. Const SWP_NOMOVE As Int32 = &H2
    7. Const SWP_NOZORDER As Int32 = &H4
    8. Const SWP_NOACTIVATE As Int32 = &H10
    9. Const SWP_DRAWFRAME As Int32 = &H20
    10. Const SWP_SHOWWINDOW As Int32 = &H40
    11. Const SWP_HIDEWINDOW As Int32 = &H80
    12. Const HWND_TOPMOST As Int32 = -1 'bringt ein fenster an die oberste stelle
    13. Const HWND_NOTOPMOST As Int32 = -2 'bringt ein fenster an die unterste stelle eines containers
    14. Const HWND_BOTTOM As Int32 = 1 'bringt ein fenster an die unterste position aller fenster
    15. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    16. Process.Start("Notepad")
    17. End Sub
    18. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    19. Dim p() As Process = Process.GetProcessesByName("Notepad")
    20. If p.Length > 0 Then
    21. SetWindowPos(p(0).MainWindowHandle, Nothing, 500, 500, 0, 0, SWP_NOSIZE)
    22. End If
    23. End Sub
    24. End Class

    Gruss

    mikeb69
    joa, das beispiel hatte ich auch schon gefunden(funktioniert auch tatsächlich ;)) hihi

    wenn ich nun aber nicht mit der process klasse arbeiten kann, weil meine anwendung nicht in den prozessen auftaucht, muss ich ja die findwindow api nehmen

    VB.NET-Quellcode

    1. Dim handle = FindWindow(0, "Unbenannt - Editor")
    2. SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE) ' Or SWP_NOMOVE)


    sollte doch dann so richtig sein oder? (1. parameter bei findwindow versteh ich nich so ganz)

    jedoch sagt mir vb bei einem start immer wieder, arithmetischer überlauf bla bla
    Hallo doci,

    hier mal das Beispiel mit deiner FindWindow Api.

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Public Class Form1
    3. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    4. Private Shared Function FindWindow( _
    5. ByVal lpClassName As String, _
    6. ByVal lpWindowName As String) As IntPtr
    7. End Function
    8. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    9. Process.Start("Notepad")
    10. End Sub
    11. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    12. Dim p() As Process = Process.GetProcessesByName("Notepad")
    13. Dim h As IntPtr = FindWindow(Nothing, "Unbenannt - Editor")
    14. If p.Length > 0 Then
    15. 'SetWindowPos(p(0).MainWindowHandle, Nothing, 500, 500, 0, 0, SWP_NOSIZE)
    16. SetWindowPos(h, Nothing, 500, 500, 0, 0, SWP_NOSIZE)
    17. End If
    18. End Sub
    19. End Class

    Zitat aus dem Inet

    http://www.dotnetspider.com/forum/186813-FindWindow-vb-net.aspx schrieb:

    Notes:

    The FindWindow function retrieves the handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows.

    Works hand-in-hand with FindWindowEx

    *If lpClassName is NULL FindWindow will search for the window by the lpWindowName (window's title) only. This is useful if the class of a particular window is variable.


    Gruss

    mikeb69
    Datentypen sind IMMER wichtig... ;)

    PS: Ich empfehle dir den API-Viewer. Ist kostenlos und enthält eine riesige Auflistung mit allen API´s. Dazu gibt´s auch direkt eine VB.NET Beispieldeklaration - sodass ein Fehler praktisch unmöglich ist.