Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1848to1852
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

InputBox in Vordergrund

InputBox in Vordergrund
11.10.2021 10:04:51
flo4711
Gibt es bei einer Inputbox die Möglichkeit das erscheinen im Vordergrund zu erzwingen?
Bei einer Msgbox geht das z.B. über den Parameter vbSytemModal ... kann ich aber bei der Inpubox nicht mitgeben.
Gruß
Gode

4
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: InputBox in Vordergrund
11.10.2021 11:20:10
volti
Hallo Gode,
falls niemand eine andere Lösung hat, hier mal eine Lösungsidee über die API.
Teste mal....
Code:

[Cc][+][-]

Option Explicit Private Declare PtrSafe Function KillTimer Lib "user32" ( _ ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long Private Declare PtrSafe Function SetTimer Lib "user32" ( _ ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, _ ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr Private Declare PtrSafe Function FindWindowA Lib "user32" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As LongPtr Private Declare PtrSafe Function SetWindowPos Lib "user32" ( _ ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, _ ByVal x As Long, ByVal y As Long, ByVal cx As Long, _ ByVal cy As Long, ByVal wFlags As Long) As Long Private Const HWND_TOPMOST = -1 Dim hTimer As LongPtr Dim sInputBoxCaption As String Sub ClickOk() Dim sText As String On Error GoTo Fehler sInputBoxCaption = "Meine Inputbox" hTimer = SetTimer(0&, 0&, 50, AddressOf DlgInputBox) sText = InputBox("Mein Default", sInputBoxCaption) Fehler: KillTimer 0&, hTimer End Sub Private Sub DlgInputBox() ' Setzt die Dg dauerhaft in den Vordergrund Dim hDlg As LongPtr hDlg = FindWindowA("#32770", sInputBoxCaption) If hDlg > 0 Then KillTimer 0&, hTimer SetWindowPos hDlg, HWND_TOPMOST, 0, 0, 0, 0, &H3 End If End Sub

_________________________
viele Grüße aus Freigericht 😊
Karl-Heinz

Anzeige
AW: InputBox in Vordergrund
11.10.2021 11:25:30
volti
Kleine Schönheitsanpassung...
Code:

[Cc][+][-]

Option Explicit Private Declare PtrSafe Function KillTimer Lib "user32" ( _ ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long Private Declare PtrSafe Function SetTimer Lib "user32" ( _ ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, _ ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr Private Declare PtrSafe Function FindWindowA Lib "user32" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As LongPtr Private Declare PtrSafe Function SetWindowPos Lib "user32" ( _ ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, _ ByVal x As Long, ByVal y As Long, ByVal cx As Long, _ ByVal cy As Long, ByVal wFlags As Long) As Long Private Const HWND_TOPMOST = -1 Dim hTimer As LongPtr Dim sInputBoxCaption As String Sub Inpuboxtest() Dim sText As String On Error GoTo Fehler sInputBoxCaption = "Meine Inputbox" hTimer = SetTimer(0&, 0&, 50, AddressOf DlgInputBoxProc) sText = InputBox("Mein Default", sInputBoxCaption) Fehler: KillTimer 0&, hTimer End Sub Private Sub DlgInputBoxProc() ' Setzt die Dlg dauerhaft in den Vordergrund Dim hDlg As LongPtr hDlg = FindWindowA("#32770", sInputBoxCaption) If hDlg > 0 Then KillTimer 0&, hTimer SetWindowPos hDlg, HWND_TOPMOST, 0, 0, 0, 0, &H3 End If End Sub

_________________________
viele Grüße aus Freigericht 😊
Karl-Heinz

Anzeige
AW: InputBox in Vordergrund
11.10.2021 12:11:14
flo4711
Hallo Karl-Heinz,
vielen Dank für deine Rückmeldung und deinen Beispiel-Code!
Ohne zu wissen was da passiert ... es hat auf alle Fälle funktioniert! ;-)
Aber irgendwie kommt's mir vor, als ob ich mit einer Kanone auf eine einzelne Ameise schieße ;-))
Eine Rückfrage hätte ich allerdings noch.
Wenn ich anstatt der Fehlerabhandlung mit "Resume next ... On error Goto 0" den Fehler ignoriere und der Fehler würde auftreten, wäre lediglich die Folge, dass das Fenster nicht in den Vordergrund kommen würde?

Sub ClickOk()
Dim sText  As String
On Error Resume next
sInputBoxCaption = "Meine Inputbox"
hTimer = SetTimer(0&, 0&, 50, AddressOf DlgInputBox)
sText = InputBox("Mein Default", sInputBoxCaption)
On Error Goto 0 
Gruß
Gode
Anzeige
AW: InputBox in Vordergrund
11.10.2021 12:31:53
volti
Hallo Gode,
es passiert folgendes:
Da mit Aufruf der Inputbox der Code ja nicht weiterläuft, wird ein Timer gesetzt, der periodisch alle 50ms eine weitere Sub anspringt.
In dieser wird geprüft, ob die Inputbox schon aktiv ist und diese dann entsprechend in den Vordergrund gestellt.
Anschließend wird der Timer beendet.
Der Fehlerabfang soll sicherstellen, dass auf jeden Fall der Timer beendet wird, da dieser nicht zu Excel sondern zu Windows gehört und ansonsten gnadenlos weiterläuft.
Mit On Error Resume Next sollte das auch klappen, evtl. kann man auch ganz auf den Fehlercode verzichten, da der Timer ja schon bei Aktivierung der Dlg in der Timer-Sub beendet wird.
Gruß
Karl-Heinz
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige