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

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


イメージ操作


image.as

/*
 * image.as
 *
 * イメージ操作モジュール
 */

#ifndef IMAGE_HSP_INCLUDED
#define global IMAGE_HSP_INCLUDED

#module Image

#uselib "comctl32"
#func InitCommonControls "InitCommonControls"
#func ImageList_Create "ImageList_Create" int, int, int, int, int
#func ImageList_AddMasked "ImageList_AddMasked" int, int, int
#func ImageList_Destroy "ImageList_Destroy" int
#func ImageList_Draw "ImageList_Draw" int, int, int, int, int, int
#func ImageList_GetIcon "ImageList_GetIcon" int,int,int

#uselib "user32"
#func DestroyIcon "DestroyIcon" int
#func DrawIconEx "DrawIconEx" int, int, int, int, int, int, int, int, int
#func LoadCursor "LoadCursorA" int, int
#func LoadCursorFromFile "LoadCursorFromFileA" sptr
#func SetCursor "SetCursor" int
#func SetClassLong "SetClassLongA" int, int, int

#uselib "shell32"
#func ExtractIconEx "ExtractIconExA" sptr, int, int, int, int

#uselib "gdi32"
#func CreateCompatibleBitmap "CreateCompatibleBitmap" int, int, int
#func CreateCompatibleDC "CreateCompatibleDC" int
#func SelectObject "SelectObject" int, int
#func DeleteObject "DeleteObject" int
#func BitBlt "BitBlt" int, int, int, int, int, int, int, int, int
#func DeleteDC "DeleteDC" int

// カーソル ID
#const global IDC_APPSTARTING 0x00007F8A // 砂時計付き矢印カーソル
#const global IDC_ARROW       0x00007F00 // 標準矢印カーソル
#const global IDC_CROSS       0x00007F03 // 十字カーソル
#const global IDC_HAND        0x00007F89 // Windows 98/Me/2000/XP:ハンドカーソル
#const global IDC_HELP        0x00007F8B // クエスチョンマーク付き矢印カーソル
#const global IDC_IBEAM       0x00007F01 // アイビーム (縦線) カーソル
#const global IDC_NO          0x00007F88 // 禁止カーソル
#const global IDC_SIZEALL     0x00007F86 // 4 方向矢印カーソル
#const global IDC_SIZENESW    0x00007F83 // 斜め左下がりの両方向矢印カーソル
#const global IDC_SIZENS      0x00007F85 // 上下両方向矢印カーソル
#const global IDC_SIZENWSE    0x00007F82 // 斜め右下がりの両方向矢印カーソル
#const global IDC_SIZEWE      0x00007F84 // 左右両方向矢印カーソル
#const global IDC_UPARROW     0x00007F04 // 垂直の矢印カーソル
#const global IDC_WAIT        0x00007F02 // 砂時計カーソル

#const ILC_MASK 0x00000001
#const ILC_COLOR24 0x00000018

#const ILD_TRANSPARENT 0x00000001

/*
 * extract_icon filename, index, size
 *   filename : アイコンのあるファイル名
 *   index    : アイコンのインデックス
 *   size     : アイコンのサイズ(1=大きいアイコン/0=小さいアイコン)
 * 
 * filenameのファイルから、アイコンを読み込みます。
 * 成功した場合は、statにアイコンのハンドルが返ります。
 * 失敗した場合は、statの値は0になります。
 * なお、読み込んだハンドルは、必ずdestroy_iconで破棄する必要があります。
 */
#deffunc extract_icon str filename, int index, int size, local hicon

    if size {
        ExtractIconEx filename, index, varptr(hicon), 0, 1
    } else {
        ExtractIconEx filename, index, 0, varptr(hicon), 1
    }
    if stat > 0 {
        return hicon
    }
    return 0

/*
 * draw_icon hicon, w, h
 *   hicon : アイコンのハンドル
 *   w     : アイコンの幅
 *   h     : アイコンの高さ
 * 
 * 現在の画面のカレントポジションに、アイコンを描画します。
 * この描画は、redraw 1 を行うまで画面には反映されません。
 */
#deffunc draw_icon int hicon, int w, int h

    DrawIconEx hdc, ginfo_cx, ginfo_cy, hicon, w, h, 0, 0, 3
    return

/*
 * destroy_icon hicon
 *   hicon : アイコンのハンドル
 * 
 * アイコンのハンドルを破棄します。
 */
#deffunc destroy_icon int hicon

    DestroyIcon hicon
    return

#deffunc change_cursor@Image int hcursor

    SetClassLong hwnd, -12, hcursor
    SetCursor hcursor
    return

/*
 * set_cursor filename
 *   filename : カーソルファイル名
 * 
 * カーソルファイルを読み込み、カーソルを変更します。
 */
#deffunc set_cursor str filename

    LoadCursorFromFile filename
    if stat : change_cursor stat
    return

/*
 * set_syscursor id
 *   id : カーソルID
 * 
 * システム定義のカーソルを読み込み、カーソルを変更します。
 * カーソルIDには、このファイルで定義されているIDC_*の定数を使用してください。
 */
#deffunc set_syscursor int id

    LoadCursor 0, id
    if stat : change_cursor stat
    return

#deffunc create_dib@Image int x, int y, int w, int h, local hbitmap, local hdcmem

    CreateCompatibleBitmap hdc, w, h
    hbitmap = stat

    CreateCompatibleDC hdc
    hdcmem = stat

    SelectObject hdcmem, hbitmap
    BitBlt hdcmem, 0, 0, w, h, hdc, x, y, 0xcc0020
    DeleteDC hdcmem
    return hbitmap

/*
 * create_imagelist w, h, count
 *   w     : イメージの幅
 *   h     : イメージの高さ
 *   count : イメージの枚数
 *
 * イメージリストを作成し、そのハンドルをstatに返します。
 * なお、作成したイメージリストは、必ずdestroy_imagelistで破棄する必要があります。
 */
#deffunc create_imagelist int w, int h, int count

    InitCommonControls

    ImageList_Create w, h, ILC_COLOR24 | ILC_MASK, count, 0
    return

/*
 * add_imagelist himagelist, x, y w, h, mask
 *   himagelist : イメージリストのハンドル
 *   x          : 追加するイメージの左上X座標
 *   y          : 追加するイメージの左上Y座標
 *   w          : 追加するイメージの幅
 *   h          : 追加するイメージの高さ
 *   mask       : 透過色(0xRRGGBBの形式)
 *
 * 現在の画面のイメージをイメージリストに追加し、statにイメージのインデックスを返します。
 */
#define global add_imagelist(%1, %2 = ginfo_cx, %3 = ginfo_cy, %4 = 0, %5 = 0, %6 = 0x000000) \
    add_imagelist@Image %1, %2, %3, %4, %5, %6

#deffunc add_imagelist@Image int himagelist, int x, int y, int w, int h, int mask, local hbitmap, local index

        create_dib x, y, w, h
    hbitmap = stat

    ImageList_AddMasked himagelist, hbitmap, ((mask << 16) & $FF0000) | ((mask << 8) & $FF00) | (mask & $FF)
    index = stat

    DeleteObject hbitmap
    return index

/*
 * draw_imagelist himagelist, index
 *   himagelist : イメージリストのハンドル
 *   index      : 描画するイメージのインデックス
 *
 * 現在の画面のカレントポジションに、イメージを描画します。
 * この描画は、redraw 1 を行うまで画面には反映されません。
 */
#deffunc draw_imagelist int himagelist, int index

    ImageList_Draw himagelist, index, hdc, ginfo_cx, ginfo_cy, ILD_TRANSPARENT
    return

/*
 * geticon_imagelist himagelist, index
 *   himagelist : イメージリストのハンドル
 *   index      : 取得するイメージのインデックス
 *
 * イメージをアイコンとして取得します。
 * 使い終わったアイコンは destroy_icon で削除してください。
 */
#deffunc geticon_imagelist int himagelist, int index

        ImageList_GetIcon himagelist, index, ILD_TRANSPARENT
        return stat

/*
 * destroy_imagelist himangelist
 *   himangelist : イメージリストのハンドル
 *
 * イメージリストのハンドルを破棄します。
 */
#deffunc destroy_imagelist int himangelist

    ImageList_Destroy himangelist
    return

#global

#endif

image_sample.hsp

#include "image.as"

    dialog "ico", 16
    if stat == 0 : end

    extract_icon refstr, 0, 1
    hicon = stat

    if hicon == 0 {
        dialog "アイコンの読み込みに失敗しました"
        end
    }

    pos 0, 0
    draw_icon hicon, 32, 32

    destroy_icon hicon

    set_syscursor IDC_WAIT

    await 500
    redraw

    set_syscursor IDC_ARROW