1 生成txt文件。
創(chuàng)新互聯(lián)建站服務(wù)緊隨時(shí)代發(fā)展步伐,進(jìn)行技術(shù)革新和技術(shù)進(jìn)步,經(jīng)過十年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設(shè)計(jì)師、專業(yè)的網(wǎng)站實(shí)施團(tuán)隊(duì)以及高素質(zhì)售后服務(wù)人員,并且完全形成了一套成熟的業(yè)務(wù)流程,能夠完全依照客戶要求對(duì)網(wǎng)站進(jìn)行網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)、建設(shè)、維護(hù)、更新和改版,實(shí)現(xiàn)客戶網(wǎng)站對(duì)外宣傳展示的首要目的,并為客戶企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。
DimSaveFileDialog1AsNewSaveFileDialog()?'創(chuàng)建一個(gè)保存對(duì)話框
SaveFileDialog1.Filter?="txt?files?(*.txt)|*.txt"?'設(shè)置擴(kuò)展名
IfSaveFileDialog1.ShowDialog()?=?System.Windows.Forms.DialogResult.OKThen?'如果確定保存
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.Filename,?Textbox1.Text,False)?'保存文本,False表示不追加文本,直接覆蓋其內(nèi)容
EndIf
原文鏈接:
Imports?System.Text
Imports?System.Runtime.InteropServices
Public?Class?Form1
'?相關(guān)API函數(shù)聲明,注釋掉的這里沒用到,但是也比較常用吧,這些函數(shù)的功能都能搜到。
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
'?相關(guān)消息定義,也有沒用到的
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
'?儲(chǔ)存窗口句柄
Dim?WindowHandle?As?IntPtr
'?儲(chǔ)存兩個(gè)(或者多個(gè))編輯框句柄
Dim?EditHandle?As?New?List(Of?IntPtr)
Dim?EditWindowsText?As?List(Of?String)
'?儲(chǔ)存復(fù)選框句柄
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?回調(diào)函數(shù),該函數(shù)名作為API函數(shù)EnumChildWindows?的一個(gè)參數(shù)
'?該函數(shù)實(shí)現(xiàn)了枚舉各個(gè)子窗口,找出編輯框?qū)傩缘墓δ?/p>
Public?Function?EnumChildProcC(ByVal?hwnd?As?IntPtr,?ByVal?lParam?As?Integer)?As?Boolean
Dim?dwWindowClass?As?StringBuilder?=?New?StringBuilder(100)
'?獲得某一個(gè)句柄的類名
GetClassName(hwnd,?dwWindowClass,?100)
If?dwWindowClass.ToString.Contains("EDIT")?Or?dwWindowClass.ToString.Contains("Edit")?Then?????'?類名包含EDIT的為編輯框
EditHandle.Add(hwnd)????????????????????????'?存儲(chǔ)該句柄
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
'?枚舉所有主窗口的子窗口(控件),枚舉時(shí)自動(dòng)調(diào)用回調(diào)函數(shù),完成編輯框句柄的獲取
EnumChildWindows(WindowHandle,?AddressOf?EnumChildProcC,?0)
'?尋找復(fù)選框
CheckHandle?=?FindWindowEx(WindowHandle,?IntPtr.Zero,?vbNullString,?"記住密碼")
Dim?str?As?New?StringBuilder
Dim?j?As?Integer?=?0
'?對(duì)編輯框文本賦值
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)
'?對(duì)復(fù)選框進(jìn)行鼠標(biāo)單擊操作
SendMessage(CheckHandle,?BM_CLICK,?0,?0)
'SendMessage(CheckHandle,?BM_SETCHECK,?True,?0)
End?If
End?Sub
End?Class
用Timer控件吧,設(shè)置1秒執(zhí)行1次。然后直接在Timer中寫代碼,將全全局的值來
減減1,如果值為0,則停止Timer
跳出提示框
以下示例:
int
Tatal
=
100;
Timer.Inveral
=
1000;
Timer_()
{
if
(
Tatal
==
0)
{
MessageBox.Show("完成!");
Timer.Stop();
}
else
{
Tatal
--;
}
}
例1:以下程序我們申請(qǐng)幾個(gè)指向不同類型的指針:
’使用StructLayout(LayoutKind.Sequential)屬性告訴net編譯器:結(jié)構(gòu)的元素在內(nèi)存中按其出現(xiàn)的順序排列
StructLayout(LayoutKind.Sequential) _
Public Structure DEFUDT_Test
Public bytb As Byte
Public i32a As Int32
End Structure
Public Function fnGetIntptr1() As IntPtr
’取得一個(gè)4字節(jié)數(shù)組指針
Dim tabytTest(3) As Byte
’以下語(yǔ)句告訴net垃圾回收進(jìn)程不對(duì)tabytTest進(jìn)行處理,也就是說tabytTest占用的內(nèi)存區(qū)域固定不變。
Dim thObject As GCHandle = GCHandle.Alloc(tabytTest, GCHandleType.Pinned)
Dim tpObject As IntPtr = thObject.AddrOfPinnedObject() ’取得指向字節(jié)數(shù)組的指針
’取得一個(gè)指向32位內(nèi)存數(shù)據(jù)的指針,
’由于使用gchandle取指針的方法只能對(duì)引用的對(duì)象有效,
’所以對(duì)如int32等值類型必須使用將其封裝成為一個(gè)對(duì)象的方法以變?yōu)橐妙愋?/p>
Dim ti32Test As Object = Convert.ToInt32(0)
’以下語(yǔ)句告訴net垃圾回收進(jìn)程不對(duì)ti32test進(jìn)行處理,也就是說ti32Test的內(nèi)存位置固定不變。
Dim thObject1 As GCHandle = GCHandle.Alloc(ti32Test, GCHandleType.Pinned)
Dim tpObject1 As IntPtr = thObject1.AddrOfPinnedObject() ’取得ti32Test的首地址
Dim tudtTest1 As DEFUDT_Test
’由于結(jié)構(gòu)是一種值類型變量,為保證指針申請(qǐng)方便,我們申請(qǐng)
’取得一個(gè)和結(jié)構(gòu)tudtTest1大小一致的字節(jié)數(shù)組指針,只要空間占用長(zhǎng)度和結(jié)構(gòu)一樣就可以了
’由于net在結(jié)構(gòu)封裝中會(huì)插入額外的數(shù)據(jù)位,所以一定要用sizeof方法得到結(jié)構(gòu)在非托管使用時(shí)的實(shí)際大小
Dim tudtTest(Marshal.SizeOf(tudtTest1)) As Byte
Dim thObject2 As GCHandle = GCHandle.Alloc(tudtTest, GCHandleType.Pinned)
Dim tpObject2 As IntPtr = thObject2.AddrOfPinnedObject() ’取得指向結(jié)構(gòu)的指針
’在這兒你可以寫對(duì)指針處理的任意代碼(在例2中會(huì)給予補(bǔ)充)……
’在使用完畢后一定要釋放指針指向的內(nèi)存塊,讓垃圾回收器可對(duì)這個(gè)內(nèi)存塊回收處理
If thObject.IsAllocated Then
thObject.Free()
End If
If thObject1.IsAllocated Then
thObject1.Free()
End If
If thObject2.IsAllocated Then
thObject2.Free()
End If
End Function
上例中指針流程處理可以歸納為:
1、 定義一個(gè)具有合適內(nèi)存長(zhǎng)度的引用變量(關(guān)于引用變量和值變量的差異可以參觀VB.NET的書籍)
2、使用GCHandle.Alloc方法將變量的內(nèi)存區(qū)域固定下來。
3、使用GCHandle對(duì)象的AddrOfPinnedObject取得該內(nèi)存區(qū)域的首地址并賦值給指針變量.
4、對(duì)指針進(jìn)行操作
5、使用GCHandle對(duì)象的free方法釋放指針指向的內(nèi)存區(qū)域以便net垃圾回收器可以回收這個(gè)內(nèi)存空間
2、指針?biāo)赶驍?shù)據(jù)的存取
在.net中,對(duì)指針指向數(shù)據(jù)的存儲(chǔ)函數(shù)都封裝在marshal類中,主要的函數(shù)包括:Copy、PtrToStringUni 、PtrToStructure 、OffsetOf、WriteXXX,RreadXXX等,其中WriteXXX的表示向指針?biāo)硎镜牡刂分袑懭隭XX類型的數(shù)據(jù),而ReadXXX中作用就是將指針?biāo)诘刂返臄?shù)據(jù)以XXX類型方式讀出??蠢?,我們使用這些方法演示對(duì)例1那幾個(gè)指向不同類型數(shù)據(jù)的指針作數(shù)據(jù)存/取操作。
例2:演示向例1申請(qǐng)得到的幾個(gè)指針執(zhí)行寫入及讀取數(shù)據(jù)的操作.
Marshal.WriteInt32(tpObject1, 0, Convert.ToInt32(77)) ’向ti32Test變量指向的地址寫入32位整數(shù)77
MsgBox("現(xiàn)在ti32Test的值為:" ti32Test) ’因?yàn)樽兞看鎯?chǔ)地址的數(shù)據(jù)已改為77,所以顯示為77
’以下這句之所以可行,因?yàn)閠i32Test是32位整數(shù),而tpObject指向的tabytTest數(shù)組剛好有4個(gè)元素
’而每一個(gè)byte元素都占用8位,合起來就是32位,和ti32Test占用的空間一樣。這就印證了前面提’
’到的net中指針沒有指向類型的說明。
Marshal.WriteInt32(tpObject, 0, ti32Test)
’以下代碼再將tabytTest字節(jié)數(shù)組的內(nèi)容理解為一個(gè)int32整數(shù),
’并將值賦值給tudtTest結(jié)構(gòu)中的int32元素
’我們使用Marshal.OffsetOf(GetType(DEFUDT_Test), "i32a").ToInt32以取得i32a元素在結(jié)構(gòu)中的內(nèi)存偏移位置
’所以New IntPtr(tpObject2.ToInt32 + Marshal.OffsetOf(GetType(DEFUDT_Test), "i32a").ToInt32)就臨時(shí)產(chǎn)生了
’一個(gè)指針并指向i32a所在的內(nèi)存地址(, 這個(gè)方法也說明了指針可以以字節(jié)為單位進(jìn)行加減計(jì)算以指向合適的變量。
’Marshal.ReadInt32的作用是從指針中讀取一個(gè)32整數(shù)。
Marshal.WriteInt32(New IntPtr(tpObject2.ToInt32 + Marshal.OffsetOf(GetType(DEFUDT_Test), "i32a").ToInt32), _
0, Marshal.ReadInt32(tpObject))
’這兒可以將字節(jié)數(shù)組的內(nèi)容復(fù)制到真正的結(jié)構(gòu)中
MsgBox(Marshal.OffsetOf(tudtTest1.GetType, "i32a").ToInt32)
tudtTest1 = CType(Marshal.PtrToStructure(tpObject2, GetType(DEFUDT_Test)), DEFUDT_Test)
MsgBox("結(jié)構(gòu)tidtTest1中i32a元素的值為:" tudtTest1.i32a) ’此處將顯示剛賦的值77
一委托:此示例演示如何將方法與委托關(guān)聯(lián)然后通過委托調(diào)用該方法。
創(chuàng)建委托和匹配過程
創(chuàng)建一個(gè)名為 MySubDelegate 的委托。
Delegate Sub MySubDelegate(ByVal x As Integer)
聲明一個(gè)類,該類包含與該委托具有相同簽名的方法。
Class class1
Sub Sub1(ByVal x As Integer)
MsgBox("The value of x is: " CStr(x))
End Sub
End Class
定義一個(gè)方法,該方法創(chuàng)建該委托的實(shí)例并通過調(diào)用內(nèi)置的 Invoke 方法調(diào)用與該委托關(guān)聯(lián)的方法。
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
二、事件
下面的示例程序闡釋如何在一個(gè)類中引發(fā)一個(gè)事件,然后在另一個(gè)類中處理該事件。AlarmClock 類定義公共事件 Alarm,并提供引發(fā)該事件的方法。AlarmEventArgs 類派生自 EventArgs,并定義 Alarm 事件特定的數(shù)據(jù)。WakeMeUp 類定義處理 Alarm 事件的 AlarmRang 方法。AlarmDriver 類一起使用類,將使用 WakeMeUp 的 AlarmRang 方法設(shè)置為處理 AlarmClock 的 Alarm 事件。
該示例程序使用事件和委托和引發(fā)事件中詳細(xì)說明的概念。
示例
' 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
DLL(動(dòng)態(tài)鏈接庫(kù))是一個(gè)很有用的東西,在開發(fā)大項(xiàng)目的時(shí)候顯得非常重要,因?yàn)槎嗳撕献鏖_發(fā)時(shí),可以給每個(gè)人分配一個(gè)任務(wù),用DLL完成,最后組合起來,就不會(huì)出現(xiàn)互相沖突的問題。這里給出最簡(jiǎn)單的DLL編寫與調(diào)用的示例
首先,我們打開VB.NET,選擇類庫(kù),名稱改為test
然后輸入以下代碼
Public?Class?test
Public?Function?test(ByVal?a?As?Long,?ByVal?b?As?Long)?As?Long
Return?a?+?b
End?Function
End?Class
保存后,生成DLL文件。
這就是最簡(jiǎn)單的一個(gè)DLL,下面是調(diào)用該DLL的示例
新建一個(gè)工程,單擊“項(xiàng)目” -- 添加引用
找到剛才生成的DLL,雙擊它
添加引用以后,似乎什么也沒發(fā)生,這時(shí)我們輸入以下代碼:
Imports test.test
這樣,就包含了該DLL的類。
然后我們定義一個(gè)類
Dim test As New test.test
這樣,就可以使用里面的函數(shù)了,下面是程序示例
Imports?test.test
Public?Class?Form1
Private?Sub?Form1_Load(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?MyBase.Load
Dim?test?As?New?test.test
MsgBox(test.test(1,?2))
End?Sub
End?Class
運(yùn)行該程序,可以看到,調(diào)用了DLL內(nèi)的函數(shù)。
這就是最簡(jiǎn)單的DLL示例,可以將一些復(fù)雜的代碼集成到DLL里,以后升級(jí)或重用都比較方便。