qq51023578 2023-07-22 12:49 采纳率: 50%
浏览 6
已结题

窗体水印C#转VB.Net

麻烦各位帮忙把以下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;
    }
}

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-07-22 12:58
    关注
    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
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月30日
  • 已采纳回答 7月22日
  • 创建了问题 7月22日

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备