プログラミングのメモ帳(C/C++/HSP)

日々のプログラミングで気づいた点や小技集を紹介します。(Windows 10/XP/Vista、VC2017、HSP)

[HSP]μウインドウ情報のソース

2018年09月15日 23時03分00秒 | HSP講座

スクリプト言語の HSP については、公式ホームページの「HSPTV!」をどうぞ。(戻る)

ここには、超小さいマイクロ・ウインドウ情報の全ソースを紹介します。
このμウインドウ情報のソースは、次の5要素を表示/非表示に出来ます。

  1. ウインドウの横軸位置
  2. ウインドウの縦軸位置
  3. ウインドウの横サイズ
  4. ウインドウの縦サイズ
  5. カーソルのRGBカラー
#const FLAG_WINX        %00001          ;ウインドウの横軸位置
#const FLAG_WINY        %00010          ;ウインドウの縦軸位置
#const FLAG_WINW        %00100          ;ウインドウの横サイズ
#const FLAG_WINH        %01000          ;ウインドウの縦サイズ
#const FLAG_RGB         %10000          ;カーソルのRGBカラー

上記のフラグ定数でμデジタル時計の表示形式を、ある程度カスタマイズできます。
お好みにカスタマイズするには、ソース上の「DESIGN_SW」定数を書き換えます。

#const int  DESIGN_SW   (FLAG_WINX|FLAG_WINY|FLAG_WINW|FLAG_WINH|FLAG_RGB)

#const int  DESIGN_SW   (FLAG_WINW|FLAG_WINH|FLAG_RGB)

上記の場合は、横サイズ、縦サイズ、RGBカラーなので「W=9999 H=9999 RGB=FFCC00」という表示になります。
HSPを使ってる方は、ソースをコピー&ペーストして「F5」キーを押して実行してみると分かるでしょう。

μウインドウ情報のソース

管理上このサンプルは sample328c.hsp のソースです。

//------------------------------------------------------------------------------
// μウインドウ情報のサンプル by 科学太郎
//==============================================================================
// 新規作成日:2018-09-15 (土) 17:28:00
// 最終更新日:2018-09-15 (土) 18:40:00
//------------------------------------------------------------------------------
// @URL(sample75a.hsp)→「カラーコードの監視」
// @URL(sample75b.hsp)→「カラーコードの監視」
// @URL(sample117.hsp)→「マウス直下のウインドウ取得」
//------------------------------------------------------------------------------
#cmpopt varinit     1
#packopt hide       1
#packopt name       __FILE__
#
#include "Gdi32.as"
#include "User32.as"
#include "Kernel32.as"

//--------------------------------------
// 記号定数(API定数)
//--------------------------------------
#const global NULL                      0
#const global WM_CLOSE                  $00000010
#const global WM_TIMER                  $00000113
#const global WM_MOUSEMOVE              $00000200
#const global WM_LBUTTONDOWN            $00000201
#const global WM_NCLBUTTONDOWN          $000000A1
#const global HTCAPTION                 $00000002
#
#const global GWL_EXSTYLE               $FFFFFFEC
#const global WS_EX_TOOLWINDOW          $00000080
#
#const global SWP_NOSIZE                $00000001
#const global SWP_NOMOVE                $00000002
#const global SWP_NOZORDER              $00000004
#const global SWP_NOACTIVATE            $00000010
#const global SWP_FRAMECHANGED          $00000020
#const global SWP_SHOWWINDOW            $00000040
#const global SWP_HIDEWINDOW            $00000080
#const global SPI_GETWORKAREA           $00000030
#
#const global GA_ROOT                   $00000002

//------------------------------------------------------------------------------

//--------------------------------------
// 記号定数(フラグ値)
//--------------------------------------
#const FLAG_WINX        %00001          ;ウインドウの横軸位置
#const FLAG_WINY        %00010          ;ウインドウの縦軸位置
#const FLAG_WINW        %00100          ;ウインドウの横サイズ
#const FLAG_WINH        %01000          ;ウインドウの縦サイズ
#const FLAG_RGB         %10000          ;カーソルのRGBカラー

//--------------------------------------
// 列挙定数(非表示タイプ)
//--------------------------------------
#enum HIDE_NULL=0
#enum HIDE_LFTBTM
#enum HIDE_BOTTOM
#enum HIDE_RHTBTM
#enum HIDE_LEFT
#enum HIDE_CENTER
#enum HIDE_RIGHT
#enum HIDE_LFTTOP
#enum HIDE_TOP
#enum HIDE_RHTTOP

//--------------------------------------
// マクロ命令(表示/非表示)
//--------------------------------------
#define SetShowWindow   SetWindowPos hWnd,NULL,0,0,0,0,(SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_SHOWWINDOW)
#define SetHideWindow   SetWindowPos hWnd,NULL,0,0,0,0,(SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_HIDEWINDOW)

//------------------------------------------------------------------------------

//--------------------------------------
// 記号定数(全体)
//--------------------------------------
#const WID_MAIN         (0)
#const TID_CLOCK        (0)
#const TMS_CLOCK        (100)

//--------------------------------------
// 列挙定数(RECT)
//--------------------------------------
#enum rcLeft=0
#enum rcTop
#enum rcRight
#enum rcBottom
#enum rcWidth
#enum rcHeight

//--------------------------------------
// マクロ変数(ワークエリア領域)
//--------------------------------------
#define cxMonitor       rcMonitor(0)
#define cyMonitor       rcMonitor(1)
#define exMonitor       rcMonitor(2)
#define eyMonitor       rcMonitor(3)

//--------------------------------------
// マクロ変数(デスクトップ矩形)
//--------------------------------------
#define cxDesktop       (0)
#define cyDesktop       (0)
#define exDesktop       (ginfo_dispX-1)
#define eyDesktop       (ginfo_dispY-1)

//--------------------------------------
// マクロ変数(ウインドウ矩形)
//--------------------------------------
#define cxWindow        (0)
#define cyWindow        (0)
#define exWindow        (ginfo_winX-1)
#define eyWindow        (ginfo_winY-1)

//--------------------------------------
// 記号定数(サイズ)
//--------------------------------------
#define     FONT_NAME   "HG明朝E"
#const int  FONT_SIZE   (14)
#const int  HALF_SIZE   (FONT_SIZE/2)
#const int  QUAD_SIZE   (FONT_SIZE/4+0.5)
#const int  DESIGN_SW   (FLAG_WINX|FLAG_WINY|FLAG_WINW|FLAG_WINH|FLAG_RGB)

//--------------------------------------
// メイン部
//--------------------------------------
*Init
    ;情報
    dim rcWin,6                         ;ウインドウ情報
    dim nRGB                            ;カーソル位置色
    ;文字列
    sdim sWinInfo,100                   ;表示文字列
    sdim sWinInfoChr,100                ;文字文字列
    sdim sWinInfoNum,100                ;数値文字列
    ;その他
    dim nSecSave                        ;秒数保存変数
    dim nSecCount                       ;秒数カウンタ
*Main
    ;計算
    MakeWinInfo sWinInfo,DESIGN_SW
    x=HALF_SIZE+HALF_SIZE*strlen(sWinInfo)
    y=HALF_SIZE+FONT_SIZE
    ;作成
    bgscr WID_MAIN,x,y,SCREEN_NORMAL|SCREEN_FIXEDSIZE|SCREEN_HIDE
    font FONT_NAME,FONT_SIZE:title "μウインドウ情報のサンプル by 科学太郎"
    GetWindowLong hWnd,GWL_EXSTYLE
    SetWindowLong hWnd,GWL_EXSTYLE,stat|WS_EX_TOOLWINDOW
    SetWindowPos  hWnd,NULL,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED
    ;設定
    oncmd   gosub *OnTimer,         WM_TIMER
    oncmd   gosub *OnMouseMove,     WM_MOUSEMOVE
    oncmd   gosub *OnLButton1,      WM_LBUTTONDOWN
    onkey   gosub *OnKeyBoard
    ;初期化
    InitWinInfo
    InitHideWindow
    ;開始
    SetTimer hWnd,TID_CLOCK,TMS_CLOCK,NULL
    SendMsg  hWnd,WM_TIMER,TID_CLOCK,NULL
    ;表示
    gsel WID_MAIN,2
    stop
//--------------------------------------
// WM_TIMERメッセージの処理
//--------------------------------------
*OnTimer
    redraw 0
    n=gettime(6)
    if(n!=nSecSave):nSecSave=n:nSecCount=0
    repeat 1
        if(nSecCount==0):
        if(nSecCount==1):
        if(nSecCount==2):
        if(nSecCount==3):
        if(nSecCount==4):
        if(nSecCount==5):
        if(nSecCount==6):
        if(nSecCount==7):
        if(nSecCount==8):
    loop
    ;0.2秒単位
    if(nSecCount & 1)==0{
        TimerShowWindow 50
    }
    ;0.1秒単位
    InfoWinInfo
    DrawWinInfo
    nSecCount++
    redraw 1
    return 0
//--------------------------------------
// WM_MOUSEMOVEメッセージの処理
//--------------------------------------
*OnMouseMove
    MouseHideWindow
    return 0
//--------------------------------------
// WM_LBUTTONDOWNメッセージの処理
//--------------------------------------
*OnLButton1
    SendMsg hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0
    ClipDragWindow ginfo_sizeY
    return 0
//--------------------------------------
// キーボードの割り込み処理
//--------------------------------------
*OnKeyBoard
    if(iParam=='Q'):PostMessage hWnd,WM_CLOSE,0,0
    return
//--------------------------------------
// ウインドウ非表示の初期化
//--------------------------------------
#deffunc InitHideWindow
    dim ConvTable,16
    n=-1
    n++:ConvTable(n)=HIDE_CENTER        ;%0000=中央
    n++:ConvTable(n)=HIDE_LEFT          ;%0001=左側
    n++:ConvTable(n)=HIDE_RIGHT         ;%0010=右側
    n++:ConvTable(n)=HIDE_NULL          ;%0011=※左右に広いウインドウ(中央)
    n++:ConvTable(n)=HIDE_TOP           ;%0100=上側
    n++:ConvTable(n)=HIDE_LFTTOP        ;%0101=左上
    n++:ConvTable(n)=HIDE_RHTTOP        ;%0110=右上
    n++:ConvTable(n)=HIDE_NULL          ;%0111=※左右に広いウインドウ(上側)
    n++:ConvTable(n)=HIDE_BOTTOM        ;%1000=下側
    n++:ConvTable(n)=HIDE_LFTBTM        ;%1001=左下
    n++:ConvTable(n)=HIDE_RHTBTM        ;%1010=右下
    n++:ConvTable(n)=HIDE_NULL          ;%1011=※左右に広いウインドウ(下側)
    n++:ConvTable(n)=HIDE_NULL          ;%1100=※上下に広いウインドウ(中央)
    n++:ConvTable(n)=HIDE_NULL          ;%1101=※上下に広いウインドウ(左側)
    n++:ConvTable(n)=HIDE_NULL          ;%1110=※上下に広いウインドウ(右側)
    n++:ConvTable(n)=HIDE_NULL          ;%1111=※デスクトップよりも広いウインドウ
    return
//--------------------------------------
// ウインドウ非表示の開始
//--------------------------------------
#deffunc MouseHideWindow
    IsWindowVisible hWnd
    if(stat){
        ;カーソル位置
        x=1
        y=1
        if(ginfo_mx==cxDesktop):x=0
        if(ginfo_mx==exDesktop):x=2
        if(ginfo_my==cyDesktop):y=2
        if(ginfo_my==eyDesktop):y=0
        n=(y*3+x)+1
        if(n==5):return
        ;ウインドウ位置
        m=%0000
        if(ginfo_wx1<=cxDesktop)and(cxDesktop<=ginfo_wx2):m|=%0001;左側
        if(ginfo_wx1<=exDesktop)and(exDesktop<=ginfo_wx2):m|=%0010;右側
        if(ginfo_wy1<=cyDesktop)and(cyDesktop<=ginfo_wy2):m|=%0100;上側
        if(ginfo_wy1<=eyDesktop)and(eyDesktop<=ginfo_wy2):m|=%1000;下側
        ;非表示判定
        if(ConvTable(m)==n):SetHideWindow
    }
    return
//--------------------------------------
// ウインドウ非表示の復帰
//--------------------------------------
#deffunc TimerShowWindow int _gap_
    IsWindowVisible hWnd
    if(stat==0){
        if(ginfo_mx)<(ginfo_wx1-(_gap_)):SetShowWindow:return
        if(ginfo_mx)>(ginfo_wx2+(_gap_)):SetShowWindow:return
        if(ginfo_my)<(ginfo_wy1-(_gap_)):SetShowWindow:return
        if(ginfo_my)>(ginfo_wy2+(_gap_)):SetShowWindow:return
    }
    return
//--------------------------------------
// 画面外補正と画面端吸着
//--------------------------------------
#deffunc ClipDragWindow int _gap_
    dim rcMonitor,4
    SystemParametersInfo SPI_GETWORKAREA,0,varptr(rcMonitor),0
    ;初期値
    x=(ginfo_wx1)
    y=(ginfo_wy1)
    ;画面外補正
    if(ginfo_wx1<cxMonitor):x=(cxMonitor)
    if(ginfo_wy1<cyMonitor):y=(cyMonitor)
    if(ginfo_wx2>exMonitor):x=(exMonitor-ginfo_sizeX)
    if(ginfo_wy2>eyMonitor):y=(eyMonitor-ginfo_sizeY)
    ;画面端吸着
    if(cxMonitor<ginfo_wx1)and(ginfo_wx1<cxMonitor+(_gap_)):x=(cxMonitor)
    if(exMonitor>ginfo_wx2)and(ginfo_wx2>exMonitor-(_gap_)):x=(exMonitor-ginfo_sizeX)
    if(cyMonitor<ginfo_wy1)and(ginfo_wy1<cyMonitor+(_gap_)):y=(cyMonitor)
    if(eyMonitor>ginfo_wy2)and(ginfo_wy2>eyMonitor-(_gap_)):y=(eyMonitor-ginfo_sizeY)
    ;移動
    SetWindowPos hWnd,NULL,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER
    return
//--------------------------------------
// ウインドウ情報の描画
//--------------------------------------
#deffunc DrawWinInfo
    ;背景
    gradf 0,0,ginfo_winX,ginfo_winY,1,$BB6000,$221100
    ;外枠
    color $80,$40,$00
    line exWindow,cyWindow,cxWindow,cyWindow
    line exWindow,eyWindow
    line cxWindow,eyWindow
    line cxWindow,cyWindow
    ;情報
    MakeWinInfo sWinInfo,DESIGN_SW
    SplitChrNum sWinInfo,sWinInfoChr,sWinInfoNum,0,stat
    color $FF,$80,$00:pos QUAD_SIZE,QUAD_SIZE:mes sWinInfoChr
    color $FF,$C0,$40:pos QUAD_SIZE,QUAD_SIZE:mes sWinInfoNum
    return
//--------------------------------------
// 情報文字列の作成
//--------------------------------------
#deffunc MakeWinInfo var _buff_,int _sw_
    _buff_=""
    if((_sw_) & FLAG_WINX):_buff_+=strf(" X=%4d",rcWin.rcLeft)
    if((_sw_) & FLAG_WINY):_buff_+=strf(" Y=%4d",rcWin.rcTop)
    if((_sw_) & FLAG_WINW):_buff_+=strf(" W=%4d",rcWin.rcWidth)
    if((_sw_) & FLAG_WINH):_buff_+=strf(" H=%4d",rcWin.rcHeight)
    if((_sw_) & FLAG_RGB ):_buff_+=strf(" RGB=%06X",nRGB)
    _buff_=strtrim(_buff_)
    ;検索
    n=instr(_buff_,0,"RGB=")
    if(n<0):return 1000
    return(n+4)
//--------------------------------------
// 文字と数値を分離
//--------------------------------------
#deffunc SplitChrNum var _str_,var _chr_,var _num_,int _chrPos_,int _numPos_
    n=strlen(_str_)
    repeat n
        c=peek(_str_,cnt)
        IsDBCSLeadByte c
        if(stat){
            d=peek(_str_,cnt+1)
            poke(_chr_),(cnt+0),c:poke(_num_),(cnt+0),' '
            poke(_chr_),(cnt+1),d:poke(_num_),(cnt+1),' '
            continue(cnt+2)
        }
        else:if(cnt<_chrPos_){
            poke(_chr_),cnt,c
            poke(_num_),cnt,' '
        }
        else:if(cnt>=_numPos_){
            poke(_chr_),cnt,' '
            poke(_num_),cnt,c
        }
        else:if('0'<=c)and(c<='9')or(c=='.')or(c=='+')or(c=='-'){
            poke(_chr_),cnt,' '
            poke(_num_),cnt,c
        }
        else{
            poke(_chr_),cnt,c
            poke(_num_),cnt,' '
        }
    loop
    poke(_chr_),n
    poke(_num_),n
    return
//--------------------------------------
// ウインドウ情報の初期化
//--------------------------------------
#deffunc InitWinInfo
    hdcScreen=CreateDC("DISPLAY",NULL,NULL,NULL)
    return
//--------------------------------------
// ウインドウ情報の後始末
//--------------------------------------
#deffunc TermWinInfo onexit
    DeleteDC hdcScreen
    return
//--------------------------------------
// ウインドウ情報の収集
//--------------------------------------
#deffunc InfoWinInfo
    ;取得
    WindowFromPoint ginfo_mx,ginfo_my
    GetAncestor stat,GA_ROOT
    GetWindowRect stat,varptr(rcWin)
    ;サイズ
    rcWin.rcWidth =(rcWin.rcRight-rcWin.rcLeft)
    rcWin.rcHeight=(rcWin.rcBottom-rcWin.rcTop)
    ;色取得
    GetPixel hdcScreen,ginfo_mx,ginfo_my
    r=(stat>> 0) & $FF
    g=(stat>> 8) & $FF
    b=(stat>>16) & $FF
    nRGB=(r<<16)|(g<<8)|(b)
    return
//------------------------------------------------------------------------------
// End of sample328c(μウインドウ情報).hsp
//------------------------------------------------------------------------------

使い方

  • マウスの左ドラッグでウインドウが移動します。
  • ウインドウをデスクトップ外に移動して離すと、デスクトップ内に自動的に移動します。(画面外補正の処理)
  • ウインドウをデスクトップ端に近づけて離すと、デスクトップ端に自動的に吸着します。(画面端吸着の処理)
  • ウインドウがデスクトップ端にくっついてると、デスクトップ端にマウスを乗せると自動的に非表示になります。
  • ウインドウがデスクトップ隅にくっついてると、デスクトップ隅にマウスを乗せると自動的に非表示になります。
  • キーボードの「Q」キーでμウインドウ情報を終了します。

関連記事



コメント    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« [HSP]μモニター情報の分割... | トップ | [HSP]ストップウォッチのソ... »
最新の画像もっと見る

コメントを投稿

HSP講座」カテゴリの最新記事