-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobals.vb
More file actions
144 lines (113 loc) · 4.55 KB
/
Globals.vb
File metadata and controls
144 lines (113 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
Imports System
Imports System.IO
Imports System.Runtime.ExceptionServices
Imports System.Runtime.InteropServices
Imports System.Security
Imports System.Security.AccessControl
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Threading
Imports Microsoft.Win32
Imports System.Math
Public Enum EditType
Create
Edit
End Enum
Public Module Globals
Public WithEvents ErrorLogFile As String = My.Application.Info.DirectoryPath & "\ErrorLog.txt"
Public WithEvents ErrorLog As StreamWriter
Public ppDip As Double 'pixels per Dip (Pixels Per Density)
Public ppiX As Double 'pixels per inch
Public ppiY As Double 'pixels per inch
Public taskbarHeight As Double
Public Sub RunException(ByVal ex As Exception)
ErrorLog.WriteLine(Now.ToString)
ErrorLog.WriteLine(ex.ToString)
ErrorLog.WriteLine(New String("*"c, 50))
End Sub
Public Sub RunExceptionWithMessageBox(ByVal ex As Exception)
ErrorLog.WriteLine(Now.ToString)
ErrorLog.WriteLine(ex.ToString)
ErrorLog.WriteLine(New String("*"c, 50))
MessageBox.Show(String.Format("{0} Error: {1}" & vbCrLf & vbCrLf & "{2}", ex.Source, ex.Message, ex.StackTrace, "Initialize Error", MessageBoxButton.OK, MessageBoxImage.Error))
End Sub
#Region "ToolBarEx Loaded"
Public Sub RemoveToolbarHandleAndOverflow(ByVal TB As ToolBarEx)
Dim overflowPanel = TryCast(TB.Template.FindName("PART_ToolBarOverflowPanel", TB), FrameworkElement)
If overflowPanel IsNot Nothing Then
overflowPanel.Opacity = 0
overflowPanel.Visibility = Visibility.Hidden
End If
Dim overflowGrid = TryCast(TB.Template.FindName("OverflowGrid", TB), FrameworkElement)
If overflowGrid IsNot Nothing Then
overflowGrid.Opacity = 0
overflowGrid.Visibility = Visibility.Hidden
End If
Dim mainPanelBorder = TryCast(TB.Template.FindName("MainPanelBorder", TB), FrameworkElement)
If mainPanelBorder IsNot Nothing Then
mainPanelBorder.Margin = New Thickness(0)
End If
End Sub
#End Region
#Region "Centroid"
Public Function Centroid(ByVal points As IEnumerable(Of Point)) As Point
Dim pt As Point = points.Aggregate(New With {
Key .xSum = 0.0,
Key .ySum = 0.0,
Key .n = 0
}, Function(acc, p) New With {
Key .xSum = acc.xSum + p.X,
Key .ySum = acc.ySum + p.Y,
Key .n = acc.n + 1
}, Function(acc) New Point(acc.xSum / acc.n, acc.ySum / acc.n))
Return pt
End Function
Public Function PolygonArea(ByVal points As IEnumerable(Of Point)) As Double
Dim e = points.GetEnumerator()
If Not e.MoveNext() Then
Return 0
End If
Dim first As Point = e.Current, last As Point = first
Dim area As Double = 0
Do While e.MoveNext()
Dim [next] As Point = e.Current
area += [next].X * last.Y - last.X * [next].Y
last = [next]
Loop
area += first.X * last.Y - last.X * first.Y
Return Abs(area)
End Function
''' <summary>
''' Works for any regular polygon.
''' </summary>
''' <param name="points">Vertices of the polygon/</param>
''' <returns></returns>
Public Function Perimeter(ByVal points As IEnumerable(Of Point)) As Double
Dim len As Double
len = points.Count * Point.Subtract(points(1), points(0)).Length
Return len
End Function
''' <summary>
''' Works for any polygon.
''' </summary>
''' <param name="points">Vertices of the polygon.</param>
''' <returns></returns>
Public Function GeneralPerimeter(ByVal points As IEnumerable(Of Point)) As Double
Dim len As Double
For i = 0 To points.Count - 2
len += Point.Subtract(points(i + 1), points(i)).Length
Next
len += Point.Subtract(points(0), points(points.Count - 1)).Length
Return len
End Function
#End Region
Public Function BitmapToBitmapSource(img As System.Drawing.Bitmap) As System.Windows.Media.Imaging.BitmapSource
Try
Return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(img.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())
Catch ex As Exception
ErrorLog.WriteLine(Now.ToString)
ErrorLog.WriteLine(ex.ToString)
Return Nothing
End Try
End Function
End Module