Cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted

Center a user dialog API form

Valued Contributor
Valued Contributor

Does anyone know how to center a user-created dialog box on the bottom of the screen so that it matches the location of the default dialogs? All I can seem to do is manually enter a screen coordinate, but it will not necessarily be centered depending on the resolution of the screen.

2 REPLIES 2

Re: Center a user dialog API form

Siemens Phenom Siemens Phenom
Siemens Phenom

The location of WinWrap BASIC dialogs can be specified, as you mentioned, before UserDialog as such (e.g. xpos, ypos):


Begin Dialog UserDialog xpos, ypos, 200,120,.DialogFunc

 

where xpos = the distance from the left side of the monitor and ypos = the distance from the top of the monitor.  As such, the location FEMAP does not affect the location of the WinWrap dialog.  In order to accomplish what you are looking for, we will have to include some Windows API calls and user32.dll.  Specifically, we will use GetActiveWindow and GetWindowRect to return the location of the active FEMAP window.  From there, it is a simple matter of calibrating the WinWrap dialog as some percentage from the right and bottom of your FEMAP application. 

 

(view in My Videos)

 

Here is an example script:


Option Explicit
Dim App As femap.model
Public fTop As Long, fBottom As Long, fLeft As Long, fRight As Long

Private Type Size
    L As Long
    T As Long
    R As Long
    B As Long
End Type

Declare Function GetActiveWindow Lib "user32" () As PortInt

'Returns a RECT, try with WinWrap Type
Declare Function GetWindowRect Lib "user32" _
     (ByVal hwnd  As Long, ByRef getSize As Size) As PortInt

Declare Function GetWindowTextLength Lib "user32" _
    (ByVal hwnd As PortInt) As PortInt

Declare Sub GetWindowText Lib "user32" _
    (ByVal hwnd  As PortInt, ByVal lpsz As String, ByVal cbMax  As PortInt)

Function ActiveWindowTitle() As String
    Dim ActiveWindow As PortInt
    Dim ActiveWindowSize As Size

    ActiveWindow = GetActiveWindow()
    GetWindowRect ActiveWindow, ActiveWindowSize
    fTop = ActiveWindowSize.T
    fBottom = ActiveWindowSize.b
    fLeft = ActiveWindowSize.L
    fRight = ActiveWindowSize.r
    Dim TitleLen As PortInt
    TitleLen = GetWindowTextLength(ActiveWindow)
    Dim Title As String
    Title = Space(TitleLen)
    GetWindowText ActiveWindow, Title, TitleLen+1
    ActiveWindowTitle = Title
End Function

Sub Main
    Set App = feFemap()
    Debug.Print ActiveWindowTitle()

    Dim xpos As Long, ypos As Long
    xpos = 0.6*fRight
    ypos = 0.6*fBottom

    Begin Dialog UserDialog xpos, ypos, 200,120,.DialogFunc
        text 10,10,180,15,"Please push the OK button"
        TextBox 10,40,180,15,.text
        OKButton 30,90,60,20
        PushButton 110,90,60,20,"&Hello"
    End Dialog
    Dim dlg As UserDialog
    Debug.Print Dialog(dlg)
End Sub

Function DialogFunc(DlgItem$, Action%, SuppValue?) As Boolean
    Select Case Action%
    Case 1 ' Dialog box initialization
        Beep
    Case 2 ' Value changing or button pressed
        Select Case DlgItem$
        Case "OK"
            App.feAppMessage(0, "Ok was pressed")
        Case "Hello"
            App.feAppMessage(2, "Hello was pressed")
        End Select
    Case 4 ' Focus changed
        Debug.Print "DlgFocus="""; DlgFocus(); """"
    Case 6 ' Function key
        If SuppValue? And &H100 Then Debug.Print "Shift-";
        If SuppValue? And &H200 Then Debug.Print "Ctrl-";
        If SuppValue? And &H400 Then Debug.Print "Alt-";
        Debug.Print "F" & (SuppValue And &HFF)
    End Select
End Function

Re: Center a user dialog API form

Valued Contributor
Valued Contributor

Perfect! Thanks!