顯示具有 Visual Basic 標籤的文章。 顯示所有文章
顯示具有 Visual Basic 標籤的文章。 顯示所有文章

2008-09-16

SQLite + Visual Studio 2005

SQLite 是一套 Embedded RDBMS,很適合加進 App 裡頭跟著跑,並且資料庫檔能輕鬆跨平台。關於其它 SQLite 的優點,可以 Google 大神或 Wikipedia 取得。或許效能與支援度未能與其它 DBMS 相比,但你總不會想為了暫存 Spore 裡的建築方式,去安裝這些巨獸吧 XD


參考這篇討論串的作法,我下載了 SQLite for ADO.Net 2.0,目前使用正常 XD

底下簡述安裝及使用方式,作個記錄備查:
  1. 下載並解壓縮前面的 SQLite for ADO.Net 2.0 檔案
  2. 安裝 bin\Designer\install.exe,以提供設計階段的 VS.Net IDE支援
  3. 在方案中,加入對 bin\System.Data.SQLite.DLL 的參考
  4. 可以用了 !
下面是簡單的使用範例,語言為 Visual Basic.Net


Imports System.Data.SQLite

' ADO.Net 標準 Reader 三物件
Dim slconn As SQLiteConnection
Dim slcmd As SQLiteCommand
Dim slrdr As SQLiteDataReader

'建立 connection,給定檔名即可。注意讀寫權限 !
slconn = New SQLiteConnection("Data Source=c:\backup\Test.db3")
slconn.Open()
slcmd = slconn.CreateCommand() '建立 command 物件

'取得目前該檔案已有之 Table 名稱
Dim found as Boolean = False
slcmd.CommandText = "SELECT name FROM sqlite_master WHERE type='table';"
slrdr = slcmd.ExecuteReader() '建立 reader 物件
If slrdr.HasRows Then
While slrdr.Read()
MsgBox(If slrdr.GetString(0))
If slrdr.GetString(0).Equals("test") then found = True

End While
slrdr.Close() 'Reader 要關閉才能再利用

End If

If Not found Then
'建立 Table
slcmd.CommandText = "CREATE TABLE test (name);"
slcmd.ExecuteNonQuery()

End If

'INSERT, UPDATE, DELETE 用法均類似
slcmd.CommandText = "INSERT INTO test(name) VALUES ('clifflu');"
slcmd.ExecuteNonQuery()

'SELECT 需要用 cmd.ExecuteReader 抓結果
slcmd.CommandText = "SELECT name FROM test ;"
slrdr = slcmd.ExecuteReader()
If slrdr.HasRows() Then
While slrdr.Read()
Msgbox(slrdr.GetString(0))
End While
slrdr.Close()

End If

slconn.Close() 'Connection 用完要關

2008-08-04

以 VB.Net (Visual Studio 2005) 模擬滑鼠動作

玩一些網路 Flash Game 的時候,常常需要狂暴的點擊滑鼠,而所謂的 "滑鼠連點程式" 就成了第一選擇。網路上可以搜尋到不同作者篆寫的版本,像是 windsheep 寫的 windclick,或是..... 忘了,不重要 XD 不過之前有次想抓的時候,發現載點好像掛了,就只好自己寫一個囉。

要送出滑鼠事件,例如點擊 (Click)、壓下按鍵 (Mouse Down)、放開按鍵 (Mouse Up)、或是移動滑鼠 (Mouse Move),必需透過視窗訊息 (Windows Message) 運作,然而 .Net Framework 並沒有提供這樣的功能,因此只能透過 API。網路上可以找到一些關於所需 API 的說明,而這邊有人寫出基本的模組供使用,而這邊則有包好的 Class 源碼。

但考量到平台 (32 or 64 bit system) 和版本的關係,那個模組需要做一點修改。VS 2005 + vista x64 底下的 Long 是 8 byte,而 Integer 是 4 byte,但並非所有作業系統及 Compiler 下均是如此;相對的,user32.dll 所提供的 API 如 SetCursorPos 與 GetCursorPos 則明顯不使用 8 byte 的變數型態,因此將相關的變數型態指定為 int32 會是比較安全的做法。然後 TYPE 在 VS 2005 中已經被拿掉了,只要改成 Structure 就好。

修改過的模組檔如下:
Option Explicit On

Module MManip
'API 定義
Public Declare Sub Mouse_Event Lib "user32" _
(ByVal dwFlags As int32, ByVal dx As int32, ByVal dy As int32, _
ByVal cButtons As int32, ByVal dwExtraInfo As int32)

Public Declare Function SetCursorPos Lib "user32" _
(ByVal X As int32, ByVal Y As int32) As int32
Public Declare Function GetCursorPos Lib "user32"
(ByRef lpPoint As POINTAPI) As int32

Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20
Public Const MOUSEEVENTF_MIDDLEUP = &H40
Public Const MOUSEEVENTF_RIGHTDOWN = &H8
Public Const MOUSEEVENTF_RIGHTUP = &H10
Public Const MOUSEEVENTF_MOVE = &H1

Public Structure POINTAPI
Dim X As Int32
Dim Y As Int32

End Structure

'取得目前滑鼠座標
Public Function GetCurrentPos As POINTAPI
Dim Position As POINTAPI
GetCursorPos(Position)
GetCurrentPos = Position

End Function

'取得目前滑鼠座標 x 值
Public Function GetCurrentX() As int32
Dim Position As POINTAPI
GetCursorPos(Position)
GetCurrentX = Position.X

End Function

'取得目前滑鼠座標 y 值
Public Function GetCurrentY() As int32
Dim Position As POINTAPI
GetCursorPos(Position)
GetCurrentY = Position.Y

End Function

'滑鼠左擊
Public Sub LeftClick()
LeftDown()
LeftUp()

End Sub

'按下滑鼠左鍵
Public Sub LeftDown()
Mouse_Event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
End Sub

'放開滑鼠左鍵
Public Sub LeftUp()
Mouse_Event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub

'滑鼠中擊
Public Sub MiddleClick()
MiddleDown()
MiddleUp()

End Sub

'按下滑鼠中鍵
Public Sub MiddleDown()
Mouse_Event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
End Sub

'放開滑鼠中鍵
Public Sub MiddleUp()
Mouse_Event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
End Sub

'移動滑鼠
Public Sub MoveMouse(ByVal xMove As int32, ByVal yMove As int32)
Mouse_Event(MOUSEEVENTF_MOVE, xMove, yMove, 0, 0)
End Sub

'滑鼠右擊
Public Sub RightClick()
RightDown()
RightUp()

End Sub

'按下滑鼠右鍵
Public Sub RightDown()
Mouse_Event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
End Sub

'放開滑鼠右鍵
Public Sub RightUp()
Mouse_Event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
End Sub

End Module