1. 同一应用程序,不可同时运行多个。
Private Sub Form_Load()
If App.PrevInstance Then
SaveTitle$ = App.Title
App.Title = "... duplicate instance."
Form1.Caption = "... duplicate instance."
AppActivate SaveTitle$
SendKeys "% R", True
N1 = MsgBox("The program is running", 48, "")
End
End If
end sub
2.注册表的操作
1) RegCreateKey() : 在指定的项下创建一个新项。如指定的项已经存在,那么函数会打开现有的项
http://baike.baidu.com/view/1293829.htm
例:Dim nKeyHandle As Long, nValueType As Long, nLength As Long
Dim sValue As String, sValue1 As String
RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\MOXGatewayConfigTool", nKeyHandle)
2)RegQueryValueEx(): 获取一个项的设置值
http://baike.baidu.com/view/1293857.htm
例: RegQueryValueEx(nKeyHandle, "AutoStartAfterWindow", 0, nValueType, sValue1, nLength)
3)RegSetValueEx(): 设置指定项的值
http://baike.baidu.com/view/1297625.htm
例: RegSetValueEx(nKeyHandle, "AutoStartAfterWindow", 0, REG_SZ, sValue1, 255)
4)RegCloseKey(): 释放指定注册键的句柄
http://baike.baidu.com/view/1293825.htm
例:RegCloseKey(nKeyHandle)
3. 设置listBox控件同时具有水平和垂直滚动条
SendMessage List1.hWnd, &H194, 999, ByVal 0
4. 鼠标放在窗体控件上,按下鼠标左键,拖动窗体
Private Sub Label4_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
MoveScreen = True
MousX = X
MousY = Y
End If
End Sub
Private Sub Label4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If MoveScreen Then
CurrX = Form1.Left - MousX + X
CurrY = Form1.Top - MousY + Y
Form1.Move CurrX, CurrY
End If
End Sub
Private Sub Label4_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MoveScreen = False
End Sub
5. 最小化窗体到工具栏:
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const NIF_ICON = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDBLCLK = &H203
Public Type NOTIFYICONDATA
cbSize As Long
hWnd As Long
uId As Long
uFlags As Long
uCallBackMessage As Long
hIcon As Long
szTip As String * 64
End Type
Public nid As NOTIFYICONDATA
Private Sub Image1_Click()
nid.cbSize = Len(nid)
nid.uId = vbNull
nid.hWnd = Form1.hWnd
nid.uFlags = NIF_TIP Or NIF_MESSAGE Or NIF_ICON
nid.uCallBackMessage = WM_MOUSEMOVE
nid.hIcon = Form1.Icon
nid.szTip = "Gateway config Tool" & vbNullChar
Me.Hide
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim msg As Long
msg = X / 15
If msg = WM_LBUTTONDBLCLK Then
Me.Show
Shell_NotifyIcon NIM_DELETE, nid
End If
End Sub
6. 当窗体类型无边框时,在状态栏显示窗体图标:
在窗体的属性页中设置 ShowInTaskbar=True;
7. vb 读CSV文件
dim linNum as interger
dim str as string,strr as string
lineNum = 0
If Right(App.Path, 1) = "\" Then
Open App.Path + "datalist.csv" For Input As #1
Else
Open App.Path + "\datalist.csv" For Input As #1
End If
Do Until EOF(1) = True
Line Input #1, strr
lineNum = lineNum + 1
If lineNum >= 1 Then
str = Split(strr, ",")(2)
End If
Loop
Close
8. VB 写CSV文件(文件已存在)
Public Function ResetFile(strSourceFile As String)
On Error GoTo skip
Dim Filenum As Integer
Dim FileContents As String
Dim FileInfo() As String
Dim i As Integer
Dim J As Integer
Filenum = FreeFile
Open strSourceFile For Binary As #Filenum
FileContents = Space(LOF(Filenum))
Get #Filenum, , FileContents
Close Filenum
FileInfo = Split(FileContents, vbCrLf)
'取出源文件行數,按照回車換行來分隔成數組
Filenum = FreeFile
If Dir(strSourceFile, vbNormal) <> " " Then
Kill App.Path + "\datalist.csv"
End If
Dim FileStr() As String
'替換一行代碼塊
Open strSourceFile For Append As #Filenum
'循環每一行
For i = 0 To UBound(FileInfo) - 1
If (i = 2) Then
Print #Filenum, "OPC Server name"; ","; Trim(Form1.name1.Text)
ElseIf (i = 3) Then
Print #Filenum, "OPC server Address "; ","; Trim(Form1.node1.Text)
ElseIf (i = 4) Then
Print #Filenum, "OPC scan cycle(s) "; ","; Trim(Form1.OPCScanCycle.Text)
ElseIf (i = 5) Then
Print #Filenum, "OPC_Reconnect_Time(Min)"; ","; Trim(Form1.OPCRetryTime.Text)
ElseIf (i = 7) Then
If ModeFlag = True Then
Print #Filenum, "TCP/UDP communication mode"; ","; "TCP"
Else
Print #Filenum, "TCP/UDP communication mode"; ","; "UDP"
End If
ElseIf (i = 8) Then
Print #Filenum, "Port Number"; ","; Trim(Form1.TCPport.Text)
ElseIf (i = 9) Then
Select Case Form1.Combo1.ListIndex
Case 0
Print #Filenum, "ByteOrder"; ","; "0"
Case 1
Print #Filenum, "ByteOrder"; ","; "1"
Case 2
Print #Filenum, "ByteOrder"; ","; "2"
Case 3
Print #Filenum, "ByteOrder"; ","; "3"
End Select
Else
Print #Filenum, FileInfo(i)
End If
Next
Close #Filenum
Form1.ErrMsg.Text = "Save config Successfully!"
Exit Function
skip:
Form1.ErrMsg.Text = "Save config fail!"
End Function