Form (TopMost) ohne Fokus öffnen

  • VB.NET

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von Sederic Enders.

    Form (TopMost) ohne Fokus öffnen

    Hi,

    Wie kann ich meine Form öffnen (show) ohne den Fokus zu bekommen? Das ganze findet in einer DLL statt (falls das jmnd wissen will :D, sollte aber nichts ändern)

    Was ich überlegt habe:
    Spoiler anzeigen
    1. ShowWithoutActivation überschreiben (vbarchiv.net/tipps/tipp_1758-f…n-fokus-zu-geben-net.html) PROBLEM: Geht anscheinend nicht, wenn man TopMost = True hat
    2. ShowWindow (msdn.microsoft.com/en-us/libra…op/ms633548(v=vs.85).aspx) PROBLEM: Ich habs iwie nicht geschnallt. Auf jeden Fall löst das bei mir ne Exception aus, von wegen das Gleichgewicht des Stapels wurde gestört...
    Super :D
    Da hab ich wohl iwas aus vb6 erwischt oder so... Danke!

    Mein Code:
    Spoiler anzeigen
    Deklaration:

    VB.NET-Quellcode

    1. #Region "Ohne Fokus zeigen"
    2. <Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)> _
    3. Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As ShowWindowCommands) As Boolean
    4. End Function
    5. Enum ShowWindowCommands As Integer
    6. ''' <summary>
    7. ''' Hides the window and activates another window.
    8. ''' </summary>
    9. Hide = 0
    10. ''' <summary>
    11. ''' Activates and displays a window. If the window is minimized or
    12. ''' maximized, the system restores it to its original size and position.
    13. ''' An application should specify this flag when displaying the window
    14. ''' for the first time.
    15. ''' </summary>
    16. Normal = 1
    17. ''' <summary>
    18. ''' Activates the window and displays it as a minimized window.
    19. ''' </summary>
    20. ShowMinimized = 2
    21. ''' <summary>
    22. ''' Maximizes the specified window.
    23. ''' </summary>
    24. Maximize = 3
    25. ' is this the right value?
    26. ''' <summary>
    27. ''' Activates the window and displays it as a maximized window.
    28. ''' </summary>
    29. ShowMaximized = 3
    30. ''' <summary>
    31. ''' Displays a window in its most recent size and position. This value
    32. ''' is similar to Normal, except
    33. ''' the window is not actived.
    34. ''' </summary>
    35. ShowNoActivate = 4
    36. ''' <summary>
    37. ''' Activates the window and displays it in its current size and position.
    38. ''' </summary>
    39. Show = 5
    40. ''' <summary>
    41. ''' Minimizes the specified window and activates the next top-level
    42. ''' window in the Z order.
    43. ''' </summary>
    44. Minimize = 6
    45. ''' <summary>
    46. ''' Displays the window as a minimized window. This value is similar to
    47. ''' ShowMinimized, except the window is not activated.
    48. ''' </summary>
    49. ShowMinNoActive = 7
    50. ''' <summary>
    51. ''' Displays the window in its current size and position. This value is
    52. ''' similar to Show, except the window is not activated.
    53. ''' </summary>
    54. ShowNA = 8
    55. ''' <summary>
    56. ''' Activates and displays the window. If the window is minimized or
    57. ''' maximized, the system restores it to its original size and position.
    58. ''' An application should specify this flag when restoring a minimized window.
    59. ''' </summary>
    60. Restore = 9
    61. ''' <summary>
    62. ''' Sets the show state based on the SW_* value specified in the
    63. ''' STARTUPINFO structure passed to the CreateProcess function by the
    64. ''' program that started the application.
    65. ''' </summary>
    66. ShowDefault = 10
    67. ''' <summary>
    68. ''' <b>Windows 2000/XP:</b> Minimizes a window, even if the thread
    69. ''' that owns the window is not responding. This flag should only be
    70. ''' used when minimizing windows from a different thread.
    71. ''' </summary>
    72. ForceMinimize = 11
    73. End Enum
    74. #End Region

    zum Aufrufen:

    VB.NET-Quellcode

    1. Dim Fenster As New FormXY()
    2. ShowWindow(Fenster.Handle, ShowWindowCommands.ShowNA)
    3. Fenster.Show()
    @Sederic Enders:: Das geht doch auch ohne iwelche API-Funktionen:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private WithEvents frm2 As Form2
    3. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    4. If frm2 Is Nothing OrElse frm2.IsDisposed Then
    5. frm2 = New Form2
    6. End If
    7. If Not frm2.Visible Then
    8. frm2.Show(Me)
    9. End If
    10. If frm2.WindowState = FormWindowState.Minimized Then
    11. frm2.WindowState = FormWindowState.Normal
    12. End If
    13. frm2.Location = New Point(1000, 20)
    14. frm2.TopMost = True
    15. Me.Focus()
    16. End Sub
    17. End Class
    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!

    Sederic Enders schrieb:

    Der Fokus soll halt auf dem aktuellen Fenster bleiben,
    Dann mach halt

    VB.NET-Quellcode

    1. frm2.Focus()
    Wo ist das Problem?
    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!
    das es keine frm2 gibt, das den Fokus hat, bevor die Form1 erscheint:
    Ich hab ein Prog, das im "Hintergrund" läuft, also als NotifyIcon in der Taskleiste sichtbar ist. Dieses Prog reagiert auf globale HotKeys. Zu diesem Prog kann man plugIns machen und in einem dieser Plugins soll ein Fenster sich öffnen, wenn man eben einen HotKey drückt.
    DAnn ruf Me.Fokus() im Shown-Event der 2. Form auf.
    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!

    Sederic Enders schrieb:

    mit welchem erfolg?
    Nochmal:
    Wenn Du ein Problem hast, poste den Code und eine Beschreibung des Effekts.
    Code hab ich von Dir noch nicht gesehen.
    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!
    Nagut hier ein bisschen Code:

    Die Klasse, aus der dieser Ausschnitt ist, hat ein NotifyIcon, aber keine Form. Das Event HK.Pressed wird ausgelöst, wenn ein registrierter Hotkey gedrückt wurde. im PlugInDictionary sind alle PlugIns des Programms gespeichert und ihr zugehöriger Hotkey. Über den hotKey kann man also das PlugIn rausfinden und dessen Run Action ausführen:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub _Hotkeys_Pressed(ByVal sender As Object, ByVal e As HotKeys.PressedEventArgs) Handles HK.Pressed
    2. With PlugInDictionary(e.Key)
    3. .Run()
    4. End With
    5. End Sub

    Die Run Action ist im Interface für die PlugIns so definiert:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Sub Run()

    und dann endlich das PlugIn, um das es hier geht: Dieses soll eben ein kleines Statusfenster öffnen, welches aber keinen Fokus erhalten soll:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Sub Run() Implements [Interface].MHKPlugIn.Run
    2. ShowStatusForm('Hire kommen ein paar Argumente, wie z.B. der Text der auf dem Statusfenster angezeigt werden soll)
    3. End Sub

    und dann innerhalb der Statusform wird das ausgeführt:

    VB.NET-Quellcode

    1. 'Das ist erstmal, um das ganze als shared zu haben, dass man das ähnlich wie bei MassageBoxen einfach so anzeigen kann:
    2. Public Shared Sub ShowStatusForm('Eben diese Argumente)
    3. 'Hier hab ich die Möglichkeit versucht, die mir schon angeboten wurde:
    4. ShowWindow(StatusForm.Handle, ShowWindowCommands.ShowNA)
    5. 'Ein Objekt der Statusform bekommt man durch eine Property, die eine neue Stausform erstellt, wenn es noch keine gibt
    6. StatusForm.Show('Die Argumente werden einfach weitergegeben)
    7. End Sub

    und die eigentliche Show Methode:

    VB.NET-Quellcode

    1. Public Overloads Sub Show('Die ganzen Argumente)
    2. 'Hier werden jetzt endlich die Argumente an die Controls übergeben, also z.B. der Anzeigetext
    3. Me.Label1 = Text
    4. MyBase.Show()
    5. End Sub


    Ich hoff, das ist jetzt halbwegs verständlich.
    Und eigentlich hab ich ja schon die Lösung. Das mit ShowWindowState läuft ja eigentlich, wie es soll...