一委托:此示例演示如何将方法与委托关联然后通过委托调用该方法。
专注于为中小企业提供成都网站建设、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业科尔沁右翼中免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
创建委托和匹配过程
创建一个名为 MySubDelegate 的委托。
Delegate Sub MySubDelegate(ByVal x As Integer)
声明一个类,该类包含与该委托具有相同签名的方法。
Class class1
Sub Sub1(ByVal x As Integer)
MsgBox("The value of x is: " CStr(x))
End Sub
End Class
定义一个方法,该方法创建该委托的实例并通过调用内置的 Invoke 方法调用与该委托关联的方法。
Protected Sub DelegateTest()
Dim c1 As New class1
' Create an instance of the delegate.
Dim msd As MySubDelegate = AddressOf c1.Sub1
' Call the method.
msd.Invoke(10)
End Sub
二、事件
下面的示例程序阐释如何在一个类中引发一个事件,然后在另一个类中处理该事件。AlarmClock 类定义公共事件 Alarm,并提供引发该事件的方法。AlarmEventArgs 类派生自 EventArgs,并定义 Alarm 事件特定的数据。WakeMeUp 类定义处理 Alarm 事件的 AlarmRang 方法。AlarmDriver 类一起使用类,将使用 WakeMeUp 的 AlarmRang 方法设置为处理 AlarmClock 的 Alarm 事件。
该示例程序使用事件和委托和引发事件中详细说明的概念。
示例
' EventSample.vb.
'
Option Explicit
Option Strict
Imports System
Imports System.ComponentModel
Imports Microsoft.VisualBasic
Namespace EventSample
' Class that contains the data for
' the alarm event. Derives from System.EventArgs.
'
Public Class AlarmEventArgs
Inherits EventArgs
Private _snoozePressed As Boolean
Private nrings As Integer
'Constructor.
'
Public Sub New(snoozePressed As Boolean, nrings As Integer)
Me._snoozePressed = snoozePressed
Me.nrings = nrings
End Sub
' The NumRings property returns the number of rings
' that the alarm clock has sounded when the alarm event
' is generated.
'
Public ReadOnly Property NumRings() As Integer
Get
Return nrings
End Get
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public ReadOnly Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
End Property
' The AlarmText property that contains the wake-up message.
'
Public ReadOnly Property AlarmText() As String
Get
If _snoozePressed Then
Return "Wake Up!!! Snooze time is over."
Else
Return "Wake Up!"
End If
End Get
End Property
End Class
' Delegate declaration.
'
Public Delegate Sub AlarmEventHandler(sender As Object, _
e As AlarmEventArgs)
' The Alarm class that raises the alarm event.
'
Public Class AlarmClock
Private _snoozePressed As Boolean = False
Private nrings As Integer = 0
Private stopFlag As Boolean = False
' The Stop property indicates whether the
' alarm should be turned off.
'
Public Property [Stop]() As Boolean
Get
Return stopFlag
End Get
Set
stopFlag = value
End Set
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
Set
_snoozePressed = value
End Set
End Property
' The event member that is of type AlarmEventHandler.
'
Public Event Alarm As AlarmEventHandler
' The protected OnAlarm method raises the event by invoking
' the delegates. The sender is always this, the current instance
' of the class.
'
Protected Overridable Sub OnAlarm(e As AlarmEventArgs)
RaiseEvent Alarm(Me, e)
End Sub
' This alarm clock does not have
' a user interface.
' To simulate the alarm mechanism it has a loop
' that raises the alarm event at every iteration
' with a time delay of 300 milliseconds,
' if snooze is not pressed. If snooze is pressed,
' the time delay is 1000 milliseconds.
'
Public Sub Start()
Do
nrings += 1
If stopFlag Then
Exit Do
Else
If _snoozePressed Then
System.Threading.Thread.Sleep(1000)
If (True) Then
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
Else
System.Threading.Thread.Sleep(300)
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
End If
Loop
End Sub
End Class
' The WakeMeUp class has a method AlarmRang that handles the
' alarm event.
'
Public Class WakeMeUp
Public Sub AlarmRang(sender As Object, e As AlarmEventArgs)
Console.WriteLine((e.AlarmText + ControlChars.Cr))
If Not e.SnoozePressed Then
If e.NumRings Mod 10 = 0 Then
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Press Snooze? Enter N")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
If input.Equals("N") Or input.Equals("n") Then
CType(sender, AlarmClock).SnoozePressed = True
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End If
Else
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End Sub
End Class
' The driver class that hooks up the event handling method of
' WakeMeUp to the alarm event of an Alarm object using a delegate.
' In a forms-based application, the driver class is the
' form.
'
Public Class AlarmDriver
Public Shared Sub Main()
' Instantiates the event receiver.
Dim w As New WakeMeUp()
' Instantiates the event source.
Dim clock As New AlarmClock()
' Wires the AlarmRang method to the Alarm event.
AddHandler clock.Alarm, AddressOf w.AlarmRang
clock.Start()
End Sub
End Class
End Namespace
这是我自己写的一个进度条控件,如图:
边框粗细和边框颜色以及进度条颜色都可以调。
进度条大小你可以自己拉伸。
同时,进度条还提供了丰富的动画效果(如边框闪烁效果【可关闭】,进度条滑动效果等。)
将边框粗细调为0即可实现无边框(同时边框闪烁效果(见“Flash”属性)也将关闭!)
进度值(见“ProgressValue”属性)类型为Single(单精度浮点数)
将我提供的DLL文件拖到工具箱即可安装
只能用于Windows窗体应用程序!
目标平台:.Net Framework 3.5
属性说明:
1.BorderColor 用于调整边框颜色
2.BorderWidth 用于调整边框粗细
3.Flash 用于开启、关闭边框闪烁效果
4.ProgressValue 用于设置、查看进度值
5.ForeColor 用于设置进度条颜色
6.BackColor 用于设置控件背景色
附件如果丢失,请私信。
1.不正确
Dim abc As myClass,定义一个可以引用myClass类型对象的变量abc。这个abc变量存在于栈上。
abc=New myClass。New myClass创建myClass实例对象,这个对象存在于托管堆上。然后(同时)让abc引用(指向)这个对象。
abc变量在栈上,而对象在托管堆上。
abc储存的是对象的地址,而不是对象本身。
看下面的代码
Dim a As myClass,b As myClass,c As myClass
a=New myClass
b=a
c=a
整个过程中只创建了一个myClass对象,位于托管堆上。
三个myClass类型的变量a、b、c位于栈上,都指向了这一个myClass对象
2.类的成员分 实例成员 和 静态成员
实例成员 必须在实例对象上调用
静态成员 可以直接调用,不需要实例对象。
花了二十分钟给你写了代码,已测试。建议学习并使用System.Drawing绘制。
主要是掌握Graphics.FillRectangle和DrawString的使用。
Imports System.Drawing
Public Class 进度条UI
Public 上面笔刷 As SolidBrush = New SolidBrush(Color.FromArgb(192, 175, 238, 238))
Public 下面笔刷 As SolidBrush = New SolidBrush(Color.FromArgb(192, 30, 144, 255))
Public 文字笔 As SolidBrush = New SolidBrush(Color.FromArgb(255, 255, 255, 255))
Public 字体 As Font = New Font("微软雅黑", 14.0)
Public 文字格式 As StringFormat = New StringFormat() With
{.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
''' summary
''' 绘制指定进度的图像。
''' 当进度变化时调用一次本方法,建议将创建的Graphics对象保存到变量而不要重复创建。。
''' /summary
''' param name="控件"绘制到此控件的工作区/param
''' param name="g"绘制到控件的Graphics对象,例如 Button1.CreateGraphics()/param
''' param name="进度"进度百分比实数,57% = 0.57/param
Public Sub 绘制(ByRef 控件 As Control, ByRef g As Graphics, ByVal 进度 As Double)
Dim 矩形 = 控件.ClientRectangle '获取控件的工作区矩形
Dim 下面高度 = CInt(矩形.Height * 进度) '获取下面颜色块的高度
Dim 中间位置 = 矩形.Top + 矩形.Height - 下面高度 '获取中间分界线的Y坐标
Dim 上矩形 = New Rectangle(矩形.X, 矩形.Y, 矩形.Width, 矩形.Height - 下面高度)
Dim 下矩形 = New Rectangle(矩形.X, 中间位置, 矩形.Width, 下面高度)
g.FillRectangle(上面笔刷, 上矩形)
g.FillRectangle(下面笔刷, 下矩形)
'绘制文字
Dim 文字 As String = String.Format("{0:0.00}%", 进度 * 100)
g.DrawString(文字, 字体, 文字笔, 矩形, 文字格式)
End Sub
End Class
下面是Form1窗体的代码:添加一个Button1和Timer1控件,将Button1尺寸拖大点
Public Class Form1
Public g As Graphics
Public 进度条UI As New 进度条UI
Public 进度 As Double
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
g = Button1.CreateGraphics()
Timer1.Enabled = Not Timer1.Enabled
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
进度 += 0.01
进度条UI.绘制(Button1, g, 进度)
End Sub
End Class
1 生成txt文件。
DimSaveFileDialog1AsNewSaveFileDialog() '创建一个保存对话框
SaveFileDialog1.Filter ="txt files (*.txt)|*.txt" '设置扩展名
IfSaveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OKThen '如果确定保存
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.Filename, Textbox1.Text,False) '保存文本,False表示不追加文本,直接覆盖其内容
EndIf
原文链接:
Imports System.Text
Imports System.Runtime.InteropServices
Public Class Form1
' 相关API函数声明,注释掉的这里没用到,但是也比较常用吧,这些函数的功能都能搜到。
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
Private Delegate Function EnumChildProc(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
Private Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal lpEnumFunc As EnumChildProc, ByVal lParam As Integer) As Boolean
Private Declare Auto Function SendMessage Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
'Private Declare Function CheckDlgButton Lib "user32" Alias "CheckDLGButtonA" (ByVal hDlg As IntPtr, ByVal nIDButton As IntPtr, ByVal wCheck As Integer) As Integer
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
'Private Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As IntPtr, ByVal lpdwProcessId As Long) As Integer
Private Declare Auto Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLength" (ByVal hwnd As IntPtr) As Integer
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer
' 相关消息定义,也有没用到的
Const WM_SETTEXT = HC
Const WM_GETTEXT = HD
'Const WM_SETFOCUS = H7
'Const WM_KILLFOCUS = H8
'Const WM_CLOSE = H10
'Const WM_SYSCOMMAND = H112
'Const SC_CLOSE = HF060
'Const SC_MINIMIZE = HF020
Const BM_GETCHECK = HF0
Const BM_SETCHECK = HF1
Const BM_GETSTATE = HF2
Const BM_SETSTATE = HF3
Const BM_SETSTYLE = HF4
Const BM_CLICK = HF5
'Const BM_GETIMAGE = HF6
'Const BM_SETIMAGE = HF7
Const BST_UNCHECKED = O0
Const BST_CHECKED = O1
Const BST_INDETERMINATE = O2
' 储存窗口句柄
Dim WindowHandle As IntPtr
' 储存两个(或者多个)编辑框句柄
Dim EditHandle As New List(Of IntPtr)
Dim EditWindowsText As List(Of String)
' 储存复选框句柄
Dim CheckHandle As IntPtr = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1_Click(sender, e)
End Sub
' EnumChildWindows 回调函数,该函数名作为API函数EnumChildWindows 的一个参数
' 该函数实现了枚举各个子窗口,找出编辑框属性的功能
Public Function EnumChildProcC(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
Dim dwWindowClass As StringBuilder = New StringBuilder(100)
' 获得某一个句柄的类名
GetClassName(hwnd, dwWindowClass, 100)
If dwWindowClass.ToString.Contains("EDIT") Or dwWindowClass.ToString.Contains("Edit") Then ' 类名包含EDIT的为编辑框
EditHandle.Add(hwnd) ' 存储该句柄
End If
' 返回 True 一直枚举完
Return True
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WindowHandle = FindWindow(vbNullString, "登陆")
If WindowHandle.ToInt32 = 0 Then
MsgBox("未捕获到窗口" + "登陆")
Return
End If
' 枚举所有主窗口的子窗口(控件),枚举时自动调用回调函数,完成编辑框句柄的获取
EnumChildWindows(WindowHandle, AddressOf EnumChildProcC, 0)
' 寻找复选框
CheckHandle = FindWindowEx(WindowHandle, IntPtr.Zero, vbNullString, "记住密码")
Dim str As New StringBuilder
Dim j As Integer = 0
' 对编辑框文本赋值
For j = 0 To EditHandle.Count - 1
SendMessage(EditHandle(j), WM_SETTEXT, 0, "Text")
'GetWindowText(EditHandle(j), str, 20)
'EditWindowsText.Add(Str.ToString)
'Str.Clear()
Next
If EditHandle.Count = 0 Then
MsgBox("未找到输入框!")
End If
If CheckHandle.ToInt32 0 Then
'CheckDlgButton(WindowHandle, id, 1)
' 对复选框进行鼠标单击操作
SendMessage(CheckHandle, BM_CLICK, 0, 0)
'SendMessage(CheckHandle, BM_SETCHECK, True, 0)
End If
End Sub
End Class