Attribute VB_Name = "ModuloConstOpciones"
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Declare Function RedrawWindow Lib "user32" (ByVal hWnd As Long, lprcUpdate As RECT, ByVal hrgnUpdate As Long, ByVal fuRedraw As Long) As Long

Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long

Private Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As Long) As Long

Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal insaft As Long, ByVal X%, ByVal Y%, ByVal cx%, ByVal cy%, ByVal flgs As Long) As Long

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal wIndx As Long) As Long

Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long

Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal flgs As Long) As Long

Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long

Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long

Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long

Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long

Const REG_SZ = 1
Const REG_BINARY = 3
Const REG_DWORD = 4
Const HKEY_CURRENT_USER = &H80000001
Const ERROR_SUCCESS = 0&
Const HKEY_LOCAL_MACHINE = &H80000002
Const WS_MINIMIZE = &H20000000  ' Style bit 'is minimized'
Const HWND_TOP = 0              ' Move to top of z-order
Const SWP_NOSIZE = &H1          ' Do not re-size window
Const SWP_NOMOVE = &H2          ' Do not reposition window
Const SWP_SHOWWINDOW = &H40     ' Make window visible/active
Const GW_HWNDFIRST = 0          ' Get first Window handle
Const GW_HWNDNEXT = 2           ' Get next window handle
Const GWL_STYLE = (-16)         ' Get Window's style bits
Const SW_RESTORE = 9            ' Restore window
Const SW_MAXIMIZE = 3
Const Flags = SWP_NOMOVE Or SWP_NOSIZE
Const SW_Normal = 1
Dim strcadena As String
Dim resp As Long
Public Scrypt As Object



Public Function LeerOpciones()

Dim strcadena As String
Dim resp As Long

'leo opc1
strcadena = String(255, 0)
resp = GetPrivateProfileString("OPCIONES", "DBLCLKEXECUTE", "Default", strcadena, 255, App.Path & "\ini\Config.mti")
If resp <> 0 Then
  strcadena = Left$(strcadena, resp)
  If strcadena <> "0" And strcadena <> "1" Then
    Call ResetOpciones
    Exit Function
  Else
    Form5.ChkDblClick.Value = CInt(strcadena)
  End If
End If
'leo opc2
resp = GetPrivateProfileString("OPCIONES", "PERMITIRANIMS", "Default", strcadena, 255, App.Path & "\ini\Config.mti")
If resp <> 0 Then
  strcadena = Left$(strcadena, resp)
  If strcadena <> "0" And strcadena <> "1" Then
    Call ResetOpciones
    Exit Function
  Else
    Form5.ChkAnim.Value = CInt(strcadena)
  End If
End If
'leo opc3
resp = GetPrivateProfileString("OPCIONES", "ESCRIBIRLOG", "Default", strcadena, 255, App.Path & "\ini\Config.mti")
If resp <> 0 Then
  strcadena = Left$(strcadena, resp)
  If strcadena <> "0" And strcadena <> "1" Then
    Call ResetOpciones
    Exit Function
  Else
    Form5.ChkErrorLog.Value = CInt(strcadena)
  End If
End If
'leo opc4
resp = GetPrivateProfileString("OPCIONES", "CONFIGEXCEL", "Default", strcadena, 255, App.Path & "\ini\Config.mti")
If resp <> 0 Then
  strcadena = Left$(strcadena, resp)
  If strcadena <> "0" And strcadena <> "1" Then
    Call ResetOpciones
    Exit Function
  Else
    Form5.ChkExcConfg.Value = CInt(strcadena)
  End If
End If
'leo opc5
resp = GetPrivateProfileString("OPCIONES", "MOSTRARPRO", "Default", strcadena, 255, App.Path & "\ini\Config.mti")
If resp <> 0 Then
  strcadena = Left$(strcadena, resp)
  If strcadena <> "0" And strcadena <> "1" Then
    Call ResetOpciones
    Exit Function
  Else
    Form5.Chkpro.Value = CInt(strcadena)
  End If
End If
'leo opc6
resp = GetPrivateProfileString("OPCIONES", "MINSTANCES", "Default", strcadena, 255, App.Path & "\ini\Config.mti")
If resp <> 0 Then
  strcadena = Left$(strcadena, resp)
  If strcadena <> "0" And strcadena <> "1" Then
    Call ResetOpciones
    Exit Function
  Else
    Form5.ChkMInstances.Value = CInt(strcadena)
  End If
End If






End Function

Public Function SalvarNewOpciones()
On Error Resume Next
Kill App.Path & "\ini\Config.mti"
On Error GoTo ErrorLoguer
WritePrivateProfileString "OPCIONES", "DBLCLKEXECUTE", CStr(Form5.ChkDblClick.Value), App.Path & "\ini\Config.mti"
WritePrivateProfileString "OPCIONES", "PERMITIRANIMS", CStr(Form5.ChkAnim.Value), App.Path & "\ini\Config.mti"
WritePrivateProfileString "OPCIONES", "ESCRIBIRLOG", CStr(Form5.ChkErrorLog.Value), App.Path & "\ini\Config.mti"
WritePrivateProfileString "OPCIONES", "CONFIGEXCEL", CStr(Form5.ChkExcConfg.Value), App.Path & "\ini\Config.mti"
WritePrivateProfileString "OPCIONES", "MOSTRARPRO", CStr(Form5.Chkpro.Value), App.Path & "\ini\Config.mti"
WritePrivateProfileString "OPCIONES", "MINSTANCES", CStr(Form5.ChkMInstances.Value), App.Path & "\ini\Config.mti"

Exit Function

ErrorLoguer:

WriteLogError ("No se ha podido guardar correctamente la configuracion de opciones!!!")

End Function

Public Function BuscarExisteArchivoIni()
Dim A
Dim PathIni As String

Set Scrypt = CreateObject("Scripting.FileSystemObject")

PathIni = App.Path & "\Ini\Config.mti"

  On Error Resume Next
  
  Set A = Scrypt.createfolder(App.Path & "\Ini")
  Set A = Scrypt.createfolder(App.Path & "\Log")
  
  If Scrypt.fileexists(App.Path & "\log\mati.log") = False Then
    Set A = Scrypt.createtextfile(App.Path & "\Log\Mati.log")
  End If
  
  If Scrypt.fileexists(PathIni) = True Then Exit Function
  Call ResetOpciones

End Function

Public Function ResetOpciones()
On Error Resume Next
Kill App.Path & "\ini\Config.mti"
WritePrivateProfileString "OPCIONES", "DBLCLKEXECUTE", "1", App.Path & "\ini\Config.mti"
WritePrivateProfileString "OPCIONES", "PERMITIRANIMS", "1", App.Path & "\ini\Config.mti"
WritePrivateProfileString "OPCIONES", "ESCRIBIRLOG", "1", App.Path & "\ini\Config.mti"
WritePrivateProfileString "OPCIONES", "CONFIGEXCEL", "0", App.Path & "\ini\Config.mti"
WritePrivateProfileString "OPCIONES", "MOSTARPRO", "1", App.Path & "\ini\Config.mti"
WritePrivateProfileString "OPCIONES", "MINSTANCES", "0", App.Path & "\ini\Config.mti"

End Function

Public Function MakeNewKeys()

Dim Res, Resultado, Myhandle As Long
Dim MyString As String

On Error Resume Next

RegDeleteKey HKEY_LOCAL_MACHINE, "Software\Mati15"
Resultado = RegCreateKey(HKEY_LOCAL_MACHINE, "Software\Mati15", Res)

If Resultado = ERROR_SUCCESS Then
  RegCloseKey Res
Else
  WriteLogError "No se ha podido crear la clave Mati15"
End If

MyString = App.Path & "\" & App.EXEName & ".EXE"
Myhandle = Form12.hWnd

RegOpenKey HKEY_LOCAL_MACHINE, "Software\Mati15", Res
RegSetValueEx Res, "Hwnd", 0, REG_BINARY, ByVal CStr(Myhandle), Len(Myhandle)
RegSetValueEx Res, "Minstances", 0, REG_BINARY, Form5.ChkMInstances.Value, 1
RegSetValueEx Res, "InstalationPath", 0, REG_SZ, ByVal MyString, Len(MyString)
RegCloseKey Res

End Function

Public Function WriteLogError(ErrorDesC As String)

If Form5.ChkErrorLog.Value = 0 Then Exit Function

Dim MyString, Nombreusuario, NombrePC As String
Dim Answer As Boolean

On Error Resume Next

'GetUserName ByVal Nombreusuario, 16
'Nombreusuario = Trim$(Nombreusuario)

'GetComputerName ByVal NombrePC, 16
'NombrePC = Trim$(NombrePC)

MyString = ReadStringFromFile(App.Path & "\log\mati.log")
Answer = WriteStringToFile(App.Path & "\log\mati.log", Trim(MyString) & ErrorDesC & " " & Time & " " & Date & vbCrLf, False)

End Function

Public Function VaciarLogFile()

Dim Ts, No As Object

On Error Resume Next

Set Ts = CreateObject("Scripting.filesystemobject")
Set No = Ts.createtextfile(App.Path & "\log\Mati.log", True)
No.Close

End Function

Public Function ExisteEjecutado() As Boolean

Dim MyHwnd As String
Dim Test, Binario, Res As Long

MyHwnd = String(255, Chr(0))

RegOpenKey HKEY_LOCAL_MACHINE, "Software\Mati15", Res

Test = RegQueryValueEx(Res, "Hwnd", 0, REG_BINARY, ByVal MyHwnd, 4)

If Test <> 0 Then
  GetKeys = False
  Exit Function
End If

Test = RegQueryValueEx(Res, "Minstances", 0, REG_BINARY, Binario, 1)

If Test <> 0 Then
  ExisteEjecutado = False
  Exit Function
End If

RegCloseKey Res

Test = ExistsWinHwnd(CInt(MyHwnd))

If Test = False Then
  ExisteEjecutado = False
  Exit Function
End If

ExisteEjecutado = True

End Function

Private Function ExistsWinHwnd(Handle As Long) As Boolean

Dim Equis As Long
Dim MyRect As RECT

Equis = GetDC(Handle)

If Equis <> 0 Then
  ExistsWinHwnd = False
  Exit Function
Else
  ExistsWinHwnd = False
End If

End Function
