Jump to content

Minimize CUI to tray


Recommended Posts

Hi, I have a script that runs in a console user interface and I'd like for it to minimize to system tray or even better close to system tray. Most of the search results I find are related to GUI which doesn't quite work for me. Is it even possible? Or should I completely rewrite my code to work on GUI?

Link to comment
Share on other sites

This code runs a test on the computer
- Windows 11 24H2 LTSC
- Autoit version: 3.3.16.1

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

If @Compiled < 1 Then
    MsgBox(48 + 262144, "TEST CUI - You have not compiled yet!", "Let's compile the script with CUI to try it out!", 10)
    Exit
EndIf

Local $hDOS = WinGetHandle("[CLASS:ConsoleWindowClass]")
If Not $hDOS Then WinGetTitle("[active]")

ConsoleWrite("Hello" & @CRLF)
WinSetState($hDOS, '', @SW_HIDE)
Sleep(2000)
WinSetState($hDOS, '', @SW_SHOW)
Sleep(2000)
WinSetState($hDOS, '', @SW_MINIMIZE)
Sleep(2000)
WinSetState($hDOS, '', @SW_MAXIMIZE)
Sleep(2000)
WinSetState($hDOS, '', @SW_RESTORE)
Sleep(2000)
ConsoleWrite("Move" & WinMove($hDOS, '', @DesktopHeight, @DesktopWidth) & @CRLF)
Sleep(2000)
ConsoleWrite("Restore Move" & WinMove($hDOS, '', 10, 10) & @CRLF)
Sleep(3000)
ConsoleWrite("Move" & WinMove($hDOS, '', @DesktopHeight, @DesktopWidth) & @CRLF)
Sleep(2000)
ConsoleWrite("Restore Move" & WinMove($hDOS, '', 10, 10) & @CRLF)
Sleep(3000)

 

You should rewrite script using GUI to make it easier to control than using CUI

Edited by Trong

Regards,
 

Link to comment
Share on other sites

Or something like this :

#NoTrayIcon
#include <Constants.au3>

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1)
Opt("TrayAutoPause", 0)

HotKeySet("{ESC}", Terminate)

If Not @Compiled Then Exit MsgBox($MB_OK, "Error", "The script needs to be compiled")
Local $hWnd = WinGetHandle("[CLASS:ConsoleWindowClass]")

While Sleep(100)
  If BitAND(WinGetState($hWnd), $WIN_STATE_MINIMIZED) Then
    WinSetState($hWnd, "", @SW_HIDE)
    TraySetIcon("shell32.dll", 16)
    TraySetOnEvent($TRAY_EVENT_PRIMARYUP, Restore)
    TraySetState($TRAY_ICONSTATE_SHOW)
    TraySetToolTip("Left click to restore")
  EndIf
WEnd

Func Terminate()
  Exit
EndFunc

Func Restore()
  TraySetState($TRAY_ICONSTATE_HIDE)
  WinSetState($hWnd, "", @SW_SHOW)
  WinActivate($hWnd)
EndFunc   ;==>GUI_Restore

 

Edited by Nine
simplified code
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...