Announcement

Collapse
No announcement yet.

System tray (or: my head hurts now)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • System tray (or: my head hurts now)

    I'm creating a small, single dialog app with DDT in PB/DLL 6.0.

    I would like it to minimize to the system tray, and to have a click in the tray icon bring the dialog back.

    I've looked at and tried to understand examples and messages here on the forum. I'm not interested in having a menu attached to the system tray icon, nor do I want an "invisible" dialog box. I just want an icon in the system tray that acts like the button on the taskbar would.

    What's the key?



    ------------------
    [email protected]
    http://www.northnet.org/bdurland
    Real programmers use a magnetized needle and a steady hand

  • #2
    Is this what you mean?
    It works for me, I weeded out a lot of code that's not of interest to this topic, but I'm sure you'll be able to get an idea from what's left:
    I used a temporary window because the system tray functions needed a window handle but communicate to the user with the dialog.

    Code:
    CALLBACK FUNCTION ClickButton()
    
      IF CBCTL = %BTN_OK THEN
        'hide dialog
        Dialogs = -1
        DIALOG SHOW STATE CBHNDL, %SW_HIDE
      ELSEIF CBCTL = %BTN_EXIT THEN
        IF Users <= 0 THEN
          PostQuitMessage 0
        ELSE
          IF MessageBox(0, _
                        "Are you sure ?" + $CRLF + "there are" + STR$(Users) + " users active!", _
                        $ProgTitle, _
                        %MB_YESNO) = %IDYES	THEN
            PostQuitMessage 0
          ELSE
            DIALOG SHOW STATE CBHNDL, %SW_SHOW
          END IF
        END IF
      END IF
    END FUNCTION
    '------------------------------------------------------------------------------
    
    FUNCTION WinMain (BYVAL hInstance     AS LONG, _
                      BYVAL hPrevInstance AS LONG, _
                      lpCmdLine           AS ASCIIZ PTR, _
                      BYVAL iCmdShow      AS LONG) AS LONG
    
      LOCAL Msg         AS tagMsg
      LOCAL i           AS LONG
      LOCAL j           AS LONG
      LOCAL k           AS LONG
      LOCAL f           AS LONG
      LOCAL s           AS STRING
      LOCAL s1          AS STRING
      LOCAL adr         AS LONG
      LOCAL wndclass    AS WndClassEx
      LOCAL szClassName AS ASCIIZ * 80
      LOCAL hWnd        AS LONG
      LOCAL OldWindow   AS LONG
      LOCAL pnid        AS NOTIFYICONDATA
      LOCAL ErrorState  AS LONG
      LOCAL TCPPort     AS LONG
      LOCAL ExitState   AS LONG
      LOCAL sz          AS ASCIIZ * %MAX_PATH
      LOCAL wsdata      AS WSADATA
    
      OldWindow = GetForegroundWindow
      szClassName            = $ClassName
      hInst = FindWindow(szClassName, "")
    
      'create window class
      hInst                  = hInstance
      wndclass.cbSize        = SIZEOF(WndClass)
      wndclass.style         = %CS_HREDRAW OR %CS_VREDRAW
      wndclass.lpfnWndProc   = CODEPTR(WndProc)
      wndclass.cbClsExtra    = 0
      wndclass.cbWndExtra    = 0
      wndclass.hInstance     = hInstance
      wndclass.hIcon         = LoadIcon(hInstance, "PROGRAM" )
      wndclass.hCursor       = LoadCursor( %NULL, BYVAL %IDC_ARROW )
      wndclass.hbrBackground = GetStockObject( %WHITE_BRUSH )
      wndclass.lpszMenuName  = %NULL
      wndclass.lpszClassName = VARPTR( szClassName )
      wndclass.hIconSm       = LoadIcon(hInstance, BYVAL %IDI_APPLICATION )
      RegisterClassEx wndclass
    
      'center window
      i = (GetSystemMetrics(%SM_CXSCREEN) - 160) \ 2
      j = (GetSystemMetrics(%SM_CYSCREEN) - 100) \ 2
      ' Create a window using the registered class
      hWnd = CreateWindowEx(%NULL, _                       'extended class
                              szClassName, _               ' window class name
                              $ProgTitle, _                ' window caption
                              0, _                         ' window style
                              i, _                         ' initial x position
                              j, _                         ' initial y position
                              160, _                       ' initial x size
                              100, _                       ' initial y size
                              %HWND_DESKTOP, _             ' parent window handle
                              %NULL, _                     ' window menu handle
                              hInstance, _                 ' program instance handle
                              BYVAL %NULL)                 ' creation parameters
    
      ShowWindow hWnd, iCmdShow
      UpdateWindow hWnd
    
      'Fill pnid structure (for the windows Status bar):
      pnid.cbSize           = LEN(pnid)
      pnid.hWnd             = hWnd
      pnid.uID              = %CTL_TASKBAR
      pnid.uFlags           = %NIF_ICON OR %NIF_MESSAGE OR %NIF_TIP
      pnid.uCallbackMessage = %WM_TASKBARCLICKED
      pnid.hIcon            = LoadIcon(hInstance, "TASKBAR" )
      pnid.szTip            = $ProgTitle
    
      'Put icon in the status bar
      Shell_NotifyIcon %NIM_ADD, pnid
      'hide startup window
      ShowWindow hWndMain, %SW_HIDE
      DIALOG NEW 0, $ProgTitle, ,, %DLG_WIDTH, %DLG_HEIGHT,, 0 TO hDlg
      CONTROL ADD BUTTON, hDlg, %BTN_OK, "OK", _
                          %DLG_WIDTH - 100, %DLG_HEIGHT - 20, 40, 14, 0 CALL ClickButton
      CONTROL ADD BUTTON, hDlg, %BTN_EXIT, "Exit", _
                          %DLG_WIDTH - 50, %DLG_HEIGHT - 20, 40, 14, 0 CALL ClickButton
    
      DIALOG SHOW MODELESS hDlg
      'hide in the taskbar
      Dialogs = -1
      DIALOG SHOW STATE hDlg, %SW_HIDE
      '
      '
      'start your other stuff here.
      '
      ' Main Message handler:
      DO
        ApiSleep 100
        WHILE PeekMessage(Msg, %NULL, 0, 0, %PM_NOREMOVE)
          IF GetMessage(Msg, %NULL, 0, 0) = 0 THEN
            ExitState = %TRUE
          END IF
          TranslateMessage Msg
          DispatchMessage Msg
        WEND
        IF Dialogs <> 0 THEN
          DIALOG DOEVENTS
        END IF
      LOOP UNTIL ExitState
      'cleanup and exit
      'delete dialog
      IF hDlg <> 0 THEN
        DIALOG END hDlg
        hDlg = 0
      END IF
      'Remove icon from the taskbar
      Shell_NotifyIcon %NIM_DELETE, pnid
      FUNCTION = msg.wParam
    END FUNCTION  ' WinMain
    '------------------------------------------------------------------------------
    
    FUNCTION WndProc (BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _
                      BYVAL wParam AS LONG, BYVAL lParam AS LONG) EXPORT AS LONG
    
      REGISTER i        AS LONG
      REGISTER j        AS LONG
    
      IF wMsg = %WM_TASKBARCLICKED THEN
        IF lparam = %WM_LBUTTONUP THEN
          Dialogs = 1
          DIALOG SHOW STATE hDlg, %SW_SHOW
        END IF
        FUNCTION = 0
        EXIT FUNCTION
    
      ELSEIF wMsg = %WM_DESTROY THEN
        PostQuitMessage 0
        FUNCTION = 0
        EXIT FUNCTION
      END IF
    
      FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)
    
    END FUNCTION

    ------------------
    [email protected]
    [email protected]

    Comment

    Working...
    X