1 | /*!
|
---|
2 | @file
|
---|
3 | @brief 「Win32プログラミング講座 ~ Step32. アナログ時計を作る ~」のAB5移植版
|
---|
4 | http://www.activebasic.com/help_center/articles/win32/step05/
|
---|
5 | メニューは未実装。
|
---|
6 |
|
---|
7 | @date 2008/07/20
|
---|
8 | @auther Egtra
|
---|
9 | */
|
---|
10 |
|
---|
11 | #require <Classes/ActiveBasic/Windows/UI/Form.ab>
|
---|
12 | #require <Classes/ActiveBasic/Windows/UI/Application.ab>
|
---|
13 | #require <Classes/ActiveBasic/Windows/UI/Timer.ab>
|
---|
14 |
|
---|
15 | Imports ActiveBasic.Windows.UI
|
---|
16 | Imports ActiveBasic.Math
|
---|
17 | Imports System
|
---|
18 |
|
---|
19 | #resource "UI_Sample.rc"
|
---|
20 |
|
---|
21 | Const PAI = 3.14159265358979323846264
|
---|
22 |
|
---|
23 | Class WatchForm
|
---|
24 | Inherits Form
|
---|
25 | Public
|
---|
26 | Sub WatchForm()
|
---|
27 | AddCreate(AddressOf(OnCreate))
|
---|
28 | AddPaintDC(AddressOf(OnPaint_))
|
---|
29 | AddPaintBackground(AddressOf(OnPaintBackground_))
|
---|
30 | End Sub
|
---|
31 |
|
---|
32 | Protected
|
---|
33 | Override Sub GetCreateStruct(ByRef cs As CREATESTRUCT)
|
---|
34 | Super.GetCreateStruct(cs)
|
---|
35 | With cs
|
---|
36 | .cx = 240
|
---|
37 | .cy = 240
|
---|
38 | .lpszName = "clock"
|
---|
39 | End With
|
---|
40 | End Sub
|
---|
41 |
|
---|
42 | Private
|
---|
43 | Sub OnCreate(sender As Object, e As CreateArgs)
|
---|
44 | timer = New Timer(This)
|
---|
45 | With timer
|
---|
46 | .Interval = 100 '元に忠実にするなら10
|
---|
47 | .AddTick(AddressOf(Timer_OnTick))
|
---|
48 | .Start()
|
---|
49 | End With
|
---|
50 | GetLocalTime(st)
|
---|
51 | End Sub
|
---|
52 |
|
---|
53 | Sub Timer_OnTick(sender As Object, e As Args)
|
---|
54 | Dim wsec As Word
|
---|
55 | wsec = st.wSecond
|
---|
56 | GetLocalTime(st)
|
---|
57 | '秒針を動かす必要があるときは再描画する
|
---|
58 | If wsec <> st.wSecond Then Invalidate()
|
---|
59 | End Sub
|
---|
60 |
|
---|
61 | Sub OnPaintBackground_(sender As Object, e As PaintBackgroundArgs)
|
---|
62 | Dim hdc = e.Handle
|
---|
63 | Dim rc = This.ClientRect
|
---|
64 | Dim hbrOld = SelectObject(hdc, GetStockObject(WHITE_BRUSH))
|
---|
65 | ExtTextOut(hdc, 0, 0, ETO_OPAQUE, rc, "", 0, 0)
|
---|
66 | SelectObject(hdc, hbrOld)
|
---|
67 | End Sub
|
---|
68 |
|
---|
69 | Sub OnPaint_(sender As Object, e As PaintDCArgs)
|
---|
70 | Dim hPen As HPEN, hOldPen As HPEN
|
---|
71 | Dim pos As POINTAPI
|
---|
72 |
|
---|
73 | Dim hdc = e.Handle
|
---|
74 | Dim rc = This.ClientRect
|
---|
75 |
|
---|
76 | Dim CenterPos As POINTAPI '針の中心位置
|
---|
77 | '針の中心位置
|
---|
78 | CenterPos.x = rc.right \ 2
|
---|
79 | CenterPos.y = rc.bottom \ 2
|
---|
80 |
|
---|
81 | '3つとも元と違ってDouble型にしている。
|
---|
82 | Dim Length_Second = Math.Min(rc.bottom, rc.right) / 2.0 - 2.0 '秒針の長さ
|
---|
83 | Dim Length_Minute = Length_Second As Double '短針の長さ
|
---|
84 | Dim Length_Hour = Length_Minute * 0.70 '長針の長さ
|
---|
85 |
|
---|
86 | '短針
|
---|
87 | If st.wHour=12 Then st.wHour=0
|
---|
88 | hPen=CreatePen(PS_SOLID,5,RGB(255,100,0))
|
---|
89 | hOldPen=SelectObject(hdc,hPen)
|
---|
90 | pos.x=(CenterPos.x + Length_Hour * Sin(st.wHour * PAI / 6 + st.wMinute * PAI / 360)) As Long
|
---|
91 | pos.y=(CenterPos.y - Length_Hour * Cos(st.wHour * PAI / 6 + st.wMinute * PAI / 360)) As Long
|
---|
92 | MoveToEx(hdc, CenterPos.x, CenterPos.y, ByVal NULL)
|
---|
93 | LineTo(hdc, pos.x, pos.y)
|
---|
94 | SelectObject(hdc,hOldPen)
|
---|
95 | DeleteObject(hPen)
|
---|
96 |
|
---|
97 | '長針
|
---|
98 | hPen=CreatePen(PS_SOLID,2,RGB(255,0,0))
|
---|
99 | hOldPen=SelectObject(hdc,hPen)
|
---|
100 | pos.x = (CenterPos.x + Length_Minute * Sin(st.wMinute * PAI / 30 + st.wSecond * PAI / 1800)) As Long
|
---|
101 | pos.y = (CenterPos.y - Length_Minute * Cos(st.wMinute * PAI / 30 + st.wSecond * PAI / 1800)) As Long
|
---|
102 | MoveToEx(hdc, CenterPos.x, CenterPos.y, ByVal NULL)
|
---|
103 | LineTo(hdc, pos.x, pos.y)
|
---|
104 | SelectObject(hdc, hOldPen)
|
---|
105 | DeleteObject(hPen)
|
---|
106 |
|
---|
107 | '秒針
|
---|
108 | hPen=GetStockObject(BLACK_PEN)
|
---|
109 | hOldPen=SelectObject(hdc,hPen)
|
---|
110 | pos.x = (CenterPos.x + Length_Second * Sin(st.wSecond * PAI / 30)) As Long
|
---|
111 | pos.y = (CenterPos.y - Length_Second * Cos(st.wSecond * PAI / 30)) As Long
|
---|
112 | MoveToEx(hdc, CenterPos.x, CenterPos.y, ByVal NULL)
|
---|
113 | LineTo(hdc, pos.x, pos.y)
|
---|
114 | SelectObject(hdc,hOldPen)
|
---|
115 | End Sub
|
---|
116 |
|
---|
117 | timer As Timer
|
---|
118 | st As SYSTEMTIME
|
---|
119 | ' bTopMost As Long
|
---|
120 | End Class
|
---|
121 |
|
---|
122 | Control.Initialize(GetModuleHandle(0))
|
---|
123 | Dim f = New WatchForm
|
---|
124 | f.CreateForm()
|
---|
125 | Application.Run(f)
|
---|