1 | #Region "Microsoft.VisualBasic::916f52d8290217d38cc37ac363cb21f1, Microsoft.VisualBasic.Core\ApplicationServices\Tools\WinForm\SingleInstanceFormEntry.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 SingleInstanceFormEntry |
35 | ' |
36 | ' Properties: Arguments, Form |
37 | ' |
38 | ' Constructor: (+2 Overloads) Sub New |
39 | ' |
40 | ' Function: __getDefaultPos |
41 | ' |
42 | ' Sub: [AddHandler], __clean, __invokeEntry, Invoke |
43 | ' |
44 | ' |
45 | ' /********************************************************************************/ |
46 | |
47 | #End Region |
48 | |
49 | Imports System.Windows.Forms |
50 | Imports System.Drawing |
51 | Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel |
52 | Imports Microsoft.VisualBasic.Linq |
53 | |
54 | Namespace Windows.Forms |
55 | |
56 | ''' <summary> |
57 | ''' 只打开一个窗体,当窗体已经打开的时候,就会忽略当前的这一次单击事件,反之没有窗体被打开的时候就会打开新的窗体 |
58 | ''' </summary> |
59 | ''' <typeparam name="TForm"></typeparam> |
60 | Public Class SingleInstanceFormEntry(Of TForm As Form) |
61 | |
62 | Dim _clickEntry As Control |
63 | Dim __getPosition As Func(Of Control, Form, Point) |
64 | Dim _parentForm As Form |
65 | Dim _ShowModel As Boolean |
66 | |
67 | Public ReadOnly Property Form As TForm |
68 | |
69 | Public Property Arguments As Object() |
70 | |
71 | Sub New(ControlEntry As Control, |
72 | Optional ParentForm As Form = Nothing, |
73 | Optional GetPosition As Func(Of Control, Form, Point) = Nothing, |
74 | Optional ShowModelForm As Boolean = True) |
75 | |
76 | _clickEntry = ControlEntry |
77 | __getPosition = GetPosition |
78 | _parentForm = ParentForm |
79 | _ShowModel = ShowModelForm |
80 | |
81 | AddHandler _clickEntry.Click, AddressOf __invokeEntry |
82 | |
83 | If GetPosition Is Nothing AndAlso Not _parentForm Is Nothing Then |
84 | __getPosition = AddressOf __getDefaultPos |
85 | End If |
86 | End Sub |
87 | |
88 | ''' <summary> |
89 | ''' 不做任何位置的设置操作 |
90 | ''' </summary> |
91 | ''' <remarks></remarks> |
92 | Sub New(Optional ShowModelForm As Boolean = True) |
93 | _ShowModel = ShowModelForm |
94 | End Sub |
95 | |
96 | Public Sub [AddHandler](Handle As Action(Of Object, EventArgs)) |
97 | AddHandler _clickEntry.Click, New EventHandler(Sub(obj, args) Call Handle(obj, args)) |
98 | End Sub |
99 | |
100 | ''' <summary> |
101 | ''' 默认位置是控件的中间 |
102 | ''' </summary> |
103 | ''' <param name="Control"></param> |
104 | ''' <param name="Form"></param> |
105 | ''' <returns></returns> |
106 | ''' <remarks></remarks> |
107 | Private Shared Function __getDefaultPos(Control As UserControl, Form As Form) As Point |
108 | Dim Pt As Point = Form.PointToScreen(Control.Location) |
109 | Pt = New Point(CInt(Pt.X + Control.Width / 2), CInt(Pt.Y + Control.Height / 2)) |
110 | Return Pt |
111 | End Function |
112 | |
113 | Public Sub Invoke(ParamArray InvokeSets As NamedValue(Of Object)()) |
114 | If Not _shown Then |
115 | _shown = True |
116 | __invokeSets = InvokeSets |
117 | Call __invokeEntry(Nothing, Nothing) |
118 | Else |
119 | |
120 | End If |
121 | End Sub |
122 | |
123 | Dim _shown As Boolean = False |
124 | Dim __invokeSets As NamedValue(Of Object)() |
125 | |
126 | Private Sub __invokeEntry(sender As Object, EVtargs As EventArgs) |
127 | _Form = DirectCast(Activator.CreateInstance(GetType(TForm), Arguments), TForm) |
128 | |
129 | If Not __getPosition Is Nothing Then |
130 | Dim pt As Point = __getPosition(_clickEntry, Form) |
131 | Form.Location = pt |
132 | End If |
133 | |
134 | If Not __invokeSets.IsNullOrEmpty Then |
135 | Dim setValue As New SetValue(Of TForm)() |
136 | |
137 | For Each arg As NamedValue(Of Object) In __invokeSets |
138 | Call setValue.InvokeSetValue(Form, arg.Name, arg.Value) |
139 | Next |
140 | End If |
141 | |
142 | If _ShowModel Then |
143 | Call Form.ShowDialog() |
144 | Call __clean() |
145 | Else |
146 | Call Form.Show() |
147 | AddHandler Form.FormClosed, AddressOf __clean |
148 | End If |
149 | End Sub |
150 | |
151 | Private Sub __clean() |
152 | Call Form.Free() |
153 | _shown = False |
154 | End Sub |
155 | End Class |
156 | End Namespace |