VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - Vb6.0编程技巧四则
发表评论(0)作者:廖海波, 平台:VB6.0+Win98, 阅读:9661, 日期:2001-04-03
Vb6.0编程技巧四则
上海市铁道大学
廖海波
---- 一、如何让用户自行输入方程式,并计算其结果?

---- 假设我们要让使用者在“方程式”栏位中自由输入方程式,然后利用方程式进行计算,则引用ScriptControl控件可以很方便地做到。 ( ScriptControl 控件附属于VB 6.0,如果安装后没有看到此一控件,可在光盘的 \Common\Tools\VB\Script 目录底下找此一控件, 其.文件名为Msscript.ocx。) 假设放在窗体上的ScriptControl控件名称为ScriptControl1,则在“计算”按钮的Click事件中编写如下代码:

  Dim Statement As String
  Statement = "X=" + Text1.Text + vbCrLf + _
            "Y=" + Text2.Text + vbCrLf + _
            "MsgBox ""计算结果="" & Y "
  ScriptControl1.ExecuteStatement( Statement )

---- 二、如何让Win95重新开机?

---- API 声明代码:

Private Declare Function ExitWindowsEx Lib
"user32" (ByVal uFlags As Long,
                        ByVal dwReserved As Long) As Long
    Private Enum HowExitConst
          EWX_LOGOFF = 0    注释:登出
          EWX_REBOOT = 2    注释:重新开机
          EWX_SHUTDOWN = 1  注释:关机
          EWX_FORCE = 4     注释:强制关机  
    End Enum
函数调用:
Call ExitWindowsEx( how,  0 )
     其中,how等於 EWX_LOGOFF 、 EWX_REBOOT 、
    EWX_SHUTDOWN 、 或EWX_FORCE

---- 三、如何将图片并排并且占满整个窗体?

---- 假设 Form 的 Picture 属性已经设定成某一张图片, 接下來如何把它并排, 并且占滿整个 Form 呢?

---- 在 Form_Paint 事件程序中编写以下程式即可:

   Private Sub Form_Paint()
      For I = 0 To ScaleWidth \ Picture.Width
          For J = 0 To ScaleHeight \ Picture.Height
             PaintPicture Picture, I * Picture.Width,
             J * Picture.Height
          Next
      Next
   End Sub

---- 四、快速读取 TextBox 第 N 行的数据

---- TextBox 是以 vbCr+vbLf 为分行符号, 如果我们要逐一读取 TextBox 每一行, 无非是寻找 vbCr+vbLf 的所在位置, 然后取出每一行的字串, 不过这个方法不快,而且如果我们要读取第 N 行数据,还是要从第 1、2、…N-1 行逐一读起,实在麻烦。 还好 Windows API 提供有读取 TextBox 第 N 行的功能, 细节如下:

---- 1、API 的声明:

   Const EM_GETLINE = &HC4
   Const EM_LINELENGTH = &HC1
   Const EM_LINEINDEX = &HBB
   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
Private Declare Sub RtlMoveMemory Lib "KERNEL32"
(lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)

---- (注:如果以上的声明放在“General”底下, 应在 Const 之前加上 Public 保留字, 並且將 Private 保留字去掉。)

---- 2. 程序范例:

Sub TB_GetLine(ByVal hWnd As Long, ByVal
whichLine As Long, Line As String)
Dim length As Long, bArr() As Byte,
bArr2() As Byte, lc As Long
lc = SendMessage(hWnd, EM_LINEINDEX,
whichLine, ByVal 0&)
    length = SendMessage(hWnd, EM_LINELENGTH,
    lc, ByVal 0&)
    If length > 0 Then
        ReDim bArr(length + 1) As Byte,
        bArr2(length - 1) As Byte
        Call RtlMoveMemory(bArr(0), length, 2)
        Call SendMessage(hWnd, EM_GETLINE,
        whichLine, bArr(0))
        Call RtlMoveMemory(bArr2(0), bArr(0), length)
        Line = StrConv(bArr2, vbUnicode)
    Else
        Line = ""
    End If
End Sub

注释: 假设要读取 Text1 第 5 行的数据
Dim S As String
Call TB_GetLine( Text1.hWnd, 5, S )
注释: 传回值 S 即等于第 5 行的数据
  ( 注:TextBox 的行次是从 0 起算。)