1 | #require <gdiplus.ab>
|
---|
2 | #require <Classes/System/Drawing/Graphics.ab>
|
---|
3 | #require <Classes/System/Drawing/misc.ab>
|
---|
4 | #require <Classes/System/Drawing/Point.ab>
|
---|
5 | #require <Classes/System/Drawing/PointF.ab>
|
---|
6 | #require <Classes/System/Drawing/Rectangle.ab>
|
---|
7 | #require <Classes/System/Drawing/RectangleF.ab>
|
---|
8 | #require <Classes/System/Drawing/Size.ab>
|
---|
9 | #require <Classes/System/Drawing/SizeF.ab>
|
---|
10 | #require <Classes/System/Drawing/Color.ab>
|
---|
11 | #require <Classes/System/Drawing/CharacterRange.ab>
|
---|
12 | #require <Classes/System/Drawing/Font.ab>
|
---|
13 | #require <Classes/System/Drawing/FontFamily.ab>
|
---|
14 | #require <Classes/System/Drawing/Pen.ab>
|
---|
15 | #require <Classes/System/Drawing/Drawing2D/misc.ab>
|
---|
16 | #require <Classes/System/Drawing/Drawing2D/Matrix.ab>
|
---|
17 | #require <Classes/System/Drawing/Imaging/misc.ab>
|
---|
18 | #require <Classes/System/Drawing/Imaging/MetafileHeader.ab>
|
---|
19 | #require <Classes/System/Drawing/Text/misc.ab>
|
---|
20 |
|
---|
21 | /*!
|
---|
22 | @file
|
---|
23 | @brief 「Win32プログラミング講座 ~ Step32. アナログ時計を作る ~」のAB5 GDI+移植版
|
---|
24 | http://www.activebasic.com/help_center/articles/win32/step05/
|
---|
25 | メニューは未実装。
|
---|
26 |
|
---|
27 | @date 2008/07/20
|
---|
28 | @auther Egtra
|
---|
29 | */
|
---|
30 |
|
---|
31 | #require <Classes/ActiveBasic/Windows/UI/Form.ab>
|
---|
32 | #require <Classes/ActiveBasic/Windows/UI/Application.ab>
|
---|
33 | #require <Classes/ActiveBasic/Windows/UI/Timer.ab>
|
---|
34 |
|
---|
35 | Imports ActiveBasic.Windows.UI
|
---|
36 | Imports ActiveBasic.Math
|
---|
37 | Imports System
|
---|
38 |
|
---|
39 | #resource "UI_Sample.rc"
|
---|
40 |
|
---|
41 | Const PAI = 3.14159265358979323846264
|
---|
42 |
|
---|
43 | Class WatchForm
|
---|
44 | Inherits Form
|
---|
45 | Public
|
---|
46 | Sub WatchForm()
|
---|
47 | AddCreate(AddressOf(OnCreate))
|
---|
48 | AddDestroy(AddressOf(OnDestroy))
|
---|
49 | AddPaintDC(AddressOf(OnPaint_))
|
---|
50 | AddPaintBackground(AddressOf(OnPaintBackground_))
|
---|
51 | End Sub
|
---|
52 |
|
---|
53 | Protected
|
---|
54 | Override Sub GetCreateStruct(ByRef cs As CREATESTRUCT)
|
---|
55 | Super.GetCreateStruct(cs)
|
---|
56 | With cs
|
---|
57 | .cx = 240
|
---|
58 | .cy = 240
|
---|
59 | .lpszName = "clock"
|
---|
60 | End With
|
---|
61 | End Sub
|
---|
62 |
|
---|
63 | Private
|
---|
64 | Sub OnCreate(sender As Object, e As CreateArgs)
|
---|
65 | timer = New Timer(This)
|
---|
66 | With timer
|
---|
67 | .Interval = 100 '元に忠実にするなら10
|
---|
68 | .AddTick(AddressOf(Timer_OnTick))
|
---|
69 | .Start()
|
---|
70 | End With
|
---|
71 | GetLocalTime(st)
|
---|
72 |
|
---|
73 | Dim gsi = [1] As GdiplusStartupInput
|
---|
74 | GdiplusStartup(gdipToken, gsi, 0)
|
---|
75 | End Sub
|
---|
76 |
|
---|
77 | Sub OnDestroy(sender As Object, e As EventArgs)
|
---|
78 | ' GdiplusShutdown(gdipToken)
|
---|
79 | End Sub
|
---|
80 |
|
---|
81 | Sub Timer_OnTick(sender As Object, e As Args)
|
---|
82 | Dim wsec As Word
|
---|
83 | wsec = st.wSecond
|
---|
84 | GetLocalTime(st)
|
---|
85 | '秒針を動かす必要があるときは再描画する
|
---|
86 | If wsec <> st.wSecond Then Invalidate()
|
---|
87 | End Sub
|
---|
88 |
|
---|
89 | Sub OnPaintBackground_(sender As Object, e As PaintBackgroundArgs)
|
---|
90 | Dim hdc = e.Handle
|
---|
91 | Dim rc = This.ClientRect
|
---|
92 | Dim hbrOld = SelectObject(hdc, GetStockObject(WHITE_BRUSH))
|
---|
93 | ExtTextOut(hdc, 0, 0, ETO_OPAQUE, rc, "", 0, 0)
|
---|
94 | SelectObject(hdc, hbrOld)
|
---|
95 | End Sub
|
---|
96 |
|
---|
97 | Sub OnPaint_(sender As Object, e As PaintDCArgs)
|
---|
98 | Imports System.Drawing
|
---|
99 |
|
---|
100 | Dim pos As PointF
|
---|
101 |
|
---|
102 | Dim hdc = e.Handle
|
---|
103 | Dim g = Graphics.FromHDC(hdc)
|
---|
104 | g.Clear(&hffffffff)
|
---|
105 |
|
---|
106 | Dim rc = This.ClientRect
|
---|
107 | Dim CenterPos As PointF '針の中心位置
|
---|
108 | '針の中心位置
|
---|
109 | CenterPos.X = rc.right / 2
|
---|
110 | CenterPos.Y = rc.bottom / 2
|
---|
111 |
|
---|
112 | '3つとも元と違ってDouble型にしている。
|
---|
113 | Dim Length_Second = Math.Min(rc.bottom, rc.right) / 2.0 - 2.0 '秒針の長さ
|
---|
114 | Dim Length_Minute = Length_Second As Double '短針の長さ
|
---|
115 | Dim Length_Hour = Length_Minute * 0.70 '長針の長さ
|
---|
116 |
|
---|
117 | '短針
|
---|
118 | If st.wHour = 12 Then st.wHour = 0
|
---|
119 | pos.X = (CenterPos.X + Length_Hour * Sin(st.wHour * PAI / 6 + st.wMinute * PAI / 360)) As Single
|
---|
120 | pos.Y = (CenterPos.Y - Length_Hour * Cos(st.wHour * PAI / 6 + st.wMinute * PAI / 360)) As Single
|
---|
121 | Dim pen = New Pen(&hffff6400, 5.0)
|
---|
122 | g.DrawLine(pen, CenterPos, pos)
|
---|
123 |
|
---|
124 | '長針
|
---|
125 | pos.X = (CenterPos.X + Length_Minute * Sin(st.wMinute * PAI / 30 + st.wSecond * PAI / 1800)) As Single
|
---|
126 | pos.Y = (CenterPos.Y - Length_Minute * Cos(st.wMinute * PAI / 30 + st.wSecond * PAI / 1800)) As Single
|
---|
127 | pen.Color = &hffff0000
|
---|
128 | pen.Width = 2.0
|
---|
129 | g.DrawLine(pen, CenterPos, pos)
|
---|
130 |
|
---|
131 | '秒針
|
---|
132 | pos.X = (CenterPos.X + Length_Second * Sin(st.wSecond * PAI / 30)) As Long
|
---|
133 | pos.Y = (CenterPos.Y - Length_Second * Cos(st.wSecond * PAI / 30)) As Long
|
---|
134 | pen.Color = &hff000000
|
---|
135 | pen.Width = 1.0
|
---|
136 | g.DrawLine(pen, CenterPos, pos)
|
---|
137 | pen.Dispose()
|
---|
138 | g.Dispose()
|
---|
139 | End Sub
|
---|
140 |
|
---|
141 | timer As Timer
|
---|
142 | st As SYSTEMTIME
|
---|
143 | bTopMost As Long
|
---|
144 | gdipToken As ULONG_PTR
|
---|
145 | End Class
|
---|
146 |
|
---|
147 | Control.Initialize(GetModuleHandle(0))
|
---|
148 | Dim f = New WatchForm
|
---|
149 | f.CreateForm()
|
---|
150 | Application.Run(f)
|
---|
151 | End
|
---|