麻烦各位帮忙把以下C#源码转为VB.Net源码
public static class ControlExtension
{
const float cos30 = 0.866f;
const float sin30 = 0.5f;
public static void BindWaterMark(this Control ctrl)
{
if (ctrl == null || ctrl.IsDisposed)
return;
// 绘制水印
if (ctrl.HaveEventHandler("Paint", "BindWaterMark"))
return;
ctrl.Paint += (sender, e) =>
{
System.Windows.Forms.Control paintCtrl = sender as System.Windows.Forms.Control;
var g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// 计算控件位置
int offsetX = 0;
int offsetY = 0;
while (paintCtrl.Parent != null)
{
offsetX += paintCtrl.Location.X;
offsetY += paintCtrl.Location.Y;
paintCtrl = paintCtrl.Parent;
}
// 平移画布到窗体左上角
g.TranslateTransform(0 - offsetX, 0 - offsetY + 32);
//逆时针旋转30度
g.RotateTransform(-30);
for (int x = 0; x < e.ClipRectangle.Right + 64 + offsetX; x += 128)
{
for (int y = 0; y < e.ClipRectangle.Bottom + 64 + offsetY; y += 128)
{
// 计算文字起点位置
float x1 = cos30 * x - sin30 * y;
float y1 = sin30 * x + cos30 * y;
//画上文字
g.DrawString("水印文字" , new Font("微软雅黑", 16, FontStyle.Regular ),
new SolidBrush(Color.FromArgb(50, 100, 100, 100)), x1, y1);
}
}
};
// 子控件绑定绘制事件
foreach (System.Windows.Forms.Control child in ctrl.Controls)
BindWaterMark(child);
}
public static bool HaveEventHandler(this Control control, string eventName, string methodName)
{
//获取Control类定义的所有事件的信息
PropertyInfo pi = (control.GetType()).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
//获取Control对象control的事件处理程序列表
EventHandlerList ehl = (EventHandlerList)pi.GetValue(control, null);
//获取Control类 eventName 事件的字段信息
FieldInfo fieldInfo = (typeof(Control)).GetField(string.Format("Event{0}", eventName), BindingFlags.Static | BindingFlags.NonPublic);
//用获取的 eventName 事件的字段信息,去匹配 control 对象的事件处理程序列表,获取control对象 eventName 事件的委托对象
//事件使用委托定义的,C#中的委托时多播委托,可以绑定多个事件处理程序,当事件发生时,这些事件处理程序被依次执行
//因此Delegate对象,有一个GetInvocationList方法,用来获取这个委托已经绑定的所有事件处理程序
Delegate d = ehl[fieldInfo.GetValue(null)];
if (d == null)
return false;
foreach (Delegate del in d.GetInvocationList())
{
string anonymous = string.Format("<{0}>", methodName);
//判断一下某个事件处理程序是否已经被绑定到 eventName 事件上
if (del.Method.Name == methodName || del.Method.Name.StartsWith(anonymous))
{
return true;
}
}
return false;
}
}
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Reflection
Imports System.Windows.Forms
Public Module ControlExtension
Private Const cos30 As Single = 0.866F
Private Const sin30 As Single = 0.5F
<System.Runtime.CompilerServices.Extension>
Public Sub BindWaterMark(ctrl As Control)
If ctrl Is Nothing OrElse ctrl.IsDisposed Then
Return
End If
' 绘制水印
If ctrl.HaveEventHandler("Paint", "BindWaterMark") Then
Return
End If
AddHandler ctrl.Paint, Sub(sender, e)
Dim paintCtrl As System.Windows.Forms.Control = TryCast(sender, System.Windows.Forms.Control)
Dim g = e.Graphics
g.SmoothingMode = SmoothingMode.AntiAlias
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
' 计算控件位置
Dim offsetX As Integer = 0
Dim offsetY As Integer = 0
While paintCtrl.Parent IsNot Nothing
offsetX += paintCtrl.Location.X
offsetY += paintCtrl.Location.Y
paintCtrl = paintCtrl.Parent
End While
' 平移画布到窗体左上角
g.TranslateTransform(0 - offsetX, 0 - offsetY + 32)
'逆时针旋转30度
g.RotateTransform(-30)
For x As Integer = 0 To e.ClipRectangle.Right + 64 + offsetX Step 128
For y As Integer = 0 To e.ClipRectangle.Bottom + 64 + offsetY Step 128
' 计算文字起点位置
Dim x1 As Single = cos30 * x - sin30 * y
Dim y1 As Single = sin30 * x + cos30 * y
'画上文字
g.DrawString("水印文字", New Font("微软雅黑", 16, FontStyle.Regular),
New SolidBrush(Color.FromArgb(50, 100, 100, 100)), x1, y1)
Next
Next
End Sub
' 子控件绑定绘制事件
For Each child As System.Windows.Forms.Control In ctrl.Controls
BindWaterMark(child)
Next
End Sub
<System.Runtime.CompilerServices.Extension>
Public Function HaveEventHandler(control As Control, eventName As String, methodName As String) As Boolean
'获取Control类定义的所有事件的信息
Dim pi As PropertyInfo = control.GetType().GetProperty("Events", BindingFlags.Instance Or BindingFlags.NonPublic)
'获取Control对象control的事件处理程序列表
Dim ehl As EventHandlerList = DirectCast(pi.GetValue(control, Nothing), EventHandlerList)
'获取Control类 eventName 事件的字段信息
Dim fieldInfo As FieldInfo = GetType(Control).GetField($"Event{eventName}", BindingFlags.Static Or BindingFlags.NonPublic)
'用获取的 eventName 事件的字段信息,去匹配 control 对象的事件处理程序列表,获取control对象 eventName 事件的委托对象
'事件使用委托定义的,C#中的委托时多播委托,可以绑定多个事件处理程序,当事件发生时,这些事件处理程序被依次执行
'因此Delegate对象,有一个GetInvocationList方法,用来获取这个委托已经绑定的所有事件处理程序
Dim d As [Delegate] = ehl(fieldInfo.GetValue(Nothing))
If d Is Nothing Then
Return False
End If
For Each del As [Delegate] In d.GetInvocationList()
Dim anonymous As String = $"<{methodName}>"
'判断一下某个事件处理程序是否已经被绑定到 eventName 事件上
If del.Method.Name = methodName OrElse del.Method.Name.StartsWith(anonymous) Then
Return True
End If
Next
Return False
End Function
End Module