| 网站首页 | 文章中心 | 电子书下载 | 矢量图库 | 视频教程 | 素材下载 | 程序代码下载 | JS代码 | 论坛 | 
常用软件类:
|杀毒安全 |联络聊天 |网络软件 |多媒体类 |系统工具 |图形图像 |系统工具 |应用软件 |行业软件
开发设计类:
|动画制作 |图像处理 |3D设计 |操作系统 |站长学院 |网络相关 |WEB设计 |数据库类 |程序开发
Visual Basic编程常见问题
作者:佚名    文章来源:网络    点击数:    更新时间:2006-11-24
 


  如何用程序控制滑鼠游标 (Mouse Cursor) 到指定位置?

  以下这个例子,当 User 在 Text1 中按下 ’Enter’ 键后,滑鼠游标会自动移到 Command2 按钮上方

  请在声明区中加入以下声明:

’16 位版本: ( Sub 无传回值 )
Declare Sub SetCursorPos Lib "User" (ByVal X As Integer, ByVal Y As Integer)

’32 位版本: ( Function 有传回值,Integer 改成 Long )
Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

’在 Form1 中加入以下程序码:
Private Sub Text1_KeyPress(KeyAscii As Integer)
 If KeyAscii = 13 Then
  x% = (Form1.Left + Command2.Left + Command2.Width / 2 + 60) / Screen.TwipsPerPixelX
  y% = (Form1.Top + Command2.Top + Command2.Height / 2 + 360) / Screen.TwipsPerPixelY
  SetCursorPos x%, y%
 End If
End Sub

  如何用鼠标移动没有标题的 Form,或移动 Form 中的控制项?

  在声明区中放入以下声明:

’16 位版本: ( Sub 无返回值 )
Private Declare Sub ReleaseCapture Lib "User" ()
Private Declare Sub SendMessage Lib "User" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Long)

’32 位版本: ( Function 有返回值,Integer 改成 Long )
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

’共用常数:
Const WM_SYSCOMMAND = &H112
Const SC_MOVE = &HF012

’若要移动 Form,程序码如下:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
 Dim i As Long
 i = ReleaseCapture
 i = SendMessage(Form1.hwnd, WM_SYSCOMMAND, SC_MOVE, 0)
End Sub

’以上功能也适用于用鼠标在 Form 中移动控制项,程序码如下:
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
 Dim i As Long
 i = ReleaseCapture
 i = SendMessage(Command1.hwnd, WM_SYSCOMMAND, SC_MOVE, 0)
End Sub

  检查文件是否存在?

Function FileExists(filename As String) As Integer
 Dim i As Integer
 On Error Resume Next
 i = Len(Dir$(filename))
 If Err Or i = 0 Then FileExists = False Else FileExists = True
End Function
如何设置对VB数据库连接的动态路径

  我个人因为经常作一些数据库方面的程序,对于程序间如何与数据库进行接口的问题之烦是深有体会,因为VB在数据库链接的时候,一般是静态,即数据库存放的路径是固定的,如用VB的DATA,adodc,DataEnvironment 等到作数据库链接时,如果存放数据库的路径被改变的话,就会找不到路经,真是一个特别烦的事。

  笔者的解决方法是利用app.path 来解决这个问题。

  一、用data控件进行数据库链接,可以这样:

  在form_load()过程中放入:

private form_load()
Dim str As String ’定义
str = App.Path
If Right(str, 1) <> "\" Then
str = str + "\"
End If
data1.databasename=str & "\数据库名"
data1.recordsource="数据表名"
data1.refresh
sub end

  这几句话的意为,打开当前程序运行的目录下的数据库,你只要保证你的数据库在你程序所在的目录之下就行了。

  二、利用adodc(ADO Data Control)进行数据库链接:

private form_load ()
Dim str As String ’定义
str = App.Path
If Right(str, 1) <> "\" Then
str = str + "\"
End If
str = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & str & "\tsl.mdb"
Adodc1.ConnectionString = str
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "select * from table3"
Adodc1.Refresh
end sub

  三、利用DataEnvironment进行数据库链接

  可在过程中放入:

On Error Resume Next
If DataEnvironment1.rsCommand1.State <> adStateClosed Then
 DataEnvironment1.rsCommand1.Close ’如果打开,则关闭
End If
’i = InputBox("请输入友人编号:", "输入")
’If i = "" Then Exit Sub
DataEnvironment1.Connection1.Open App.Path & "\userdatabase\tsl.mdb"
DataEnvironment1.rsCommand1.Open "select * from table3 where 编号=’" & i & "’"
’Set DataReport2.DataSource = DataEnvironment1
’DataReport2.DataMember = "command1"
’DataReport2.show
end sub

  四、利用ADO(ActiveX Data Objects)进行编程:

  建立连接:

dim conn as new adodb.connection
dim rs as new adodb.recordset
dim str
str = App.Path
If Right(str, 1) <> "\" Then
str = str + "\"
End If
str = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & str & "\tsl.mdb"
conn.open str
rs.cursorlocation=aduseclient
rs.open "数据表名",conn,adopenkeyset.adlockpessimistic
用完之后关闭数据库:
conn.close
set conn=nothing

 

上一页  [1] [2] [3] [4] [5] [6] [7] 下一页


  • 上一篇文章:

  • 下一篇文章: 没有了
  • 相关文章