MSGBOX POSICIONADO
HOLA A TODO TENGO UNA DUDA COMO PUEDO HACER QUE UN MSGBOX SE ME POSICIONE EN OTRO LUGAR DE LA PANTALLA QUE NO SEA PRECISAMENTE EL CENTRO DE ESTA,HASTA DONDE SE SE PUEDE HACER CON UNA API DE WINDOWS Y SE FUERA ASI COMO SE HACE.
DESDE YA GRACIAS...
DESDE YA GRACIAS...
El codigo que te envio no es del todo mio lo saque de una de las paginas de Guillermo muy conocida NO!
bueno hay lo tiene ojala y te sirva:
Vamos a ver cómo hacerlo.
El ejemplo, tomado y "arreglado" del artículo antes mencionado, mostrará un form con dos botones, al pulsar en uno de ellos, se mostrará un MsgBox en la esquina superior izquierda y al pulsar en el segundo botón, se mostrará en el centro del formulario, independientemente de dónde esté situado el form.
Para crear el proyecto de prueba:
Crea un nuevo proyecto, añade dos commandButtons y añade un módulo BAS, ya que el AddressOf necesita que las funciones o procedimientos estén en un módulo BAS.
Añade el siguiente código al módulo BAS:
Nota: He dejado los comentarios originales en inglés, ya que no necesitan demasiada traducción... espero...
'------------------------------------------------------------------
'Ejemplo para posicionar un MsgBox (15/Jun/98)
'
'Microsoft TechNet Knowledge Base, PSS ID Number: Q180936
'HOWTO: Position a MsgBox Using a Windows Hook Procedure
'
'©Guillermo 'guille Som, 1998
'------------------------------------------------------------------
Option Explicit
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () 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 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 GetWindowRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long
Public Const GWL_HINSTANCE = (-6)
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
Function WinProc1(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If lMsg = HCBT_ACTIVATE Then
'Show the MsgBox at a fixed location (0,0)
SetWindowPos wParam, 0, 0, 0, 0, 0, _
SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
'Release the CBT hook
UnhookWindowsHookEx hHook
End If
WinProc1 = False
End Function
Function WinProc2(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim rectForm As RECT, rectMsg As RECT
Dim x As Long, y As Long
'On HCBT_ACTIVATE, show the MsgBox centered over Form1
If lMsg = HCBT_ACTIVATE Then
'Get the coordinates of the form and the message box so that
'you can determine where the center of the form is located
GetWindowRect Form1.hwnd, rectForm
GetWindowRect wParam, rectMsg
x = (rectForm.Left + (rectForm.Right - rectForm.Left) / 2) - _
((rectMsg.Right - rectMsg.Left) / 2)
y = (rectForm.Top + (rectForm.Bottom - rectForm.Top) / 2) - _
((rectMsg.Bottom - rectMsg.Top) / 2)
'Position the msgbox
SetWindowPos wParam, 0, x, y, 0, 0, _
SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
'Release the CBT hook
UnhookWindowsHookEx hHook
End If
WinProc2 = False
End Function
Este es el código que hay que añadir al formulario:
'
'------------------------------------------------------------------
'Ejemplo para posicionar un MsgBox (15/Jun/98)
'
'Microsoft TechNet Knowledge Base, PSS ID Number: Q180936
'HOWTO: Position a MsgBox Using a Windows Hook Procedure
'
'©Guillermo 'guille Som, 1998
'------------------------------------------------------------------
Option Explicit
Private Sub Command1_Click()
Dim hInst As Long
Dim Thread As Long
'Set up the CBT hook
hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc1, hInst, Thread)
'Display the message box
MsgBox "This message box has been positioned at (0,0)."
End Sub
Private Sub Command2_Click()
Dim hInst As Long
Dim Thread As Long
'Set up the CBT hook
hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc2, hInst, Thread)
'Display the message box
MsgBox "This message box is centered over Form1."
End Sub
bueno hay lo tiene ojala y te sirva:
Vamos a ver cómo hacerlo.
El ejemplo, tomado y "arreglado" del artículo antes mencionado, mostrará un form con dos botones, al pulsar en uno de ellos, se mostrará un MsgBox en la esquina superior izquierda y al pulsar en el segundo botón, se mostrará en el centro del formulario, independientemente de dónde esté situado el form.
Para crear el proyecto de prueba:
Crea un nuevo proyecto, añade dos commandButtons y añade un módulo BAS, ya que el AddressOf necesita que las funciones o procedimientos estén en un módulo BAS.
Añade el siguiente código al módulo BAS:
Nota: He dejado los comentarios originales en inglés, ya que no necesitan demasiada traducción... espero...
'------------------------------------------------------------------
'Ejemplo para posicionar un MsgBox (15/Jun/98)
'
'Microsoft TechNet Knowledge Base, PSS ID Number: Q180936
'HOWTO: Position a MsgBox Using a Windows Hook Procedure
'
'©Guillermo 'guille Som, 1998
'------------------------------------------------------------------
Option Explicit
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () 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 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 GetWindowRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long
Public Const GWL_HINSTANCE = (-6)
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
Function WinProc1(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If lMsg = HCBT_ACTIVATE Then
'Show the MsgBox at a fixed location (0,0)
SetWindowPos wParam, 0, 0, 0, 0, 0, _
SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
'Release the CBT hook
UnhookWindowsHookEx hHook
End If
WinProc1 = False
End Function
Function WinProc2(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim rectForm As RECT, rectMsg As RECT
Dim x As Long, y As Long
'On HCBT_ACTIVATE, show the MsgBox centered over Form1
If lMsg = HCBT_ACTIVATE Then
'Get the coordinates of the form and the message box so that
'you can determine where the center of the form is located
GetWindowRect Form1.hwnd, rectForm
GetWindowRect wParam, rectMsg
x = (rectForm.Left + (rectForm.Right - rectForm.Left) / 2) - _
((rectMsg.Right - rectMsg.Left) / 2)
y = (rectForm.Top + (rectForm.Bottom - rectForm.Top) / 2) - _
((rectMsg.Bottom - rectMsg.Top) / 2)
'Position the msgbox
SetWindowPos wParam, 0, x, y, 0, 0, _
SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
'Release the CBT hook
UnhookWindowsHookEx hHook
End If
WinProc2 = False
End Function
Este es el código que hay que añadir al formulario:
'
'------------------------------------------------------------------
'Ejemplo para posicionar un MsgBox (15/Jun/98)
'
'Microsoft TechNet Knowledge Base, PSS ID Number: Q180936
'HOWTO: Position a MsgBox Using a Windows Hook Procedure
'
'©Guillermo 'guille Som, 1998
'------------------------------------------------------------------
Option Explicit
Private Sub Command1_Click()
Dim hInst As Long
Dim Thread As Long
'Set up the CBT hook
hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc1, hInst, Thread)
'Display the message box
MsgBox "This message box has been positioned at (0,0)."
End Sub
Private Sub Command2_Click()
Dim hInst As Long
Dim Thread As Long
'Set up the CBT hook
hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc2, hInst, Thread)
'Display the message box
MsgBox "This message box is centered over Form1."
End Sub
El problema que hay aquí es que la ejecución del programa se detiene mientras se muestra el messagebox con la llamada a MsgBox. Así que lo único que se me ocurre es establecer un hook, probablemente tipo CTB y monitorear la creación de la ventana del messagebox. Cuando eso se detecta, utilice SetWindowPos() para repocisionar el mensaje.
No voy a presentar código porque es claro que no es algo sencillo y nunca lo he hecho (por lo tanto no tengo código), y espero que también comprenda que no lo intentaré ya que es algo que consumiría bastante tiempo de mi parte y es un proyecto que no me sirve.
No voy a presentar código porque es claro que no es algo sencillo y nunca lo he hecho (por lo tanto no tengo código), y espero que también comprenda que no lo intentaré ya que es algo que consumiría bastante tiempo de mi parte y es un proyecto que no me sirve.
