[77] | 1 | ' Classes/System/Windows/Forms/Control.ab
|
---|
| 2 |
|
---|
| 3 | #ifndef __SYSTEM_WINDOWS_FORMS_CONTROL_AB__
|
---|
| 4 | #define __SYSTEM_WINDOWS_FORMS_CONTROL_AB__
|
---|
| 5 |
|
---|
[388] | 6 | /*
|
---|
[223] | 7 | #require <Classes/System/Windows/Forms/misc.ab>
|
---|
| 8 | #require <Classes/System/Windows/Forms/CreateParams.ab>
|
---|
| 9 | #require <Classes/System/Windows/Forms/Message.ab>
|
---|
| 10 | #require <Classes/System/Windows/Forms/PaintEventArgs.ab>
|
---|
| 11 | #require <Classes/System/misc.ab>
|
---|
| 12 | #require <Classes/System/Math.ab>
|
---|
| 13 | #require <Classes/System/Threading/WaitHandle.ab>
|
---|
[388] | 14 | */
|
---|
[223] | 15 | #require <Classes/System/Drawing/Color.ab>
|
---|
| 16 | #require <Classes/System/Drawing/Point.ab>
|
---|
| 17 | #require <Classes/System/Drawing/Size.ab>
|
---|
| 18 | #require <Classes/System/Drawing/Rectangle.ab>
|
---|
[388] | 19 | /*
|
---|
[223] | 20 | #require <Classes/System/Runtime/InteropServices/GCHandle.ab>
|
---|
[303] | 21 | #require <Classes/ActiveBasic/Windows/WindowHandle.sbp>
|
---|
[282] | 22 | #require <Classes/ActiveBasic/Strings/Strings.ab>
|
---|
[388] | 23 | */
|
---|
[282] | 24 | Namespace System
|
---|
| 25 | Namespace Windows
|
---|
| 26 | Namespace Forms
|
---|
| 27 |
|
---|
| 28 | Namespace Detail
|
---|
| 29 |
|
---|
[223] | 30 | TypeDef InvokeProc = *Function(p As VoidPtr) As VoidPtr
|
---|
| 31 |
|
---|
[77] | 32 | Class AsyncResultForInvoke
|
---|
[381] | 33 | Implements System.IAsyncResult
|
---|
[77] | 34 | Public
|
---|
| 35 | ' Properties
|
---|
| 36 | Sub AsyncResultForInvoke(h As HANDLE)
|
---|
| 37 | waitHandle.Handle = h
|
---|
| 38 | End Sub
|
---|
| 39 |
|
---|
[381] | 40 | Function AsyncState() As Object
|
---|
[223] | 41 | Return Nothing
|
---|
[77] | 42 | End Function
|
---|
| 43 |
|
---|
[381] | 44 | Function AsyncWaitHandle() As System.Threading.WaitHandle
|
---|
[223] | 45 | Return waitHandle
|
---|
[77] | 46 | End Function
|
---|
| 47 |
|
---|
[381] | 48 | Function CompletedSynchronously() As Boolean
|
---|
[303] | 49 | Return False
|
---|
[77] | 50 | End Function
|
---|
| 51 |
|
---|
[381] | 52 | Function IsCompleted() As Boolean
|
---|
[240] | 53 | Return waitHandle.WaitOne(0, False)
|
---|
[77] | 54 | End Function
|
---|
| 55 |
|
---|
| 56 | Const Function Result() As VoidPtr
|
---|
| 57 | Return result
|
---|
| 58 | End Function
|
---|
| 59 |
|
---|
| 60 | Sub Result(r As VoidPtr)
|
---|
| 61 | result = r
|
---|
| 62 | End Sub
|
---|
| 63 |
|
---|
| 64 | Private
|
---|
[282] | 65 | waitHandle As System.Threading.WaitHandle
|
---|
[77] | 66 | result As VoidPtr
|
---|
| 67 | End Class
|
---|
| 68 |
|
---|
| 69 | Class AsyncInvokeData
|
---|
| 70 | Public
|
---|
[223] | 71 | FuncPtr As InvokeProc
|
---|
[77] | 72 | Data As *VoidPtr
|
---|
[223] | 73 | AsyncResult As AsyncResultForInvoke
|
---|
[77] | 74 | End Class
|
---|
| 75 |
|
---|
[282] | 76 | End Namespace 'Detail
|
---|
| 77 |
|
---|
[77] | 78 | Class Control
|
---|
[303] | 79 |
|
---|
[223] | 80 | ' Inherits IWin32Window
|
---|
[77] | 81 | Public
|
---|
[132] | 82 | '---------------------------------------------------------------------------
|
---|
| 83 | ' Public Properties
|
---|
[223] | 84 | Function AllowDrop() As Boolean
|
---|
[77] | 85 | End Function
|
---|
| 86 |
|
---|
[303] | 87 | 'Anchor
|
---|
| 88 | 'AutoScrollOffset
|
---|
| 89 | 'AutoSize
|
---|
| 90 | 'BackColor下
|
---|
| 91 | 'BackgroundImage
|
---|
| 92 | 'BackgroundImageLayout
|
---|
| 93 | '-BindingContext
|
---|
| 94 | 'Bottom 下
|
---|
| 95 | 'Bounds 下
|
---|
| 96 | 'CanFocus
|
---|
| 97 | 'CanSelect
|
---|
| 98 | 'Capture
|
---|
| 99 | '-CausesValidation
|
---|
| 100 | 'CheckForIllegalCrossThreadCalls
|
---|
| 101 |
|
---|
[223] | 102 | /*Override*/ Function Handle() As HWND
|
---|
[77] | 103 | Return wnd.HWnd
|
---|
| 104 | End Function
|
---|
| 105 |
|
---|
| 106 | ' IDropTargetを実装するのでDragAcceptFiles関数は呼ばない。
|
---|
[223] | 107 | Sub AllowDrop(a As Boolean)
|
---|
[77] | 108 | End Sub
|
---|
| 109 |
|
---|
[223] | 110 | Const Function Visible() As Boolean
|
---|
[77] | 111 | Return wnd.Visible
|
---|
| 112 | End Function
|
---|
| 113 |
|
---|
[223] | 114 | Sub Visible(v As Boolean)
|
---|
[77] | 115 | wnd.Visible(v)
|
---|
| 116 | End Sub
|
---|
| 117 |
|
---|
| 118 | Const Function Text() As String
|
---|
| 119 | Return text
|
---|
| 120 | End Function
|
---|
| 121 |
|
---|
[240] | 122 | Sub Text(t As String)
|
---|
[77] | 123 | text = t
|
---|
| 124 | Dim e As EventArgs
|
---|
| 125 | OnTextChanged(e)
|
---|
| 126 | End Sub
|
---|
| 127 |
|
---|
[223] | 128 | Const Function Enabled() As Boolean
|
---|
[77] | 129 | Return wnd.Enabled
|
---|
| 130 | End Function
|
---|
| 131 |
|
---|
[223] | 132 | Sub Enabled(e As Boolean)
|
---|
[77] | 133 | ' OnEnabledChangedはWM_ENABLE経由で呼ばれる
|
---|
| 134 | wnd.Enabled(e)
|
---|
| 135 | End Sub
|
---|
| 136 |
|
---|
| 137 | Const Function Bounds() As Rectangle
|
---|
| 138 | Dim wr As RECT
|
---|
| 139 | wr = wnd.WindowRect
|
---|
[223] | 140 | Dim r = New Rectangle(wr)
|
---|
[77] | 141 | Dim parent = Parent
|
---|
[223] | 142 | If Object.ReferenceEquals(parent, Nothing) Then
|
---|
[77] | 143 | Return parent->RectangleToClient(r)
|
---|
| 144 | Else
|
---|
| 145 | Return r
|
---|
| 146 | End If
|
---|
| 147 | End Function
|
---|
[388] | 148 | /* BoundsSpecifiedが使用不能なのでコメントアウト
|
---|
[223] | 149 | Sub Bounds(r As Rectangle)
|
---|
[77] | 150 | SetBoundsCore(r.X, r.Y, r.Width, r.Height, BoundsSpecified.All)
|
---|
| 151 | End Sub
|
---|
[388] | 152 | */
|
---|
[77] | 153 | Const Function Location() As Point
|
---|
| 154 | Return Bounds.Location
|
---|
| 155 | End Function
|
---|
[388] | 156 | /*
|
---|
[77] | 157 | Sub Location(p As Point)
|
---|
| 158 | SetBoundsCore(p.X, p.Y, 0, 0, BoundsSpecified.Location)
|
---|
| 159 | End Sub
|
---|
[388] | 160 | */
|
---|
[77] | 161 | Const Function Size() As Size
|
---|
| 162 | Return Bounds.Size
|
---|
| 163 | End Function
|
---|
[388] | 164 | /*
|
---|
[77] | 165 | Sub Size(s As Size)
|
---|
| 166 | SetBoundsCore(0, 0, s.Width, s.Height, BoundsSpecified.Size)
|
---|
| 167 | End Sub
|
---|
| 168 |
|
---|
| 169 | Const Function ClientRectangle() As Rectangle
|
---|
[223] | 170 | Return New Rectangle(wnd.ClientRect)
|
---|
[77] | 171 | End Function
|
---|
| 172 |
|
---|
| 173 | Const Function ClientSize() As Size
|
---|
| 174 | Return ClientRectangle.Size
|
---|
| 175 | End Function
|
---|
[388] | 176 | */
|
---|
[77] | 177 | Const Function Left() As Long
|
---|
| 178 | Dim b = Bounds
|
---|
| 179 | Return b.Left
|
---|
| 180 | End Function
|
---|
[388] | 181 | /*
|
---|
[77] | 182 | Sub Left(l As Long)
|
---|
| 183 | SetBoundsCore(l, 0, 0, 0, BoundsSpecified.X)
|
---|
| 184 | End Sub
|
---|
[388] | 185 | */
|
---|
[77] | 186 | Const Function Top() As Long
|
---|
| 187 | Dim b = Bounds
|
---|
| 188 | Return b.Top
|
---|
| 189 | End Function
|
---|
[388] | 190 | /*
|
---|
[77] | 191 | Sub Top(t As Long)
|
---|
| 192 | SetBoundsCore(0, t, 0, 0, BoundsSpecified.Y)
|
---|
| 193 | End Sub
|
---|
[388] | 194 | */
|
---|
[77] | 195 | Const Function Width() As Long
|
---|
| 196 | Dim b = Bounds
|
---|
| 197 | Return b.Width
|
---|
| 198 | End Function
|
---|
[388] | 199 | /*
|
---|
[77] | 200 | Sub Width(w As Long)
|
---|
| 201 | SetBoundsCore(0, 0, w, 0, BoundsSpecified.Width)
|
---|
| 202 | End Sub
|
---|
[388] | 203 | */
|
---|
[77] | 204 | Const Function Height() As Long
|
---|
| 205 | Dim b = Bounds
|
---|
| 206 | Return b.Height
|
---|
| 207 | End Function
|
---|
[388] | 208 | /*
|
---|
[77] | 209 | Sub Height(h As Long)
|
---|
| 210 | SetBoundsCore(0, 0, 0, h, BoundsSpecified.Height)
|
---|
| 211 | End Sub
|
---|
[388] | 212 | */
|
---|
[77] | 213 | Const Function Right() As Long
|
---|
[240] | 214 | Dim b = Bounds
|
---|
| 215 | Return b.Left + b.Width
|
---|
[77] | 216 | End Function
|
---|
| 217 |
|
---|
| 218 | Const Function Bottom() As Long
|
---|
[240] | 219 | Dim b = Bounds
|
---|
| 220 | Return b.Top + b.Height
|
---|
[77] | 221 | End Function
|
---|
[303] | 222 | /*
|
---|
[77] | 223 | Const Function PointToScreen(p As Point) As Point
|
---|
[223] | 224 | PointToScreen = New Point
|
---|
[303] | 225 | PointToScreen.X = p.X
|
---|
| 226 | PointToScreen.Y = p.Y
|
---|
| 227 | wnd.ClientToScreen(ByVal ObjPtr(PointToScreen) As *POINTAPI)
|
---|
[77] | 228 | End Function
|
---|
| 229 |
|
---|
| 230 | Const Function PointToClient(p As Point) As Point
|
---|
[223] | 231 | PointToScreen = New Point
|
---|
[303] | 232 | PointToClient.X = p.X
|
---|
| 233 | PointToClient.Y = p.Y
|
---|
| 234 | wnd.ScreenToClient(ByVal ObjPtr(PointToClient) As *POINTAPI)
|
---|
[77] | 235 | End Function
|
---|
[303] | 236 | */
|
---|
[223] | 237 | Const Function RectangleToScreen(r As Rectangle) As Rectangle
|
---|
| 238 | Dim rc = r.ToRECT
|
---|
| 239 | wnd.ClientToScreen(rc)
|
---|
| 240 | Return New Rectangle(rc)
|
---|
[77] | 241 | End Function
|
---|
| 242 |
|
---|
[223] | 243 | Const Function RectangleToClient(r As Rectangle) As Rectangle
|
---|
[303] | 244 | Dim rc = r.ToRECT()
|
---|
[223] | 245 | wnd.ScreenToClient(rc)
|
---|
| 246 | Return New Rectangle(rc)
|
---|
[77] | 247 | End Function
|
---|
| 248 |
|
---|
[223] | 249 | Const Function InvokeRequired() As Boolean
|
---|
[303] | 250 | Return wnd.ThreadId <> GetCurrentThreadId()
|
---|
[77] | 251 | End Function
|
---|
| 252 |
|
---|
| 253 | Const Virtual Function BackColor() As Color
|
---|
| 254 | Return bkColor
|
---|
| 255 | End Function
|
---|
| 256 |
|
---|
| 257 | Virtual Sub BackColor(c As Color)
|
---|
| 258 | c = bkColor
|
---|
[303] | 259 | OnBackColorChanged(New EventArgs)
|
---|
[77] | 260 | End Sub
|
---|
| 261 |
|
---|
[223] | 262 | Function Parent() As Control
|
---|
[132] | 263 | Return parent
|
---|
| 264 | End Function
|
---|
| 265 |
|
---|
[223] | 266 | Const Function IsHandleCreated() As Boolean
|
---|
[303] | 267 | Return wnd.HWnd <> 0 Or IsWindow(wnd.HWnd) <> FALSE
|
---|
[223] | 268 | End Function
|
---|
| 269 |
|
---|
[132] | 270 | Static Function DefaultBackColor() As Color
|
---|
| 271 | Return Color.FromArgb(255, 255, 255)
|
---|
| 272 | End Function
|
---|
| 273 |
|
---|
| 274 | '---------------------------------------------------------------------------
|
---|
[77] | 275 | ' Constractors
|
---|
| 276 |
|
---|
| 277 | Sub Control()
|
---|
| 278 | Dim sz = DefaultSize()
|
---|
[388] | 279 | init(Nothing, "", 100, 100, sz.Width, sz.Height)
|
---|
[77] | 280 | End Sub
|
---|
| 281 |
|
---|
[223] | 282 | Sub Control(text As String)
|
---|
[77] | 283 | Dim sz = DefaultSize()
|
---|
[388] | 284 | init(Nothing, text, 100, 100, sz.Width, sz.Height)
|
---|
[77] | 285 | End Sub
|
---|
| 286 |
|
---|
[223] | 287 | Sub Control(parent As Control, text As String)
|
---|
[77] | 288 | Dim sz = DefaultSize()
|
---|
[388] | 289 | init(parent, text, 100, 100, sz.Width, sz.Height)
|
---|
[77] | 290 | End Sub
|
---|
| 291 |
|
---|
[223] | 292 | Sub Control(text As String, left As Long, top As Long, width As Long, height As Long)
|
---|
[388] | 293 | init(Nothing, text, left, top, width, height)
|
---|
[77] | 294 | End Sub
|
---|
| 295 |
|
---|
[223] | 296 | Sub Control(parent As Control, text As String, left As Long, top As Long, width As Long, height As Long)
|
---|
[388] | 297 | init(parent, text, left, top, width, height)
|
---|
| 298 | End Sub
|
---|
| 299 |
|
---|
| 300 | Private
|
---|
| 301 |
|
---|
| 302 | Sub init(parent As Control, text As String, left As Long, top As Long, width As Long, height As Long)
|
---|
[303] | 303 | This.parent = parent
|
---|
[388] | 304 | ' CreateControl()
|
---|
[77] | 305 | End Sub
|
---|
[388] | 306 |
|
---|
[77] | 307 |
|
---|
[132] | 308 | '---------------------------------------------------------------------------
|
---|
[77] | 309 | ' Destractor
|
---|
[388] | 310 | Public
|
---|
[77] | 311 | Virtual Sub ~Control()
|
---|
[240] | 312 | If Not Object.ReferenceEquals(wnd, Nothing) Then
|
---|
| 313 | If wnd.IsWindow Then
|
---|
| 314 | wnd.Destroy() ' 暫定
|
---|
| 315 | End If
|
---|
[77] | 316 | End If
|
---|
| 317 | End Sub
|
---|
| 318 |
|
---|
[132] | 319 | '---------------------------------------------------------------------------
|
---|
| 320 | ' Public Methods
|
---|
[303] | 321 | /*
|
---|
[77] | 322 | ' 同期関数呼出、Controlが作成されたスレッドで関数を実行する。
|
---|
| 323 | ' 関数は同期的に呼び出されるので、関数が終わるまでInvokeは制御を戻さない。
|
---|
[282] | 324 | Function Invoke(pfn As System.Windows.Forms.Detail.InvokeProc, p As VoidPtr) As VoidPtr
|
---|
[77] | 325 | Return wnd.SendMessage(WM_CONTROL_INVOKE, p As WPARAM, pfn As LPARAM) As VoidPtr
|
---|
| 326 | End Function
|
---|
| 327 |
|
---|
| 328 | ' 非同期関数呼出、Controlが作成されたスレッドで関数を実行する。
|
---|
| 329 | ' 関数は非同期的に呼び出されるので、BeginInvokeはすぐに制御を戻す。
|
---|
| 330 | ' 後にEndInvokeを呼び出すことにより、関数の戻り値を受け取れる。
|
---|
[282] | 331 | Function BeginInvoke(pfn As System.Windows.Forms.Detail.InvokeProc, p As VoidPtr) As System.IAsyncResult
|
---|
[77] | 332 | ' EndInvokeがDeleteする
|
---|
[282] | 333 | Dim asyncResult = New System.Windows.Forms.Detail.AsyncResultForInvoke(CreateEvent(0, FALSE, FALSE, 0))
|
---|
| 334 | Dim asyncInvokeData = New System.Windows.Forms.Detail.AsyncInvokeData
|
---|
[223] | 335 | With asyncInvokeData
|
---|
[77] | 336 | .FuncPtr = pfn
|
---|
| 337 | .Data = p
|
---|
[223] | 338 | .AsyncResult = asyncResult
|
---|
[77] | 339 | End With
|
---|
[282] | 340 | Dim gch = System.Runtime.InteropServices.GCHandle.Alloc(asyncInvokeData)
|
---|
| 341 | wnd.PostMessage(WM_CONTROL_BEGININVOKE, 0, System.Runtime.InteropServices.GCHandle.ToIntPtr(gch))
|
---|
[303] | 342 | Return asyncResult
|
---|
[77] | 343 | End Function
|
---|
| 344 |
|
---|
| 345 | ' BeginInvokeで呼び出した関数の戻り値を受け取る。
|
---|
| 346 | ' その関数がまだ終了していない場合、終了するまで待機する。
|
---|
[282] | 347 | Function EndInvoke(ar As System.IAsyncResult) As VoidPtr
|
---|
[223] | 348 | ar.WaitHandle.WaitOne()
|
---|
[282] | 349 | Dim arInvoke = ar As System.Windows.Forms.Detail.AsyncResultForInvoke
|
---|
[223] | 350 | Return arInvoke.Result
|
---|
[77] | 351 | End Function
|
---|
[303] | 352 | */
|
---|
[77] | 353 | ' 与えられたウィンドウハンドルがControl(若しくはその派生クラス)の
|
---|
| 354 | ' インスタンスに対応するものであった場合、
|
---|
[223] | 355 | ' 元のインスタンスを返す。
|
---|
| 356 | ' そうでなければNothingを返す。
|
---|
| 357 | Static Function FromHandle(hwnd As HWND) As Control
|
---|
[77] | 358 | If IsWindow(hwnd) Then
|
---|
[223] | 359 | If GetClassLongPtr(hwnd, GCW_ATOM) = atom Then
|
---|
[282] | 360 | Dim gch = System.Runtime.InteropServices.GCHandle.FromIntPtr(GetWindowLongPtr(hwnd, GWLP_THIS))
|
---|
[223] | 361 | Return gch.Target As Control
|
---|
[77] | 362 | End If
|
---|
| 363 | End If
|
---|
[223] | 364 | Return Nothing As Control
|
---|
[77] | 365 | End Function
|
---|
| 366 |
|
---|
| 367 | Virtual Sub ResetText()
|
---|
| 368 | text = ""
|
---|
| 369 | End Sub
|
---|
| 370 |
|
---|
[223] | 371 | Override Function ToString() As String
|
---|
[77] | 372 | Return text
|
---|
| 373 | End Function
|
---|
| 374 |
|
---|
| 375 | ' Wrapper Methods
|
---|
| 376 | Sub BringToFront()
|
---|
| 377 | wnd.BringToTop()
|
---|
| 378 | End Sub
|
---|
| 379 |
|
---|
| 380 | Sub Hide()
|
---|
| 381 | wnd.Show(SW_HIDE)
|
---|
| 382 | End Sub
|
---|
| 383 |
|
---|
| 384 | Sub Show()
|
---|
[303] | 385 | Debug
|
---|
[77] | 386 | wnd.Show(SW_SHOW)
|
---|
| 387 | End Sub
|
---|
| 388 |
|
---|
| 389 | Sub Update()
|
---|
| 390 | wnd.Update()
|
---|
| 391 | End Sub
|
---|
| 392 |
|
---|
[240] | 393 | Sub CreateControl()
|
---|
| 394 | CreateHandle() '暫定
|
---|
| 395 | End Sub
|
---|
| 396 |
|
---|
[77] | 397 | Protected
|
---|
[240] | 398 |
|
---|
[132] | 399 | '---------------------------------------------------------------------------
|
---|
| 400 | ' Protected Properties
|
---|
[223] | 401 | ' Const Virtual Function CanRaiseEvents() As Boolean
|
---|
| 402 | Virtual Function CreateParams() As CreateParams
|
---|
| 403 | Return createParams
|
---|
[77] | 404 | End Function
|
---|
| 405 | ' Virtual Function DefaultCursor() As Cursor
|
---|
| 406 |
|
---|
| 407 | Virtual Function DefaultSize() As Size
|
---|
[388] | 408 | Return New Size(300, 300)
|
---|
[77] | 409 | End Function
|
---|
| 410 |
|
---|
| 411 | ' Function FontHeight() As Long
|
---|
| 412 | ' Sub FontHeight(h As Long)
|
---|
| 413 |
|
---|
| 414 | ' Const Virtual Function Cursor() As Cursor
|
---|
[240] | 415 | ' Virtual Sub Cursor(c As Cursor)
|
---|
[77] | 416 |
|
---|
[132] | 417 | '---------------------------------------------------------------------------
|
---|
| 418 | ' Protected Methods
|
---|
[77] | 419 | Virtual Sub CreateHandle()
|
---|
[388] | 420 | Debug
|
---|
| 421 | If Not Object.ReferenceEquals(wnd, Nothing) Then
|
---|
| 422 | If wnd.HWnd <> 0 Then
|
---|
| 423 | Exit Sub
|
---|
| 424 | End If
|
---|
| 425 | End If
|
---|
| 426 |
|
---|
[77] | 427 | Dim createParams = CreateParams()
|
---|
[303] | 428 | Dim gch = System.Runtime.InteropServices.GCHandle.Alloc(This)
|
---|
| 429 | TlsSetValue(tlsIndex, System.Runtime.InteropServices.GCHandle.ToIntPtr(gch) As VoidPtr)
|
---|
[240] | 430 | With createParams
|
---|
| 431 | Dim pText As PCTSTR
|
---|
| 432 | If String.IsNullOrEmpty(text) Then
|
---|
| 433 | pText = "" As PCTSTR
|
---|
| 434 | Else
|
---|
| 435 | pText = ToTCStr(text)
|
---|
| 436 | End If
|
---|
| 437 |
|
---|
| 438 | If CreateWindowEx(.ExStyle, atom As ULONG_PTR As PCSTR, pText, .Style, _
|
---|
[388] | 439 | .X, .Y, .Width, .Height, _
|
---|
| 440 | .Parent, 0, hInstance, 0) = 0 Then
|
---|
[77] | 441 | ' Error
|
---|
[223] | 442 | Dim buf[1023] As TCHAR
|
---|
| 443 | wsprintf(buf, ToTCStr(Ex"Control: CreateWindowEx failed. Error code: &h%08X\r\n"), GetLastError())
|
---|
[77] | 444 | OutputDebugString(buf)
|
---|
[388] | 445 |
|
---|
| 446 | gch.Free()
|
---|
[77] | 447 | ' Debug
|
---|
| 448 | ExitThread(0)
|
---|
| 449 | End If
|
---|
| 450 | End With
|
---|
[388] | 451 |
|
---|
[77] | 452 | End Sub
|
---|
| 453 |
|
---|
[223] | 454 | Virtual Sub DefWndProc(m As Message)
|
---|
[77] | 455 | m.Result = DefWindowProc(m.HWnd, m.Msg, m.WParam, m.LParam)
|
---|
| 456 | End Sub
|
---|
| 457 |
|
---|
[223] | 458 | Virtual Sub WndProc(m As Message)
|
---|
[77] | 459 | With m
|
---|
| 460 | Select Case .Msg
|
---|
| 461 | Case WM_GETTEXTLENGTH
|
---|
[282] | 462 | .Result = text.Length 'ToDo: Unicode対応
|
---|
[77] | 463 | Case WM_GETTEXT
|
---|
[282] | 464 | Dim size = System.Math.Min(.WParam As SIZE_T, (text.Length + 1) As SIZE_T)
|
---|
| 465 | ActiveBasic.Strings.ChrCopy(.LParam As PCTSTR, ToTCStr(text), size)
|
---|
[77] | 466 | .Result = size
|
---|
| 467 | Case WM_SETTEXT
|
---|
[223] | 468 | text = New String(.LParam As PCTSTR)
|
---|
[77] | 469 | Case WM_ENABLE
|
---|
[282] | 470 | OnEnabledChanged(System.EventArgs.Empty)
|
---|
[77] | 471 | Case WM_ERASEBKGND
|
---|
| 472 | ' OnPaintBackgroundに移すべき
|
---|
[132] | 473 | Dim hdc = .WParam As HDC
|
---|
[77] | 474 | Dim hbr = CreateSolidBrush(bkColor.ToCOLORREF())
|
---|
| 475 | Dim hbrOld = SelectObject(hdc, hbr)
|
---|
[132] | 476 | Dim rc = wnd.ClientRect
|
---|
[77] | 477 | Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom)
|
---|
| 478 | SelectObject(hdc, hbrOld)
|
---|
| 479 | DeleteObject(hbr)
|
---|
| 480 | Case WM_CONTROL_INVOKE
|
---|
[282] | 481 | Dim pfn = .LParam As System.Windows.Forms.Detail.InvokeProc
|
---|
[77] | 482 | .Result = pfn(m.WParam As VoidPtr) As LRESULT
|
---|
| 483 | Case WM_CONTROL_BEGININVOKE
|
---|
| 484 | OnControlBeginInvoke(m)
|
---|
[223] | 485 | Case WM_CREATE
|
---|
[282] | 486 | OnHandleCreated(System.EventArgs.Empty)
|
---|
[240] | 487 | Case WM_DESTROY
|
---|
[282] | 488 | OnHandleDestroyed(System.EventArgs.Empty)
|
---|
[388] | 489 |
|
---|
| 490 | Case WM_LBUTTONDOWN
|
---|
| 491 | Goto *ButtonDown
|
---|
| 492 | *ButtonDown
|
---|
[77] | 493 | Case Else
|
---|
| 494 | DefWndProc(m)
|
---|
| 495 | End Select
|
---|
| 496 | End With
|
---|
| 497 | End Sub
|
---|
| 498 |
|
---|
| 499 | Virtual Sub SetClientSizeCore(x As Long, y As Long)
|
---|
| 500 | Dim rc As RECT
|
---|
| 501 | With rc
|
---|
| 502 | .left = 0
|
---|
| 503 | .top = 0
|
---|
| 504 | .right = x
|
---|
| 505 | .bottom = y
|
---|
| 506 | End With
|
---|
| 507 | Dim hasMenu = FALSE As BOOL
|
---|
| 508 | If wnd.Parent As HWND = 0 Then
|
---|
| 509 | If wnd.Menu <> 0 Then
|
---|
| 510 | hasMenu = TRUE
|
---|
| 511 | End If
|
---|
| 512 | End If
|
---|
| 513 | AdjustWindowRectEx(rc, wnd.Style, hasMenu, wnd.ExStyle)
|
---|
| 514 | wnd.Move(rc)
|
---|
| 515 | End Sub
|
---|
| 516 |
|
---|
| 517 | Virtual Sub SetBoundsCore(x As Long, y As Long, width As Long, height As Long, bs As BoundsSpecified)
|
---|
[223] | 518 | ' If Not (bs As DWord And BoundsSpecified.X As DWord) Then
|
---|
[77] | 519 | x = Left
|
---|
[223] | 520 | ' End If
|
---|
| 521 | ' If Not (bs As DWord And BoundsSpecified.Y As DWord) Then
|
---|
[77] | 522 | y = Right
|
---|
[223] | 523 | ' End If
|
---|
| 524 | ' If Not (bs As DWord And BoundsSpecified.Width As DWord) Then
|
---|
[77] | 525 | width = Width
|
---|
[223] | 526 | ' End If
|
---|
| 527 | ' If Not (bs As DWord And BoundsSpecified.Height As DWord) Then
|
---|
[77] | 528 | height = Height
|
---|
[223] | 529 | ' End If
|
---|
[77] | 530 | wnd.Move(x, y, width, height)
|
---|
| 531 | End Sub
|
---|
| 532 |
|
---|
| 533 | Virtual Sub NotifyInvalidate(r As Rectangle)
|
---|
| 534 | Dim rc As RECT
|
---|
| 535 | rc = r.ToRECT()
|
---|
| 536 | wnd.InvalidateRect(rc)
|
---|
| 537 | End Sub
|
---|
| 538 |
|
---|
[388] | 539 | ' Virtual Sub OnPaintBackground(e As PaintEventArgs) : End Sub
|
---|
[282] | 540 | Virtual Sub OnEnabledChanged(e As System.EventArgs) : End Sub
|
---|
| 541 | Virtual Sub OnBackColorChanged(e As System.EventArgs) : End Sub
|
---|
| 542 | Virtual Sub OnHandleCreated(e As System.EventArgs) : End Sub
|
---|
| 543 | Virtual Sub OnHandleDestroyed(e As System.EventArgs) : End Sub
|
---|
| 544 | Virtual Sub OnTextChanged(e As System.EventArgs)
|
---|
[223] | 545 | wnd.SetText(ToTCStr(text))
|
---|
[77] | 546 | End Sub
|
---|
| 547 |
|
---|
| 548 | Private
|
---|
| 549 | ' Member variables
|
---|
[303] | 550 | wnd As ActiveBasic.Windows.WindowHandle
|
---|
[77] | 551 | text As String
|
---|
[223] | 552 | parent As Control
|
---|
[77] | 553 | bkColor As Color
|
---|
| 554 |
|
---|
| 555 | Static createParams As CreateParams
|
---|
| 556 |
|
---|
| 557 | 'ウィンドウ作成時にウィンドウプロシージャへThisを伝えるためのもの
|
---|
| 558 | Static tlsIndex As DWord
|
---|
| 559 |
|
---|
| 560 | Static hInstance As HINSTANCE
|
---|
| 561 | Static atom As ATOM
|
---|
| 562 |
|
---|
| 563 | Static Const WM_CONTROL_INVOKE = WM_USER + 0 As DWord
|
---|
| 564 | Static Const WM_CONTROL_BEGININVOKE = WM_USER + 1 As DWord
|
---|
| 565 |
|
---|
[223] | 566 | Static Const WindowClassName = "ActiveBasic Control"
|
---|
[77] | 567 | Public
|
---|
| 568 | Static Sub Initialize(hinst As HINSTANCE)
|
---|
| 569 | tlsIndex = TlsAlloc()
|
---|
| 570 | hInstance = hinst
|
---|
| 571 |
|
---|
| 572 | Dim wcx As WNDCLASSEX
|
---|
| 573 | With wcx
|
---|
| 574 | .cbSize = Len (wcx)
|
---|
| 575 | .style = CS_HREDRAW Or CS_VREDRAW Or CS_DBLCLKS
|
---|
| 576 | .lpfnWndProc = AddressOf (WndProcFirst)
|
---|
| 577 | .cbClsExtra = 0
|
---|
| 578 | .cbWndExtra = SizeOf (LONG_PTR) * 2
|
---|
| 579 | .hInstance = hinst
|
---|
| 580 | .hIcon = 0
|
---|
| 581 | .hCursor = LoadImage(0, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE Or LR_SHARED) As HCURSOR
|
---|
| 582 | .hbrBackground = 0
|
---|
| 583 | .lpszMenuName = 0
|
---|
[240] | 584 | .lpszClassName = ToTCStr(WindowClassName)
|
---|
[77] | 585 | .hIconSm = 0
|
---|
| 586 | End With
|
---|
| 587 | atom = RegisterClassEx(wcx)
|
---|
| 588 | If atom = 0 Then
|
---|
[223] | 589 | Dim buf[1023] As TCHAR
|
---|
| 590 | wsprintf(buf, ToTCStr(Ex"Control: RegisterClasseEx failed. Error code: &h%08X\r\n"), GetLastError())
|
---|
[77] | 591 | OutputDebugString(buf)
|
---|
| 592 | Debug
|
---|
| 593 | ExitThread(0)
|
---|
| 594 | End If
|
---|
| 595 |
|
---|
| 596 | With createParams
|
---|
| 597 | ' 値は暫定的なもの
|
---|
| 598 | .Style = WS_OVERLAPPEDWINDOW
|
---|
| 599 | .ExStyle = WS_EX_APPWINDOW
|
---|
[388] | 600 | .Caption = String.Empty
|
---|
| 601 | .X = 0
|
---|
| 602 | .Y = 0
|
---|
| 603 | .Width = 0
|
---|
| 604 | .Height = 0
|
---|
[77] | 605 | End With
|
---|
| 606 | End Sub
|
---|
| 607 |
|
---|
| 608 | Static Sub Uninitialize()
|
---|
| 609 | UnregisterClass(atom As ULONG_PTR As PCSTR, hInstance)
|
---|
| 610 | TlsFree(tlsIndex)
|
---|
| 611 | End Sub
|
---|
| 612 | Private
|
---|
| 613 | Static Const GWLP_THIS = SizeOf (LONG_PTR) * 0 As Long
|
---|
| 614 | Static Const GWLP_TAG = SizeOf (LONG_PTR) * 1 As Long
|
---|
| 615 |
|
---|
| 616 | ' Windowsから呼ばれるウィンドウプロシージャ。WndProc
|
---|
| 617 | Static Function WndProcFirst(hwnd As HWND, msg As DWord, wp As WPARAM, lp As LPARAM) As LRESULT
|
---|
[223] | 618 | Dim rThis = Control.FromHandle(hwnd) As Control
|
---|
| 619 | If Object.ReferenceEquals(rThis As Object, Nothing) Then
|
---|
| 620 | Dim gchValue = TlsGetValue(tlsIndex) As LONG_PTR
|
---|
[282] | 621 | Dim gch = System.Runtime.InteropServices.GCHandle.FromIntPtr(gchValue)
|
---|
[223] | 622 | rThis = gch.Target As Control
|
---|
[77] | 623 | ' ウィンドウが作られて最初にWndProcFirstが呼ばれたとき
|
---|
[223] | 624 | If Object.ReferenceEquals(rThis, Nothing) Then
|
---|
[77] | 625 | ' あってはならない事態
|
---|
| 626 | Debug
|
---|
[240] | 627 | ExitThread(-1)
|
---|
[77] | 628 | End If
|
---|
[303] | 629 | rThis.wnd = New ActiveBasic.Windows.WindowHandle(hwnd)
|
---|
[223] | 630 | SetWindowLongPtr(hwnd, GWLP_THIS, gchValue)
|
---|
[388] | 631 | ElseIf msg = WM_NCDESTROY Then
|
---|
| 632 | Dim gch = System.Runtime.InteropServices.GCHandle.FromIntPtr(GetWindowLongPtr(hwnd, GWLP_THIS))
|
---|
| 633 | gch.Free()
|
---|
[77] | 634 | End If
|
---|
[240] | 635 |
|
---|
| 636 | Dim m = Message.Create(hwnd, msg, wp, lp)
|
---|
[223] | 637 | rThis.WndProc(m)
|
---|
[77] | 638 | Return m.Result
|
---|
| 639 | End Function
|
---|
| 640 |
|
---|
| 641 | ' BeginInvokeが呼ばれたときの処理
|
---|
[223] | 642 | Sub OnControlBeginInvoke(m As Message)
|
---|
[282] | 643 | Dim gch = System.Runtime.InteropServices.GCHandle.FromIntPtr(m.LParam)
|
---|
| 644 | Dim data = gch.Target As System.Windows.Forms.Detail.AsyncInvokeData
|
---|
[223] | 645 | With data
|
---|
| 646 | Dim pfn = .FuncPtr
|
---|
| 647 | .AsyncResult.Result = pfn(.Data)
|
---|
| 648 | SetEvent(.AsyncResult.AsyncWaitHandle.Handle)
|
---|
| 649 | End With
|
---|
[77] | 650 | End Sub
|
---|
| 651 | End Class
|
---|
| 652 |
|
---|
[282] | 653 | Namespace Detail
|
---|
[77] | 654 | Class _System_ControlIinitializer
|
---|
| 655 | Public
|
---|
| 656 | Sub _System_ControlIinitializer(hinst As HINSTANCE)
|
---|
[282] | 657 | System.Windows.Forms.Control.Initialize(hinst)
|
---|
[77] | 658 | End Sub
|
---|
| 659 |
|
---|
| 660 | Sub ~_System_ControlIinitializer()
|
---|
[282] | 661 | System.Windows.Forms.Control.Uninitialize()
|
---|
[77] | 662 | End Sub
|
---|
| 663 | End Class
|
---|
| 664 |
|
---|
| 665 | #ifndef _SYSTEM_NO_INITIALIZE_CONTROL_
|
---|
| 666 | Dim _System_ControlInitializer As _System_ControlIinitializer(GetModuleHandle(0))
|
---|
| 667 | #endif '_SYSTEM_NO_INITIALIZE_CONTROL_
|
---|
| 668 |
|
---|
[282] | 669 | End Namespace
|
---|
| 670 |
|
---|
| 671 | End Namespace 'Forms
|
---|
| 672 | End Namespace 'Widnows
|
---|
| 673 | End Namespace 'System
|
---|
| 674 |
|
---|
[77] | 675 | #endif '__SYSTEM_WINDOWS_FORMS_CONTROL_AB__
|
---|
[240] | 676 |
|
---|