热情软件屋

 

On Error GoTo是什么意思


编号:QA001096
建立日期: 1999年6月1日 最后修改日期:1999年6月1日
所属类别:

wy:
    VB
    Win9x
    Option Explicit
    Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
    Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
    Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long
    
     Private SysDir As String
     ' 保存系统目录路径
     Private IconCount As Long
     ' 图标数量
     Private MyIcon As Long
     ' 显示在图片框中的图标的句柄
    
     Private Sub Command1_Click()
    
     '(此处不懂)
     On Error GoTo ErrHandler
     With CommonDialog1
     .CancelError = True
     .DialogTitle = "选择图标文件"
     .InitDir = SysDir
     .Filter = "动态链接库|*.dll|可执行文件|*.exe"
     .FilterIndex = 2
     .ShowOpen
     End With
    
     IconCount = ExtractIcon(App.hInstance, CommonDialog1.filename, -1)
     Label4.Caption = CommonDialog1.filename
     Label5.Caption = IconCount
     Label6.Caption = "无"
     Command2.Enabled = IconCount
     If VScroll1.Enabled Then
     VScroll1.Value = 0
     VScroll1.Enabled = False
     End If
     Picture1.Cls
    
     ErrHandler:
     Exit Sub
    
     End Sub
    
     Private Sub Command2_Click()
    
     Picture1.Cls
     MyIcon = ExtractIcon(App.hInstance, CommonDialog1.filename, 0)
     Call DrawIcon(Picture1.hdc, 10, 10, MyIcon)
     Picture1.Refresh
     Call DestroyIcon(MyIcon)
     Label6.Caption = 1
    
     If IconCount > 1 Then
     VScroll1.Enabled = True
     VScroll1.Min = 0
     VScroll1.Max = IconCount - 1
     End If
    
     Command2.Enabled = False
    
     End Sub
    
     Private Sub Form_Load()
    
     Dim SysDir_Size As Long
     SysDir = Space(36) '如果你的系统目录名长度大于36,请设置成正确的长度
     SysDir_Size = GetSystemDirectory(SysDir, 35)
     SysDir = Left(SysDir, SysDir_Size)
    
     Command2.Enabled = False
     Label4.Caption = "无"
     Label5.Caption = "无"
     Label6.Caption = "无"
     Picture1.AutoRedraw = True
     Picture1.Cls
     VScroll1.TabStop = False
     VScroll1.Enabled = False
    
     End Sub
    
     Private Sub VScroll1_Change()
    
     Picture1.Cls
     MyIcon = ExtractIcon(App.hInstance, CommonDialog1.filename, VScroll1.Value)
     Call DrawIcon(Picture1.hdc, 10, 10, MyIcon)
     Picture1.Refresh
     Label6.Caption = VScroll1.Value + 1
     Call DestroyIcon(MyIcon)
     End Sub
    问题:
    这是一个提取文件图标的程序。
    1、Command1_Click()里有一句On Error GoTo ErrHandler不懂。 Error是什么地方来的?
    2、WINDOWS下的图标原理是什么?哪里有文章?

回答:

    On Error GoTo是出错处理,表示程序中出现错误就去ErrHandler执行有关代码。错误包括各种可能的错误,比如文件打不开等等。对于你这个例子,由于设置CommonDialog的CancelError为True,所以如果用户在选文件时选择Cancel就会产生错误,然后执行ErrHandler后的代码。
    Windows图标的格式可以参考http://www.wotsit.org/search.asp?s=windows。不过一般不需要直接处理.ico文件。主要是了解几个Windows API函数,如DrawIcon、LoadIcon、ExtractIcon。

此问题由李海回答。

附加关键字:编程, 源程序, programming, source code, Visual Basic, VB, 新手问题, newbie, faq, 错误信息, error, error message, link, compile, runtime

 
把这个问题推荐给朋友
   
   
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)
 
 

版权所有 1997-2008 热情软件屋
如果您有任何建议和意见, 请给我发个电子邮件 askpro@china-askpro.com
Web Designed by ZebraStudio