[API]
SetFocusApi
CheckDlgButton
IsDlgButtonChecked

[INCLUDE]
GenericInit
LoadResEx_Dialog_Shared
DialogBoxIndirectParam
SetWindowLong

[DEFINE]
Private Const LoadResEx_Dialog_Disabled = -1&
Private Type LoadResEx_Dialog_DialogInfo
  xEscBehaviour As Long
  xButtonCtlID As Long
  xCallBackProc As Long
  End Type
Private Const LoadResEx_Dialog_DialogInfoSize = 12&

[CODE]
Private Function LoadResEx_Dialog_InternalProc(ByVal HWndDialog As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  Dim xLong1 As Long
  Dim xType1 As LoadResEx_Dialog_DialogInfo
  
  If Msg = &H110& Then
    If Not lParam = 0& Then
      xLong1 = lParam
      Call RtlMoveMemory(xType1, ByVal lParam, LoadResEx_Dialog_DialogInfoSize)
      If Not xType1.xCallBackProc = 0& Then
        If IsUnicode Then
          LoadResEx_Dialog_InternalProc = CallWindowProcWide(xType1.xCallBackProc, HWndDialog, Msg, wParam, lParam)
          Else
          LoadResEx_Dialog_InternalProc = CallWindowProcAnsi(xType1.xCallBackProc, HWndDialog, Msg, wParam, lParam)
          End If
        If Not LoadResEx_Dialog_InternalProc = 0& Then
          LoadResEx_Dialog_InternalProc = GetDlgItem(HWndDialog, LoadResEx_Dialog_InternalProc)
          GoTo FocusFromProc
          End If
        End If
      If Not xType1.xButtonCtlID = 0& Then
        LoadResEx_Dialog_InternalProc = GetDlgItem(HWndDialog, xType1.xButtonCtlID)
        If Not LoadResEx_Dialog_InternalProc = 0& Then
FocusFromProc:
          If Not LoadResEx_Dialog_InternalProc = 0& Then
            SetFocusAPI LoadResEx_Dialog_InternalProc
            'Stop
            LoadResEx_Dialog_InternalProc = 0&
            Else
            LoadResEx_Dialog_InternalProc = 1&
            End If
          Else
          LoadResEx_Dialog_InternalProc = 1&
          End If
        End If
      Call SetWindowLong(HWndDialog, -21&, xLong1)
      End If
    Else
    xLong1 = GetWindowLong(HWndDialog, -21&)
    If Not xLong1 = 0& Then
      Call RtlMoveMemory(xType1, ByVal xLong1, LoadResEx_Dialog_DialogInfoSize)
      Select Case Msg
        Case &H10&
          If Not xType1.xEscBehaviour = LoadResEx_Dialog_Disabled Then
            GoTo lblEndDialog
            End If
        Case &H111& 'wm_command
          If lParam = 0& Then
            If wParam = 2& And Not xType1.xEscBehaviour = LoadResEx_Dialog_Disabled Then
'              Stop
              GoTo lblEndDialog
              End If
            Else
            If (GetWindowLong(lParam, -16&) And &HE&) = 0& Then
              If xType1.xCallBackProc = 0& Then
                xType1.xEscBehaviour = wParam
'                Stop
                GoTo lblEndDialog
                Else
                If IsUnicode Then
                  xLong1 = CallWindowProcWide(xType1.xCallBackProc, HWndDialog, Msg, wParam, lParam)
                  Else
                  xLong1 = CallWindowProcAnsi(xType1.xCallBackProc, HWndDialog, Msg, wParam, lParam)
                  End If
                If xLong1 = 1& Then
                  xType1.xEscBehaviour = wParam
'                  Stop
                  GoTo lblEndDialog
                  End If
                End If
              ElseIf Not xType1.xCallBackProc = 0& Then
              If IsUnicode Then
                Call CallWindowProcWide(xType1.xCallBackProc, HWndDialog, Msg, wParam, lParam)
                Else
                Call CallWindowProcAnsi(xType1.xCallBackProc, HWndDialog, Msg, wParam, lParam)
                End If
              End If
            End If
        End Select
      End If
    End If
  Exit Function

lblEndDialog:
  If Not xType1.xCallBackProc = 0& Then
    If IsUnicode Then
      Call CallWindowProcWide(xType1.xCallBackProc, HWndDialog, &H12&, wParam, lParam)
      Else
      Call CallWindowProcAnsi(xType1.xCallBackProc, HWndDialog, &H12&, wParam, lParam)
      End If
    End If
  Call EndDialog(HWndDialog, xType1.xEscBehaviour)
  End Function

' result, or 0/1
'''' CheckControlID: checkboxid, or radio id (must specify RadioButtonFirst / Last!!!)
' DefaultButtonID = Control ID for focus
' CanEscapeControlID = Control ID (Result) if ESC pressed, LoadResEx_Dialog_Disabled (-1) to prevent ESC from closing dialog
Public Function LoadResEx_Dialog(ByVal hWndOwner As Long, ByVal ResDialogOrID As Variant, Optional DefaultButtonID As Long = 0&, Optional ByVal CanEscapeControlID As Long = LoadResEx_Dialog_Disabled, Optional ByVal CallBackProc As Long, Optional ByVal hModule As Long) As Long
  Dim xLong1 As Long
  Dim xType1 As LoadResEx_Dialog_DialogInfo
    
  xType1.xCallBackProc = CallBackProc
  xType1.xEscBehaviour = CanEscapeControlID
  xType1.xButtonCtlID = DefaultButtonID
  xLong1 = LocalAlloc(0&, LoadResEx_Dialog_DialogInfoSize)
  If Not xLong1 = 0& Then
    Call RtlMoveMemory(ByVal xLong1, xType1, LoadResEx_Dialog_DialogInfoSize)
'    If Not hModule = 0& Then 'And Not (LoadResEx_LanguageFile_hModul = 0&) Then
      LoadResEx_Dialog = DialogBoxIndirectParam(ResDialogOrID, hWndOwner, AddressOf LoadResEx_Dialog_InternalProc, xLong1, hModule)            ', ResDialogID)
'      Else
'      LoadResEx_Dialog = DialogBoxIndirectParam(ResDialogOrID, hWndOwner, AddressOf LoadResEx_Dialog_InternalProc, xLong1)            ', ResDialogID)
'      End If
'    Call RtlMoveMemory(xType1, ByVal xLong1, LoadResEx_Dialog_DialogInfoSize)
    Call LocalFree(xLong1)
    End If
  End Function