スクリプト言語の HSP については、公式ホームページの「HSPTV!」をどうぞ。(戻る)
ここには、超小さいマイクロ・デジタル時計の全ソースを紹介します。
このμデジタル時計のソースは、次の7要素を表示/非表示に出来ます。
- 西暦
- 月日
- 曜日
- 時分
- 秒数
- 点滅コロン
- 点滅ドット
#const FLAG_YEAR %0000001 ;西暦(2018/) #const FLAG_DATE %0000010 ;月日(12/31) #const FLAG_WEEK %0000100 ;曜日(金) #const FLAG_TIME %0001000 ;時分(12:59) #const FLAG_SEC %0010000 ;秒数(.59) #const FLAG_COLON %0100000 ;点滅コロン(:) #const FLAG_POINT %1000000 ;点滅ドット(.)
上記のフラグ定数でμデジタル時計の表示形式を、ある程度カスタマイズできます。
お好みにカスタマイズするには、ソース上の「DESIGN_SW」定数を書き換えます。
#const int DESIGN_SW (FLAG_YEAR|FLAG_DATE|FLAG_WEEK|FLAG_TIME|FLAG_SEC|FLAG_POINT)
↓
#const int DESIGN_SW (FLAG_DATE|FLAG_WEEK|FLAG_TIME|FLAG_COLON)
上記の場合は、月日、曜日、時分、点滅コロンなので「12/31(金)23:59」という表示になります。
HSPを使ってる方は、ソースをコピー&ペーストして「F5」キーを押して実行してみると分かるでしょう。
μデジタル時計のソース
管理上このサンプルは sample328a.hsp のソースです。
//------------------------------------------------------------------------------ // μデジタル時計のサンプル by 科学太郎 //============================================================================== // 新規作成日:2018-09-15 (土) 12:12:00 // 最終更新日:2018-09-15 (土) 14:48:00 //------------------------------------------------------------------------------ #cmpopt varinit 1 #packopt hide 1 #packopt name __FILE__ # #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 FLAG_YEAR %0000001 ;西暦(2018/) #const FLAG_DATE %0000010 ;月日(12/31) #const FLAG_WEEK %0000100 ;曜日(金) #const FLAG_TIME %0001000 ;時分(12:59) #const FLAG_SEC %0010000 ;秒数(.59) #const FLAG_COLON %0100000 ;点滅コロン(:) #const FLAG_POINT %1000000 ;点滅ドット(.) //-------------------------------------- // 列挙定数(非表示タイプ) //-------------------------------------- #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) //-------------------------------------- // マクロ変数(ワークエリア領域) //-------------------------------------- #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_YEAR|FLAG_DATE|FLAG_WEEK|FLAG_TIME|FLAG_SEC|FLAG_POINT) //-------------------------------------- // メイン部 //-------------------------------------- *Init ;曜日 sdim WeekTable,7 WeekTable(0)="日","月","火","水","木","金","土" ;文字列 sdim sClock,100 ;表示文字列 sdim sClockChr,100 ;文字文字列 sdim sClockNum,100 ;数値文字列 ;その他 dim nSecSave ;秒数保存変数 dim nSecCount ;秒数カウンタ *Main ;計算 MakeDateTime sClock,DESIGN_SW x=HALF_SIZE+HALF_SIZE*strlen(sClock) 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 ;初期化 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):DrawClock:break if(nSecCount==1): if(nSecCount==2): if(nSecCount==3): if(nSecCount==4): if(nSecCount==5):DrawClock:break if(nSecCount==6): if(nSecCount==7): if(nSecCount==8): loop ;0.2秒単位 if(nSecCount & 1)==0{ TimerShowWindow 50 } ;0.1秒単位 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 DrawClock ;背景 gradf 0,0,ginfo_winX,ginfo_winY,1,$BB0000,$220000 ;外枠 color $80,$00,$00 line exWindow,cyWindow,cxWindow,cyWindow line exWindow,eyWindow line cxWindow,eyWindow line cxWindow,cyWindow ;時計 MakeDateTime sClock,DESIGN_SW SplitChrNum sClock,sClockChr,sClockNum,stat,1000 color $FF,$00,$00:pos QUAD_SIZE,QUAD_SIZE:mes sClockChr color $FF,$BB,$BB:pos QUAD_SIZE,QUAD_SIZE:mes sClockNum return //-------------------------------------- // 日時文字列の作成 //-------------------------------------- #deffunc MakeDateTime var _buff_,int _sw_ ;取得 y=gettime(0) m=gettime(1) w=gettime(2) d=gettime(3) h=gettime(4) n=gettime(5) s=gettime(6) t=gettime(7) ;点滅 c=':':if((_sw_) & FLAG_COLON):if(t>=500):c=' ' p='.':if((_sw_) & FLAG_POINT):if(t>=500):p=' ' ;作成 _buff_="" if((_sw_) & FLAG_YEAR):_buff_+=strf("%04d/",y) if((_sw_) & FLAG_DATE):_buff_+=strf("%02d/%02d",m,d) if((_sw_) & FLAG_WEEK):_buff_+=strf("(%s)",WeekTable(w)):else:_buff_+=" " if((_sw_) & FLAG_TIME):_buff_+=strf("%02d%c%02d",h,c,n) if((_sw_) & FLAG_SEC ):_buff_+=strf("%c%02d",p,s) _buff_=strtrim(_buff_) return strlen(_buff_) //-------------------------------------- // 文字と数値を分離 //-------------------------------------- #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 //------------------------------------------------------------------------------ // End of sample328a(μデジタル時計).hsp //------------------------------------------------------------------------------
使い方
- マウスの左ドラッグでウインドウが移動します。
- ウインドウをデスクトップ外に移動して離すと、デスクトップ内に自動的に移動します。(画面外補正の処理)
- ウインドウをデスクトップ端に近づけて離すと、デスクトップ端に自動的に吸着します。(画面端吸着の処理)
- ウインドウがデスクトップ端にくっついてると、デスクトップ端にマウスを乗せると自動的に非表示になります。
- ウインドウがデスクトップ隅にくっついてると、デスクトップ隅にマウスを乗せると自動的に非表示になります。
- キーボードの「Q」キーでμデジタル時計を終了します。
関連記事
- μデジタル時計のソース
- μモニター情報のソース 、分割(1) 、分割(2)
- μウインドウ情報のソース
※コメント投稿者のブログIDはブログ作成者のみに通知されます