Flexible use of VB App object

In practice programming in Visual Basic, App is very important to the global object. The rational use of the object development program can greatly save development time and to achieve its objectives by other means difficult to achieve. Here are some of the skills and experience of authors obtained when developing applications with Visual Basic.

Tools / materials

  • Visual Basic 6.0

Step / Method

  1. Avoid repeated instances of the same application running
    PrevInstance App object 
    property returns a logical value indicating whether there is a previous application instance running. Sometimes we just want to develop programs each run one instance, when a user repeatedly run the program, to remind the user that the program
    is already running, and automatically exit. To achieve the above function, simply add a module in the application, the module is added to start the process in Sub Main, Sub Main the following code is added to 
    the can.
    App.PrevInstance the Then IF
    Msg = "The system is running at below the taskbar icon seeks to minimize the system."
    Style = vbOKOnly + vbExclamation + vbDefaultButton2
    the Title = "Warning!"
    The Response = MsgBox (Msg, Style, the Title)
    Exit Sub 'the end of this operation
    end If
  2. Hide applications in the system task list
    App object T ask V isible property returns or sets a value (boolean 
    value boolean expression), to determine whether the application appears in the system task list. The default value is True, it indicates that the application appears in the Task List window; if set to False, then the application does not
    appear in the Task List window. This property allows the use of the title of the application does not appear Alt + Ctrl + Del Task List window that pops up, in order to avoid the application is user forcibly terminated.
    Note: The application starts and displays its interface later, TaskVisible 
    property is automatically set to the default value True. In addition, you can change the name of the application appear in the Task List window by setting the Title property, or project name will be displayed. For example: the
    establishment of a Command1 CommandButton control on FORM, add the following code:
    Private Sub Form_Load ()
    App.Title = "This is a sample application demonstrates how to hide"
    End Sub
    Private Sub Command1_Click ()
    the If the Then App.TaskVisible
    = False App.TaskVisible
    Else
    App.TaskVisible = True
    End the If
    End Sub
    Run the program, and Alt + Ctrl + Del Task List window pop-up observation, and then continue to click Command1, to see what changes.
  3. 实现与具体路径无关的应用程序
    开发程序时往往希望程序无论COPY 到哪个目录下都能正常运行,也就是程序与具体的应用程序所在的路径无关。你可以这样做:在应用程序中添加一个模块,在该模块中加入启动过程Sub Main,将如下代码加到模块中。
    Public mypass As String '声明为全局变量
    Sub Main()
    mypath = App.Path
    .. '其余程序部分
    ..
    End Sub
    在程序中凡是涉及到路径的地方,用mypath 代替绝对路径。例如:假设需要通过Data 控件Data1 
    来操作应用程序目录下的localdatabase 子目录下的user.mdb,user.mdb 包含有test 
    表。将如下代码加入该控件所在的FORM的LOAD事件中,就可实现对user.mdb 的操作与绝对路径无关。
    Data1.DatabaseName = mypath + "\localdatabase\user.mdb"
    Data1.RecordSource = "select * from test"
    Data1.Refresh
  4. 应用程序的版本管理
    与应用程序版本有关的属性有:Revision 属性、Major属性、Minor 属性。Revision 
    属性返回工程的修订版本号,该属性在运行时是只读的;Major 属性返回工程的主版本号,该属性在运行时是只读的;Minor 
    属性返回工程的小版本号,该属性在运行时是只读的。要设置工程的有关版本,可以在设计时用位于“工程属性”对话框中的“生成”选项卡上相对应的框设置这些
    属性。
    “修订版本号”反应的是对工程的修改信息,如果将“工程属性”对话框中的“生成”选项设置为“自动加”,则每当工程编译一次(编译为.EXE 
    文件),Revision 属性的值自动累加1。而Major 属性、Minor 
    属性才是我们通常提到的应用软件的版本。例如要设置软件的版本号为3.4,只需在工程设计时,将Major 属性设置为3,Minor 
    属性设置为4。软件运行时如果要显示版本号,可参考如下语句:MsgBox "系统版本为:"+Str(App.Major) + "." + 
    Trim(Str(App.Minor))运行该语句,将弹出一消息对话框显示“系统版本为:3.4”。
  5. 应用程序的日志操作
    App对象支持应用程序日志的操作,与此相关的有LogMode 属性、Logpath属性、LogEvent方法和StartLogging方法。下面说说这些属性和方法的功能及日志操作的步骤。
    1.首先用StartLogging方法对某个操作的日志目标及日志模式进行设置。
    语法格式:App.StartLogging logTarget, logModelogTarget 
    是一个字符串,用于指定一个用于记录日志的文件名。logMode 
    是一个整数值,它决定如何记入日志,一般将其置为vbLogAuto。另外,LogPath 属性、LogMode 
    属性在设计阶段不可用,且在运行阶段为只读,因此不能直接对这两个属性进行赋值,只能通过StartLogging 方法改变。
    2 . 用L o g E v e n t 方法把某个事件记入日志。在WindowsNT 平台上,该方法会把内容写到NT 的Event 
    日志中。在Windows9X平台上,该方法会把内容写到LogPath属性指定的文件中。按照缺省规定,如果不指定文件,事件将被写入
    vbevents.log 文件中。
    语法格式:App.LogEvent logBuffer, eventType
    其中:logBuffer 是要写入到日志中的信息;eventType是可选的,它指定了事件的类型。下面是eventType 的设置值:
    常量名常数值描述
    VbLogEventTypeError 1 错误
    VbLogEventTypeWarning 2 警告
    VbLogEventTypeInformation 4 信息
    3 . 程序运行过程中如果需要了解日志文件路径及文件名、日志写入模式,你可以读取LogMode 属性和LogPath属性的值。下面是一个简单的程序示例:
    App.StartLogging App.Path + "\mylog.log", vbLogAuto
    App.LogEvent "一切正常", VbLogEventTypeInformation
    MsgBox App.LogPath
    MsgBox App.LogMode
    需要注意的是,对日志操作的方法和属性只有在程序被编译为EXE 文件后才能发挥作用,在Visual Basic 调试环境下无效。

Guess you like

Origin blog.csdn.net/F2004/article/details/17277269