| 1 | #Region "Microsoft.VisualBasic::4bb180cca46a978033e3eabfbdcbe73e, Microsoft.VisualBasic.Core\ApplicationServices\Tools\WinForm\MockForm.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Class MockForm |
| 35 | ' |
| 36 | ' Constructor: (+1 Overloads) Sub New |
| 37 | ' Sub: (+2 Overloads) Dispose, Resize |
| 38 | ' |
| 39 | ' Class MockTerminal |
| 40 | ' |
| 41 | ' Constructor: (+1 Overloads) Sub New |
| 42 | ' |
| 43 | ' Function: getCmd |
| 44 | ' |
| 45 | ' Sub: init |
| 46 | ' |
| 47 | ' |
| 48 | ' /********************************************************************************/ |
| 49 | |
| 50 | #End Region |
| 51 | |
| 52 | Imports System.Drawing |
| 53 | Imports System.Threading |
| 54 | |
| 55 | Namespace Windows.Forms |
| 56 | |
| 57 | Public Class MockForm : Implements IDisposable |
| 58 | |
| 59 | Public Const SWP_SHOWWINDOW = &H40 |
| 60 | Public Const SWP_HIDEWINDOW = &H80 |
| 61 | Public Const WS_VSCROLL = &H200000 |
| 62 | Public Const WS_HSCROLL = &H100000 |
| 63 | Public Const WS_CAPTION = &HC00000 |
| 64 | Public Const WS_MINIMIZEBOX = &H20000 |
| 65 | Public Const WS_MAXIMIZEBOX = &H10000 |
| 66 | Public Const WS_OVERLAPPED = &H0& |
| 67 | Public Const WS_SYSMENU = &H40000 |
| 68 | Public Const HWND_TOPMOST = -1 |
| 69 | |
| 70 | Protected hwnd As IntPtr |
| 71 | |
| 72 | Public Declare Auto Function SetParent Lib "user32" Alias "SetParent" (hWndChild As IntPtr, hWndNewParent As IntPtr) As Integer |
| 73 | Public Declare Auto Function DestroyWindow Lib "user32" (hwnd As Long) As Long |
| 74 | Public Declare Auto Function FindWindow Lib "user32" Alias "FindWindowA" (lpClassName As String, lpWindowName As String) As IntPtr |
| 75 | Public Declare Auto Function MoveWindow Lib "user32" Alias "MoveWindow" (hwnd As IntPtr, X As Integer, Y As Integer, cx As Integer, cy As Integer, Flags As Boolean) As Boolean |
| 76 | Public Declare Auto Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (hwnd As IntPtr, nlndex As Integer, wNewLong As Long) As Long |
| 77 | Public Declare Auto Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (hwnd As Long, nIndex As Long) As Long |
| 78 | Public Declare Auto Function SetWindowPos Lib "user32" Alias "SetWindowPos" (hwnd As Long, hWndInsertAfter As Long, x As Long, y As Long, cx As Long, cy As Long, wFlags As Long) As Long |
| 79 | |
| 80 | ''' <summary> |
| 81 | ''' Construct mock from <see cref="FindWindow"/> |
| 82 | ''' </summary> |
| 83 | ''' <param name="found"></param> |
| 84 | Sub New(found As String) |
| 85 | hwnd = FindWindow(vbNullString, found) |
| 86 | End Sub |
| 87 | |
| 88 | Public Sub Resize(size As Size) |
| 89 | Call MoveWindow(hwnd, 0, 0, size.Width, size.Height, True) |
| 90 | End Sub |
| 91 | |
| 92 | #Region "IDisposable Support" |
| 93 | Private disposedValue As Boolean ' To detect redundant calls |
| 94 | |
| 95 | ' IDisposable |
| 96 | Protected Overridable Sub Dispose(disposing As Boolean) |
| 97 | If Not disposedValue Then |
| 98 | If disposing Then |
| 99 | ' TODO: dispose managed state (managed objects). |
| 100 | DestroyWindow(hwnd) ' 也不能正常调用,只能关闭自己 |
| 101 | End If |
| 102 | |
| 103 | ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. |
| 104 | ' TODO: set large fields to null. |
| 105 | End If |
| 106 | disposedValue = True |
| 107 | End Sub |
| 108 | |
| 109 | ' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources. |
| 110 | 'Protected Overrides Sub Finalize() |
| 111 | ' ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above. |
| 112 | ' Dispose(False) |
| 113 | ' MyBase.Finalize() |
| 114 | 'End Sub |
| 115 | |
| 116 | ' This code added by Visual Basic to correctly implement the disposable pattern. |
| 117 | Public Sub Dispose() Implements IDisposable.Dispose |
| 118 | ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above. |
| 119 | Dispose(True) |
| 120 | ' TODO: uncomment the following line if Finalize() is overridden above. |
| 121 | ' GC.SuppressFinalize(Me) |
| 122 | End Sub |
| 123 | #End Region |
| 124 | End Class |
| 125 | |
| 126 | Public Class MockTerminal : Inherits MockForm |
| 127 | |
| 128 | Dim parent As Control |
| 129 | |
| 130 | Sub New(parent As Control) |
| 131 | Call MyBase.New(getCmd) |
| 132 | Call init(parent) |
| 133 | End Sub |
| 134 | |
| 135 | ''' <summary> |
| 136 | ''' Init by win32 API |
| 137 | ''' </summary> |
| 138 | Private Sub init(parent As Control) |
| 139 | ' 直接嵌套到TabPage1内 |
| 140 | SetParent(hwnd, parent.Handle) |
| 141 | ' 设置窗体样式 |
| 142 | SetWindowLong(hwnd, (-16), GetWindowLong(hwnd, (-16)) And (Not WS_CAPTION) And (Not WS_VSCROLL) And (Not WS_HSCROLL)) |
| 143 | ' 改变子窗体位置 |
| 144 | SetWindowPos(parent.Handle, -1, 100, 100, 100, 100, SWP_SHOWWINDOW) |
| 145 | |
| 146 | Me.parent = parent |
| 147 | End Sub |
| 148 | |
| 149 | ''' <summary> |
| 150 | ''' Start a new cmd.exe instance for the terminal mock |
| 151 | ''' </summary> |
| 152 | ''' <returns></returns> |
| 153 | Private Shared Function getCmd() As String |
| 154 | Call Process.Start("cmd.exe") |
| 155 | Call Thread.Sleep(18) ' 过快下面的FindWindow有可能找不到窗体 |
| 156 | Return "c:\windows\system32\cmd.exe" |
| 157 | End Function |
| 158 | End Class |
| 159 | End Namespace |