包含vb.net调试类库的词条( 三 )


< System.Runtime.InteropServices.DllImport ( "Shell32.dll" ) > _
Public Function _
ExtractIcon ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
End Function
(2).使用“Declare”语句来申明Windows API函数:
使用“Declare”语句的确比使用DllImport特征类要简单了许多,下面是在Visual Basic .Net中使用“Declare”语句来声明上述二个Windows API函数的具体方法:
Declare Auto Function ExtractIcon Lib "Shell32.dll" Alias "ExtractIcon" ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
'声明ExtractIcon函数
Declare Auto Function Icon_Num Lib "Shell32.dll" Alias "ExtractIcon" ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As Integer ) As Integer
'声明Icon_Num函数
在Visual Basic .Net中声明Windows API函数时 , “Declare”语句中Alias关键字的作用相当于使用DllImport特征类中的EntryPoint对象字段 。同样在使用“Declare”语句声明Windows API函数时,如果声明的函数和函数的入口点相同,也可以省略Alias关键字对应的代码,所以ExtractIcon函数也可以简化为如下:
Declare Auto Function ExtractIcon Lib "Shell32.dll" ( ByVal src As System.IntPtr , ByVal strFileName As string , ByVal uiIconIndex As UInt32 ) As System.IntPtr
下面就结合一个示例的编写过程来掌握的这二个Windows API函数的具体使用方法,这个示例的作用就是读取指定文件中的图标数目,并显示文件中的图标 。
三.本文中程序的编写、调试和运行环境:
(1).视窗2000高级服务器版 。
(2).Visual Studio .Net 2003企业结构设计版,.Net FrameWork SDK版本号4322 。
四.Visual Basic .Net读取文件中的图标的实现步骤:
下面介绍的示例,其功能读取指定文件中包含的图标数目,并把这些图标全部显示出来 。下面是这个示例的实现步骤:
1. 启动Visual Studio .Net 。
2. 选择菜单【文件】|【新建】|【项目】后,弹出【新建项目】对话框 。
3. 将【项目类型】设置为【Visual Basic项目】 。
4. 将【模板】设置为【Windows应用程序】 。
5. 在【名称】文本框中输入【Visual Basic .Net查看文件中的图标】 。
6. 在【位置】的文本框中输入【E:\VS.NET项目】 , 然后单击【确定】按钮,这样在【E:\VS.NET项目】目录中就产生了名称为【Visual Basic .Net查看文件中的图标】文件夹 , 里面存放着【Visual Basic .Net查看文件中的图标】项目的所有文件 。具体如图02所示:
图02:【Visual Basic .Net查看文件中的图标】项目的【新建项目】对话框
7. 选择菜单【项目】|【添加新项】,在弹出的对话框中的【模板】设置为【模块】,【名称】文本框设置为【Module1.vb】后 。单击【打开】按钮 , 则在项目中增加了一个模板文件,名称为【Module1.vb】 。
8. 把Visual Stuido .Net的当前窗口切换到Module1.vb的代码编辑窗口,并在其Module1的代码区中添加下列代码,下列代码是用二种方式声明二个Windows API函数:
< System.Runtime.InteropServices.DllImport ( "Shell32.dll" ) > _
Public Function _
ExtractIcon ( ByVal src As System.IntPtr , ByVal strFileName As String , ByVal uiIconIndex As UInt32 ) As System.IntPtr
End Function
'声明ExtractIcon函数
Declare Auto Function Icon_Num Lib "Shell32.dll" Alias "ExtractIcon" ( ByVal src As System.IntPtr , ByVal strFileName As String , ByVal uiIconIndex As Integer ) As Integer
9. 把Visual Studio .Net的当前窗口切换到Form1窗体的设计查看,并从【工具箱】中的【Windows窗体】选项卡中拖入下列组件到Form1窗体:

推荐阅读