Form in Splitcontainer laden?

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von MichaHo.

    Form in Splitcontainer laden?

    Hi,
    Kurze Frage.
    Ist es möglich eine Form (FormBorderstyle = None) in ein bestimmtes Panel eines Splitcontainers zu laden?
    Also ähnlich wie bei MdiParent = Me
    Danke Euch
    "Hier könnte Ihre Werbung stehen..."
    Ja das ist moeglich.

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Public Class Form1
    3. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    4. Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    5. End Function
    6. Private Frm As New Form2
    7. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8. SetParent(Frm.Handle, SplitContainer1.Panel2.Handle)
    9. Frm.Location = New Point(0, 0)
    10. Frm.Show()
    11. End Sub
    12. End Class
    And i think to myself... what a wonderfuL World!
    @MichaHo Fas geht mit API.SetParent():
    SetParent

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Public Class NativeMethods
    3. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    4. Public Shared Function SetParent( _
    5. ByVal hWndChild As IntPtr, _
    6. ByVal hWndNewParent As IntPtr) As IntPtr
    7. End Function
    8. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    9. Public Shared Function ShowWindow( _
    10. ByVal hWnd As IntPtr, _
    11. ByVal nCmdShow As Integer) As Boolean
    12. End Function
    13. Public Const SW_MAXIMIZE As Int32 = 3
    14. <DllImport("user32")> _
    15. Public Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, x As Integer, y As Integer, cx As Integer, cy As Integer, wFlags As Integer) As Integer
    16. End Function
    17. ' Konstanten für SetWindowPos
    18. Public Const SWP_NOSIZE As Integer = &H1
    19. Public Const SWP_NOMOVE As Integer = &H2
    20. Public Const SWP_NOZORDER As Integer = &H4
    21. Public Const SWP_NOACTIVATE As Integer = &H10
    22. Public Const SWP_DRAWFRAME As Integer = &H20
    23. Public Const SWP_SHOWWINDOW As Integer = &H40
    24. Public Const SWP_HIDEWINDOW As Integer = &H80
    25. Private HWND_TOPMOST As IntPtr = New IntPtr(-1) ' bringt ein fenster an die oberste stelle
    26. Private HWND_NOTOPMOST As IntPtr = New IntPtr(-2) ' bringt ein fenster an die unterste stelle eines containers
    27. Private HWND_BOTTOM As IntPtr = New IntPtr(1) ' bringt ein fenster an die unterste position aller fenster
    28. End Class
    Der Aufruf ist so:

    VB.NET-Quellcode

    1. ' Start des Programms und Einfügen dessen in ein eigenes Control
    2. Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    3. pr = New Process
    4. pr.StartInfo.FileName = "Notepad.exe"
    5. pr.Start()
    6. pr.WaitForInputIdle(1000)
    7. NativeMethods.SetParent(pr.MainWindowHandle, Me.Panel1.Handle)
    8. NativeMethods.ShowWindow(pr.MainWindowHandle, NativeMethods.SW_MAXIMIZE)
    9. End Sub

    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Hi @Eddy und @RodFromGermany: Beide Varianten funktionieren Bestens!
    Ich hab mich erstmal für Variante 1 von Eddy entschieden da ich nur eigene Forms behandeln muss und keine externen Forms.
    Aber die 2. Variante von Rod hab ich mir gespeichert und schaue sie mir genauer an. (scheint mir beim Aufruf weniger Code zu sein :) )
    Ich habs jetzt so gemacht:

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Public Class frmMain
    3. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    4. Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    5. End Function
    6. Private Sub btnForm_Click(sender As Object, e As EventArgs) Handles btnForm.Click
    7. Dim frm = New frmReport
    8. SetParent(frm.Handle, scLeft.Panel1.Handle)
    9. frm.Width = scLeft.Panel1.Width
    10. frm.Height = scLeft.Panel1.Height
    11. frm.Location = New Point(0, 0)
    12. frm.Show()
    13. End Sub
    14. End Class


    Danke Euch beiden

    EDIT:
    wobei die Variante von @RodFromGermany noch einiges mehr kann. Hab daher die Klasse in eine eigene Datei gepackt und meinem Projekt hinzugefügt.
    Aufruf ist dann so:

    VB.NET-Quellcode

    1. Public Class frmMain
    2. Private Sub btnForm_Click(sender As Object, e As EventArgs) Handles btnForm.Click
    3. Dim frm = New frmReport
    4. NativeMethods.SetParent(frm.Handle, Me.scRight.Panel1.Handle)
    5. NativeMethods.ShowWindow(frm.Handle, NativeMethods.SW_MAXIMIZE)
    6. End Sub
    7. End Class

    Funktioniert auch bestens

    "Hier könnte Ihre Werbung stehen..."

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „MichaHo“ ()

    Wobei du dir in diesem Fall ShowWindow sparen kannst, du kannst doch bei der Form direkt den WindowState setzen, schliesslich handlet es sich ja nicht um ein externes Window.

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Public Class Form1
    3. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    4. Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    5. End Function
    6. Private Frm As New Form2
    7. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8. SetParent(Frm.Handle, SplitContainer1.Panel2.Handle)
    9. Frm.WindowState = FormWindowState.Maximized
    10. Frm.Show()
    11. End Sub
    12. End Class

    And i think to myself... what a wonderfuL World!