返回列表 回复 发帖

[转帖]VB技巧十则

【 原文由 mblike 所发表 】
1.有 一 个 简 单 的 方 法 从 完 整 路 径 名 中 摘 取 文 件 名
  1.     Function FName(filespec As String)As String
  2.       Dim i      As Integer
  3.       Dim size   As Integer
  4.       size=Len(filespec)
  5.       For i=size To 1 Step-1
  6.          if Mid$(filespec,i,1)Like"[\:]"Then
  7.              FName=Right(filespec,size-i)
  8.              Exit Function
  9.                                    
  10.          End If
  11.       Next i
  12.     End Function
  13.     例如:
  14.     File_Name=FNmae("A:\test.dat")
  15.     File_Name=FNmae("A:test.dat")
  16.     File_Name=FNmae("A:\test\test.dat")   
复制代码
2.控 制 Form_Load事 件 中 的 错 误
创 建 一 个 属 性 表 明 成 功 或 者 失 败 。 如 果 失 败 就 在 调 用 过 程 中
卸 载 该 Form。
  1.     Public SuceesfulLoad    As  Boolean
  2.     `创建属性Form1.SuccessfulLoad
  3.     Private Sub Form_Load()
  4.        SuccessfulLoad=True
  5.        If An Error Occurs Then
  6.                                     
  7.           SuccessfulLoad=False
  8.        End If
  9.     End Sub
  10.     调用过程:
  11.     Sub LoadTheForm()
  12.         Dim MyForm As Form1
  13.         Set MyForm=New Forml
  14.         Load MyForm
  15.         If MyForm.SuccessfulLoad then
  16.             MyForm.Show vbModal
  17.         End If               
  18.         Unload MyForm
  19.     Set MyForm=Nothing
  20.     End Sub         
复制代码
3.设 置 Form的 属 性 ControlBox为 True,Icon为 None,Borderstyle为 3,将 会 得
到 一 个 没 有 系 统 菜 单 但 有 关 闭 按 钮 的 对 话 框 。


4.在 结 束 程 序 之 前 设 置 所 有 的 object变 量 为 noth-ing,因 为 在 一 个
OLE客 户 端 应 用 程 序 End语 句 执 行 时 ,OLE服 务 器 的 关 闭 并 不 能 使  
所 有 的 object的 Terminate 事 件 被 调 用 。


5.使 用 特 定 的 object类 型 如 im object1 As CTask来 代 替 Dim object1 as
Object,这 样 会 极 大 地 提 高 性 能 。


6.VB4.0中 的 几 个 快 捷 键 与 VB3.0中 不 同 :  
    Ctrl-T Custom Controls.
    Ctrl-E Menu Editor.
    Ctrl-H Replace.   

7.当 传 递 Unicode字 符 串 给 函 数 或 从 函 数 中 接 受 Uni-code字 符 串 时
,应 使 用 VB4中 新 的 数 据 类 型 Byte。
转 换 字 符 串 变 量 到 Byte数 组 :
    Redim MyByteArray(0 to len(MyString$)-1)as Byte
    MyByteArray()=StrConv(MyString$,vbFromU-nicode)      
转 换 Byte数 组 到 字 符 串 变 量 :
    MyString$=StrConv(MyByteArray(),vbUnicode)   
由 于 VB4中 的 "Bug",VB4不 允 许 转 换 字 符 串 到 结 构 中 的 Byte数 组 , 例
如 下 面 程 序 将 得 不 到 正 确 结 果 。
    TYPE MyByteType
       Bytes(0 to 255)as Byte
    END TYPE
    Dim MBA as MyByteType
    MBA.Bytes()=StrConv(MyString$,vbFromU-nicode)
但 是 MyString4=StrConv(MBA.Bytes(),vbUnicode)是 正 确 的 。

8.Printer Object的 "Bug"
下 面 的 代 码 应 该 更 改 字 体 ,但 是 在 VB4中 不 起 作 用
    Printer.FontName="Arial"
    Printer.FontSize=11
    Printer.Print"This is a test."  
在 VB4中 每 开 始 一 个 新 页 必 须 初 始 化 ,下 面 的 代 码 将 会 正 确 工
作 。

9.Windows 95是 抢 先 式 多 任 务 操 作 系 统 ,所 以 函 数 DoEvents()不 是 必
须 的 ,仅 当 用 户 自 己 的 VB4应 用 程 序 需 要 时 进 行 调 用 。

10.使 用 以 下 函 数 可 获 得 当 前 用 户 名
  1.     Declare Function GetModuleHandle Lib"Kernel"
  2.        (ByVal lpModuleName As String)As Integer
  3.     Declare Function LoadString Lib "User"
  4.        (ByVal hInstance As Integer,ByVal wID As Integer,By Val lpBuffer As
  5. Any,
  6. ByVal nBufferMax As Interger)As Integer
  7.     Declare Function GetUserName Lib"advapi32.dll"Alias
  8.     "GetUserNameA"(ByVal lpBuffer As String,nSize As Long)As Long
  9.     Function MyGetUserName()As String
  10.         Dim hInst  As Integer
  11.         Dim User   As String
  12.         Dim Length  As Integer
  13.         User=Space(255)
  14.         #If Win16 Then
  15.              hInst=GetModuleHandle("User.exe")
  16.            Length=LoadString(hInst,515,User,Len(User))
  17.              User=Left$(User,Length)   
  18.              MyGetUserName=User
  19.         #Else    `Win95或WinNT
  20.             GetUserName User,Len(User)
  21.             MyGetUserName=User
  22.         #End If
  23.     End Function   
复制代码
哈哈哈!!!!你的IP是不是?我都知道了!!!
返回列表