Date: 02-16-2022
Return to Index
created by gbSnippets
'The MsgBox statement does not allow the programmer to set its position. This
'code shows how to do it.
'Primary Code:
'Credit: Semen Matusovski
Function SBProc(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'Semen's example uses screen dimensions to set the MsgBox position.
'in the compilable example below, dialog dimensions are used.
Dim rc As Local RECT
Dim hHook As Global Dword
If lMsg = %HCBT_ACTIVATE Then
GetWindowRect wParam, rc ' Msgbox
SetWindowPos wParam, 0, (GetSystemMetrics(%SM_CXSCREEN) - (rc.nRight - rc.nLeft)) * 0.10, _
(GetSystemMetrics(%SM_CYSCREEN) - (rc.nBottom - rc.nTop)) * 0.10, 0, 0, %SWP_NOSIZE Or %SWP_NOACTIVATE Or %SWP_NOZORDER
UnhookWindowsHookEx hHook
End If
End Function
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword, hHook as Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,400,400, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
hHook = SetWindowsHookEx(%WH_CBT, Codeptr(SBProc), GetModuleHandle(""), GetCurrentThreadId)
MessageBox 0, "Continue ?", "PB/CC 3.0", %MB_ICONQUESTION Or %MB_OKCANCEL Or %MB_TASKMODAL
End If
End Function
Function SBProc(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Local x as Long, y as Long 'MsgBox is approximately 200x200
CenterOverDialog 200,200,x,y
If lMsg = %HCBT_ACTIVATE Then
SetWindowPos wParam, 0, x,y, 0, 0, %SWP_NOSIZE Or %SWP_NOACTIVATE Or %SWP_NOZORDER
UnhookWindowsHookEx hHook
End If
End Function
Sub CenterOverDialog(BoxX as Long, BoxY as Long, CenterX As Long, CenterY as Long)
'gets top/left position to center Box over Dialog
'you need to know, or at least estimate, the Box size
Local x as Long, y as Long, w as Long, h as Long
Dialog Get Loc hDlg To x,y
Dialog Get Size hDlg To w,h
CenterX = x + (w-BoxX)/2
CenterY = y + (h-BoxY)/2
End Sub
'gbs_00111
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm