Tipp 0048 MessageBox frei positionieren
Autor/Einsender:
Datum:
  Detlev Schubert
01.05.2001
Entwicklungsumgebung:   VB 5
In der Regel wird die Message-Box vom Betriebssystem in der Mitte des Bildschirms positioniert, und der Zugriff auf die Position der MsgBox wird von VB nicht unterstützt. Dass es auch anders geht, zeigt dieser Tipp. Unter Zuhilfenahme einiger API-Funktionen ist es möglich, die MsgBox an jeder frei wählbaren Position zu zeigen.
Code im Codebereich des Moduls
 
Option Explicit

Public Declare Function SetWindowPos Lib "user32" _
      (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
      ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
      ByVal cy As Long, ByVal wFlags As Long) As Long

Public Declare Function SetWindowsHookEx Lib "user32" _
      Alias "SetWindowsHookExA" (ByVal idHook As Long, _
      ByVal lpfn As Long, ByVal hmod As Long, ByVal _
      dwThreadId As Long) As Long

Public Declare Function UnhookWindowsHookEx Lib "user32" _
      (ByVal hHook As Long) As Long

Public Declare Function GetCurrentThreadId Lib "kernel32" () _
      As Long

Public Const SWP_NOSIZE = &H1
Public Const SWP_NOZORDER = &H4
Public Const SWP_NOACTIVATE = &H10
Public Const HCBT_ACTIVATE = 5
Public Const WH_CBT = 5

Public hHook As Long
Public posX As Long
Public posY As Long

Public Function WinProc(ByVal lMsg As Long, ByVal wParam _
      As Long, ByVal lParam As Long) As Long
  If lMsg = HCBT_ACTIVATE Then
    SetWindowPos wParam, 0, posX, posY, 0, 0, SWP_NOSIZE Or _
       SWP_NOZORDER Or SWP_NOACTIVATE
    UnhookWindowsHookEx hHook
  End If
  WinProc = False
End Function
 
Code im Codebereich der Form
 
Option Explicit

Private Sub Form_Load()
  txtPosX = 0
  txtPosY = 0
End Sub

Private Sub Command1_Click()
  Dim hInst As Long, Thread As Long
  Dim msg As String

  If Check1.Value = 1 Then
    msg$ = "Die MessageBox ist jetzt zentriert."
  Else
    hInst = App.hInstance
    Thread = GetCurrentThreadId()
    hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc, _
        hInst, Thread)
    posX = CLng(txtPosX)
    posY = CLng(txtPosY)
    msg$ = "Die Position der MessageBox ist an:" & _
        vbCrLf & vbCrLf & "X-Position: " & posX & vbCrLf & _
        "Y-Position: " & posY
  End If

  MsgBox msg$, vbOKOnly + vbInformation, _
          "Position der MessageBox", 0, 0
End Sub
 
Weitere Links zum Thema
MessageBox automatisch schließen

Windows-Version
95
98/SE
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (3 kB) Downloads bisher: [ 2228 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Montag, 29. August 2011