[Top Page][Edit][Discussion][Edit History][All Pages][Recent Changes][->Japanese]

HSP3標準ライブラリ:tasktray.as


タスクトレイ操作


tasktray.as

/*
 * tasktray.as
 *
 * タスクトレイ操作モジュール
 * ※image.as を先に読み込んでおいてください
 */

#module "tasktray"

#uselib "shell32.dll"
#func Shell_NotifyIcon "Shell_NotifyIconA" int, sptr

#define WM_APP  $8000 ; ※重複を防ぐためグローバルになっていないので注意
#define global WM_APP_TASKTRAY  (WM_APP + 100)

#define NIM_ADD 0
#define NIM_MODIFY 1
#define NIM_DELETE 2

#define NIF_MESSAGE 1
#define NIF_ICON 2
#define NIF_TIP 4

#define TRAY_ICON_SX    16
#define TRAY_ICON_SY    16

; 外から呼ばないでください
#deffunc tasktray_core int uMsg, int wnd, int uID, int uFlags, int uCallbackMessage, int hIcon, str szTip

        ; NOTIFYICONDATA構造体に設定
        dim nid,22
        nid.0 = 88
        nid.1 = wnd
        nid.2 = uID
        nid.3 = uFlags
        nid.4 = uCallbackMessage
        nid.5 = hIcon

        if uFlags & NIF_TIP {
                sdim _tip,64
                _tip = szTip
                memcpy nid,_tip,63,24
        }

        Shell_NotifyIcon uMsg, varptr(nid)

        ; 忘れずにアイコンを削除
        if uFlags & NIF_ICON {
                destroy_icon nid.5
        }

        return

/*
 * tasktray_create id, tip, x, y, mask, wid
 *   id         : 追加するタスクトレイアイコンに関連付けるID
 *   tip        : ツールチップ
 *   x          : 追加するイメージの左上X座標
 *   y          : 追加するイメージの左上Y座標
 *   mask       : 透過色(0xRRGGBBの形式)
 *   wid        : 追加するイメージの描画されているウインドウID
 *
 * 指定の画面のイメージをタスクトレイに追加します。
 * アイコンのサイズは 16x16 固定です。
 */
#deffunc tasktray_create int id, str tip, int x, int y, int mask, int wid

        sel_id = ginfo_sel
        gsel wid, 0

        ; アイコン作成
        create_imagelist TRAY_ICON_SX, TRAY_ICON_SY, 1 : hImgIcon = stat
        add_imagelist hImgIcon, x, y, TRAY_ICON_SX, TRAY_ICON_SY, mask
        geticon_imagelist hImgIcon, stat

        gsel sel_id, 0
        mref bmscr,67

        tasktray_core NIM_ADD, bmscr.13, id, NIF_MESSAGE | NIF_ICON | NIF_TIP, WM_APP_TASKTRAY, stat, tip

        destroy_imagelist hImgIcon

        return

/*
 * tasktray_modify_tip id, tip
 *   id         : タスクトレイアイコンに関連付けられているID
 *   tip        : ツールチップ
 *
 * タスクトレイアイコンのツールチップを変更します。
 */
#deffunc tasktray_modify_tip int id, str tip

        mref bmscr,67
        tasktray_core NIM_MODIFY, bmscr.13, id, NIF_TIP, 0, 0, tip
        return

/*
 * tasktray_modify_icon id, x, y, mask, wid
 *   id         : タスクトレイアイコンに関連付けられているID
 *   tip        : ツールチップ
 *   x          : 追加するイメージの左上X座標
 *   y          : 追加するイメージの左上Y座標
 *   mask       : 透過色(0xRRGGBBの形式)
 *   wid        : 追加するイメージの描画されているウインドウID
 *
 * タスクトレイアイコンに指定されているアイコンを変更します。
 */
#deffunc tasktray_modify_icon int id, int x, int y, int mask, int wid

        sel_id = ginfo_sel
        gsel wid, 0

        ; アイコン作成
        create_imagelist TRAY_ICON_SX, TRAY_ICON_SY, 1 : hImgIcon = stat
        add_imagelist hImgIcon, x, y, TRAY_ICON_SX, TRAY_ICON_SY, mask
        geticon_imagelist hImgIcon, stat

        gsel sel_id, 0
        mref bmscr,67

        tasktray_core NIM_MODIFY, bmscr.13, id, NIF_ICON, 0, stat, ""

        destroy_imagelist hImgIcon

        return

/*
 * tasktray_delete id
 *   id         : タスクトレイアイコンに関連付けられているID
 *
 * タスクトレイアイコンを削除します。
 */
#deffunc tasktray_delete int id

        mref bmscr,67
        tasktray_core NIM_DELETE, bmscr.13, id, 0, 0, 0, ""

        return

#global

tasktray_sample.hsp

#include "image.as"
#include "tasktray.as"

#define WM_APP                          $8000
#define WM_MOUSEMOVE            512
#define WM_LBUTTONDBLCLK        515
#define WM_LBUTTONDOWN          513
#define WM_LBUTTONUP            514
#define WM_MBUTTONDBLCLK        521
#define WM_MBUTTONDOWN          519
#define WM_MBUTTONUP            520
#define WM_RBUTTONDBLCLK        518
#define WM_RBUTTONDOWN          516
#define WM_RBUTTONUP            517

        onexit goto *quit

        font "MS ゴシック", 10
        pos 0, -2 : mes "HSP"
        pos 0,  6 : mes "3.0"

        tasktray_create 1,"HSP 3.0",0,0,$FFFFFF,0

        oncmd gosub *on_wm_app_tasktray,WM_APP_TASKTRAY

        stop

*on_wm_app_tasktray
        switch lParam
                case WM_RBUTTONDOWN
                        dialog "右クリック"
                        swbreak

                case WM_LBUTTONDBLCLK
                        dialog "左ダブルクリック"
                        swbreak

                default
                        mes strf("%08X,", lParam) + strf("%08X", wParam)
        swend
        return
        
*quit
        tasktray_delete 1
        end

tasktray_sample2.hsp

; タスクトレイスロット
#include "image.as"
#include "tasktray.as"

#define WM_APP                          $8000
#define WM_LBUTTONDBLCLK        515
#define WM_LBUTTONDOWN          513
#define WM_RBUTTONUP            517

#define SCROLL_Y        8

        gsel 0, -1
        onexit goto *quit
        oncmd gosub *on_wm_app_tasktray,WM_APP_TASKTRAY

        font "MS ゴシック", 16, 1

        color 255,255,255 : boxf  0,0,16,11*16
        color 255,  0,  0 : boxf 16,0,32,11*16
        color 255,255,  0 : boxf 32,0,48,11*16
        repeat 10+1
                n = 9 - (cnt \ 10)
                color   0,  0,  0 : pos    4,cnt * 16 : mes n
                color 255,255,255 : pos 16+4,cnt * 16 : mes n
                color   0,  0,  0 : pos 32+4,cnt * 16 : mes n
        loop

        tasktray_create 0,"",0,0,$FFFFFE,0
        tasktray_create 1,"",0,0,$FFFFFE,0
        tasktray_create 2,"",0,0,$FFFFFE,0

        roll_count = 3
        gosub *init_counter

*main
        await 10

        if(0 == roll_count) {
                wait 100
                goto *main
        }

        roll_count = 3
        repeat 3
                if f(cnt) {
                        num = cnt
                        info= 0
                        gosub *draw_counter
                
                        if 0 != n(cnt) \ SCROLL_Y {
                                f(cnt) |= 2
                        } else {
                                f(cnt) &= 1
                        }
                
                        n(cnt) = (n(cnt) + 1) \ (10 * SCROLL_Y)
                } else {
                        roll_count--
                }
        loop

        if 0 == roll_count {
                repeat 3
                        n(cnt) = n(cnt) / SCROLL_Y * SCROLL_Y
                loop

                if n.0 == n.1 && n.1 == n.2 {
                        info = 2
                } else {
                        info = 1
                }

                gosub *blink_counter

                gosub *init_counter
        }

        goto *main

*init_counter
        n = 0,0,0
        f = 3,3,3
        repeat 3
                n(cnt) = rnd(10) * SCROLL_Y
        loop
        return

*draw_counter

        y = (n(num) / SCROLL_Y \ 10) * 16
        y+=  n(num) \ SCROLL_Y * (16 / SCROLL_Y)
        tasktray_modify_icon num, info * 16, 159 - y, $FFFFFE, 0

        return

*blink_counter
        oncmd 0 ; 割り込み(マウス移動)がかかるとうまくいかないので
        info.1 = info

        repeat 6
                info = info.1 * (cnt \ 2)
                repeat 3
                        num = cnt
                        gosub *draw_counter
                loop
                wait 25
        loop

        oncmd 1
        return

*on_wm_app_tasktray
        oncmd 0 ; 割り込み(マウス移動)がかかるとうまくいかないので

        switch lParam
                case WM_RBUTTONUP
                        goto *quit
                        swbreak

                case WM_LBUTTONDBLCLK
                        roll_count = 3
                        swbreak

                case WM_LBUTTONDOWN
                        if 0 < roll_count : f(wParam) &= $E
                        swbreak
        swend

        oncmd 1
        return
        
*quit
        tasktray_delete 0
        tasktray_delete 1
        tasktray_delete 2
        end