| Rubrik: Oberfläche · Fenster | VB-Versionen: VB6 | 14.03.26 |
Mehrmonitorbetrieb ProgrammaufrufDer folgende Quellcode öffnet beim Mehrmonitorbetrieb das Programm immer auf dem Monitor, auf welchem sich der Mauszeiger befindet. Danke ChatGPT | ||
| Autor: | Bewertung: | Views: 270 |
| https://bt-soft.de | System: Win7, Win8, Win10, Win11 | kein Beispielprojekt |
Der folgende Quellcode öffnet beim Mehrmonitorbetrieb das Programm immer auf dem Monitor, auf welchem sich der Mauszeiger befindet. Danke ChatGPT
Code für ein Modul:
Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Type MONITORINFO cbSize As Long rcMonitor As RECT rcWork As RECT dwFlags As Long End Type Private Declare Function MonitorFromPoint Lib "user32" ( _ ByVal x As Long, ByVal y As Long, ByVal dwFlags As Long) As Long Private Declare Function GetMonitorInfo Lib "user32" Alias "GetMonitorInfoA" ( _ ByVal hMonitor As Long, lpmi As MONITORINFO) As Long Private Declare Function GetCursorPos Lib "user32" ( _ lpPoint As POINTAPI) As Long Private Type POINTAPI x As Long y As Long End Type Private Const MONITOR_DEFAULTTONEAREST = &H2
Public Sub CenterFormOnMouseMonitor(frm As Form) Dim pt As POINTAPI Dim hMon As Long Dim mi As MONITORINFO ' Mausposition holen GetCursorPos pt ' Monitor bestimmen hMon = MonitorFromPoint(pt.x, pt.y, MONITOR_DEFAULTTONEAREST) mi.cbSize = Len(mi) GetMonitorInfo hMon, mi ' Breite und Höhe des Arbeitsbereichs (ohne Taskleiste) Dim workWidth As Long Dim workHeight As Long workWidth = mi.rcWork.Right - mi.rcWork.Left workHeight = mi.rcWork.Bottom - mi.rcWork.Top ' Zentriert positionieren frm.Move _ (mi.rcWork.Left + (workWidth - frm.Width / Screen.TwipsPerPixelX) 2) * Screen.TwipsPerPixelX, _ (mi.rcWork.Top + (workHeight - frm.Height / Screen.TwipsPerPixelY) 2) * Screen.TwipsPerPixelY End Sub
Code für die Hauptform:
StartUpPosition 0 - Manuell
Private Sub Form_Activate() Static Done As Boolean If Not Done Then CenterFormOnMouseMonitor Me Done = True End If End Sub


Mehrmonitorbetrieb Programmaufruf