Jump to content

Recommended Posts

Posted (edited)

I've gutted my app down to the problem code. When it's launched, it does a 'connect' (mainly to see if it can).

When you click on settings, you can change the url, and I'm running 'connect' again, to see if the url is valid. When I run that, using a hidden browser window, the Settings window closes, and clicking on the Settings item in the tray menu no longer works.

Any/all help is greatly appreciated.

ps: at what point do people prefer attachments over code boxes?

pps: all comments welcome, I've been using AutoIt for less than a week, so feel free to tell me "You're doing it wrong!".

Edit: Clarification (just in case):

I do _not_ want the settings window to close when 'connect' runs.

I _do_ want the settings menu item to work after a 'connect'.

Edit: At least I _think_ it's the browser window that's killing it. Feel free to look for other causes. If I knew what the hell I was talking about, I wouldn't be asking here.

#cs
connect.ini
------------
[User]
Url =http://www.google.com
#ce

#include <IE.au3>; browser
#include <GUIConstants.au3>; GUI

#NoTrayIcon 
Opt("MustDeclareVars", 1); be anal about variables

Global $App = ObjCreate("Scripting.Dictionary")
If @error Then
  MsgBox(0, '', 'Unable to create the configuration dictionary.')
Else
  With $App
    .Add('Dir_App'  , 'c:\temp\')
    .Add('File_Ini' , 'connect.ini')
    .Add('Path_Ini' , $App.Item('Dir_App') & 'connect.ini')

    .Add('Err_NotFound', 'File not found: ' & $App.Item('Dir_App'))
    .Add('Err_IniSave' , 'Unable to save setting: ')

    .Add('Msg_SettingsSaved', 'Your settings have been saved.')

    .Add('Msg_Lbl_Debug', 'Debug: ')
    .Add('Msg_Lbl_Error', 'Error: ')
    .Add('Msg_Lbl_Warn' , 'Warning: ')

    .Add('User_Url' , 'http://www.google.com')

    .Add('Input_UserUrl' , '')
    .Add('Lbl_User_Url'  , 'Url:')  

    .Add('Menu_Settings' , '')
    .Add('Menu_Exit'     , '')

    .Add('Txt_Menu_Settings' , 'Settings')
    .Add('Txt_Menu_Exit'     , 'Exit'   )

    .Add('Txt_Settings_Done', 'Close' )
    .Add('Txt_Settings_Save', 'Update')

    .Add('App_Installed', false)
    .Add('App_Connected', false)

    .Add('App_Name', 'Gorilla Droppings')

    .Add('Title_SystemTray'   , $App.Item('App_Name') & ' Menu.')
    .Add('Title_SettingsEdit' , $App.Item('App_Name') & ' User Settings.')
    .Add('Title_WebGetPage'   , $App.Item('App_Name') & ' Page Grabber.')
  EndWith
EndIf

Global $WinSettings

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DELETE ME!!!!!!!!!!!!!!!!!!!!!!!!!!
KillCurrent(); Kill it if it's already running - THIS IS FOR TESTING - COMMENT BEFORE RELEASE (I think).
Init()
Menu()
Exit

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DELETE ME!!!!!!!!!!!!!!!!!!!!!!!!!!
Func KillCurrent()
;  Local $bs = DirRemove($App.Item('Dir_App'), 1); nuke the app_dir
  Local $myWin = $App.item('Title_SystemTray')
  If WinExists($myWin) Then WinKill($myWin) 
  AutoItWinSetTitle($myWin)
EndFunc

Func Init()
  Opt("TrayMenuMode",1); Default tray menu items (Script Paused/Exit) will not be shown.
  Opt("TrayOnEventMode",1); Change to OnEvent mode
  Opt("GUIOnEventMode", 1); Change to OnEvent mode

;  TraySetClick(16); Left Click to show menu.
  TraySetState() 

  $App.Item('App_Installed') = true
  SettingsLoad()
EndFunc

Func Menu()
  $App.Item('Menu_Settings')  = TrayCreateItem($App.Item('Txt_Menu_Settings'))
  $App.Item('Menu_Exit')      = TrayCreateItem($App.Item('Txt_Menu_Exit'))

  TrayItemSetOnEvent($App.Item('Menu_Settings')  , "SettingsEdit" )
  TrayItemSetOnEvent($App.Item('Menu_Exit')   , "ExitApp"     )

  WebConnect()

  While 1
    Sleep(10); Idle loop
  WEnd
  Exit
EndFunc

Func ExitApp()
  Exit
EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Connect
Func WebConnect()
  local $text, $parts, $url = $App.Item('User_Url')
  If $url then
    $text = WebGetPage($url)
    debug($text)
  EndIf
  return false
EndFunc

Func WebGetPage($url)
  Local $text, $browser, $window, $GUIActiveX
  _IEErrorHandlerRegister()
  $browser = _IECreateEmbedded()
  GUISetState(@SW_HIDE);HIDE GUI
  $window = GUICreate($App.item('Title_WebGetPage'))
  $GUIActiveX = GUICtrlCreateObj($browser, 1, 1)
  _IENavigate($browser, $url)
  $text = _IEBodyReadHTML($browser)
  $text = StringStripWS($text, 3)
  GUIDelete($window)
  return $text
EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Settings

Func SettingsEdit()
  If WinGetHandle($App.item('Title_SettingsEdit')) then
    WinActivate($App.item('Title_SettingsEdit'))
    return true
  EndIf

  Local $ClickUpdate, $ClickClose, $temp

  $WinSettings = GUICreate( _
    $App.item('Title_SettingsEdit'), 400, 450, _
    -1, -1, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS _
  )

  GUICtrlCreateLabel($App.item('Lbl_User_Url'  ), 10,  10, 100, 20);  10

  $App.Item('Input_UserUrl')  = GUICtrlCreateInput($App.Item('User_Url')  , 105,  10, 250, 18)

  $ClickUpdate = GUICtrlCreateButton($App.Item('Txt_Settings_Save'), 220, 410, 80, 30)
  $ClickClose  = GUICtrlCreateButton($App.Item('Txt_Settings_Done'), 310, 410, 80, 30)

  GUICtrlSetOnEvent($ClickUpdate, "SettingsUpdate")
  GUICtrlSetOnEvent($ClickClose , "SettingsClose")
  GUISetOnEvent($GUI_EVENT_CLOSE, "SettingsClose")

  GUISetState(@SW_SHOW);Show GUI
EndFunc

Func SettingsClose()
  debug(@GUI_WINHANDLE)
  if @GUI_WINHANDLE = $WinSettings Then GUIDelete($WinSettings)
EndFunc

Func SettingsUpdate()
  if SettingsSaveUser() then
    local $temp = GUICtrlCreateLabel($App.Item('Msg_SettingsSaved'), 100, 385, 200, 20)
    sleep(2000)
    GUICtrlDelete($temp)
    WebConnect()
  endif
EndFunc

Func SettingsLoad(); this can't fail - worst case, it returns the default values.
  local $file = $App.item('Path_Ini')
  $App.Item('User_Url')   = IniRead($file, "User", "Url" , "")
  return true
EndFunc

Func SettingsSaveUser()
  local $file = $App.item('Path_Ini')
  DoOrDie(FileExists($file), $App.item('Err_NotFound') & $file)
  $App.Item('User_Url')  = GUICtrlRead($App.Item('Input_UserUrl' ))
  SettingWrite($file, "User", "Url" , $App.Item('User_Url' ))
  return true
EndFunc

Func SettingWrite($file, $section, $key, $value)
  DoOrDie(IniWrite($file, $section, $key, $value), $App.item('Err_IniSave') & $key)
EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; UTILITIES
Func Debug($msg)
  MsgBox(4096,$App.item('Msg_Lbl_Debug'), $msg)
EndFunc

Func DoOrDie($act, $err_msg)
  If $act Then return true
  MsgBox(4096,$App.item('Msg_Lbl_Error'), $err_msg)
  exit
EndFunc

Func DoOrWarn($act, $err_msg)
  If $act Then return true
  Tell($App.item('Msg_Lbl_Warn') & $err_msg)
  return false
EndFunc

Func Tell($msg)
  TrayTip($App.item('App_Name'), $msg, 1)
  sleep(2000)
  TrayTip("", "", 0)
  return true
EndFunc
Edited by aGorilla
Posted (edited)

2 long phone calls later, a bit of simple debugging, and the answer was clear.

I was doing GUISetState(@SW_HIDE) before creating the next window.

a) This means it was hiding the 'last' window created.

B) I was foolish enough to do this without a window handle, which may have prevented the above.

I thought that I had to hide it first, or it would 'flash' briefly on creation. It seems that assumption was false.

Here's the corrected function:

Func WebGetPage($url)
  Local $text, $browser, $window, $GUIActiveX
  _IEErrorHandlerRegister()
  $browser = _IECreateEmbedded()
  $window = GUICreate($App.item('Title_WebGetPage'))
  GUISetState(@SW_HIDE, $window);HIDE GUI
  $GUIActiveX = GUICtrlCreateObj($browser, 1, 1)
  _IENavigate($browser, $url)
  $text = _IEBodyReadHTML($browser)
  $text = StringStripWS($text, 3)
  GUIDelete($window)
  return $text
EndFunc

Edit: After a bit of rtfm'ing, I find out that windows are hidden by default, and I didn't even need to do that. Doh!

Edited by aGorilla

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...