AddressOf 运算符 您所在的位置:网站首页 sub地址的用法 AddressOf 运算符

AddressOf 运算符

2024-05-28 03:02| 来源: 网络整理| 查看: 265

AddressOf 运算符 项目04/07/2023

一个一元运算符,可导致它之后的过程的地址传递到 API 过程,该 API 过程应是参数列表中该位置处的一个函数指针。

语法

AddressOfprocedurename

必需的 procedurename 指定要传递其地址的过程。 它必须表示进行调用的项目中标准模块中的过程。

备注

当过程名称出现在参数列表中时,通常会评估该过程,并且传递过程的返回值的地址。 AddressOf 允许将过程的地址传递到动态链接库中的 Windows API 函数 , (DLL) ,而不是传递过程的返回值。 该 API 函数然后可以使用该地址来调用 Basic 过程(此过程称为回调)。 AddressOf 运算符只出现在调用 API 过程时。

尽管可以使用 AddressOf 在 Basic 过程之间传递过程指针,但不能通过这种指针从 Basic 内调用函数。 例如,这意味着在 Basic 中编写的 类 无法使用此类指针对其控制器进行回调。 当使用 AddressOf 在 Basic 内的过程间传递过程指针时,键入的被调用过程的参数必须为 Long。

如果您不完全了解函数回调概念,那么使用 AddressOf 可能会导致不可预知的结果。 您必须了解回调的 Basic 部分如何工作,以及要向其中传递函数地址的 DLL 的代码。 调试此类交互非常困难,因为程序在 与开发环境相同的进程中运行。 In some cases, systematic debugging may not be possible.

注意

[!注释] 可以在用 Microsoft Visual C++(或类似工具)编译的 Dll 中创建您自己的回调函数原型。 要使用 AddressOf ,您的原型必须使用 __stdcall 调用约定。 默认调用约定 (__cdecl) 不能与 AddressOf 结合使用。

由于回调的调用方不在程序中,因此不要将回调过程中的错误传播回调用方,这一点很重要。 可以通过将 On Error Resume Next 语句放在回调过程的开头来完成此操作。

示例

下面的示例创建一个带列表框的窗体,其中包含您的系统中按字母顺序排列的字体列表。

要运行此示例,请创建一个带列表框的窗体。 窗体的代码如下所示:

Option Explicit Private Sub Form_Load() Module1.FillListWithFonts List1 End Sub

将下面的代码放在模块中。 EnumFontFamilies 函数定义中的第三个参数是 Long ,代表一个过程。 该参数必须包含过程的地址,而非过程返回的值。 在调用 EnumFontFamilies 时,第三个参数要求 AddressOf 运算符返回 EnumFontFamProc 过程的地址,这是您在调用 Windows API 函数 EnumFontFamilies 时提供的回调过程的名称。 将 AddressOf EnumFontFamProc 传递给 EnumFontFamilies 时,Windows 会对系统上每个字体系列调用一次 EnumFontFamProc。 传递给 EnumFontFamilies 的最后的参数指定用来显示信息的列表框。

'Font enumeration types Public Const LF_FACESIZE = 32 Public Const LF_FULLFACESIZE = 64 Type LOGFONT lfHeight As Long lfWidth As Long lfEscapement As Long lfOrientation As Long lfWeight As Long lfItalic As Byte lfUnderline As Byte lfStrikeOut As Byte lfCharSet As Byte lfOutPrecision As Byte lfClipPrecision As Byte lfQuality As Byte lfPitchAndFamily As Byte lfFaceName(LF_FACESIZE) As Byte End Type Type NEWTEXTMETRIC tmHeight As Long tmAscent As Long tmDescent As Long tmInternalLeading As Long tmExternalLeading As Long tmAveCharWidth As Long tmMaxCharWidth As Long tmWeight As Long tmOverhang As Long tmDigitizedAspectX As Long tmDigitizedAspectY As Long tmFirstChar As Byte tmLastChar As Byte tmDefaultChar As Byte tmBreakChar As Byte tmItalic As Byte tmUnderlined As Byte tmStruckOut As Byte tmPitchAndFamily As Byte tmCharSet As Byte ntmFlags As Long ntmSizeEM As Long ntmCellHeight As Long ntmAveWidth As Long End Type ' ntmFlags field flags Public Const NTM_REGULAR = &H40& Public Const NTM_BOLD = &H20& Public Const NTM_ITALIC = &H1& ' tmPitchAndFamily flags Public Const TMPF_FIXED_PITCH = &H1 Public Const TMPF_VECTOR = &H2 Public Const TMPF_DEVICE = &H8 Public Const TMPF_TRUETYPE = &H4 Public Const ELF_VERSION = 0 Public Const ELF_CULTURE_LATIN = 0 ' EnumFonts Masks Public Const RASTER_FONTTYPE = &H1 Public Const DEVICE_FONTTYPE = &H2 Public Const TRUETYPE_FONTTYPE = &H4 Declare Function EnumFontFamilies Lib "gdi32" Alias _ "EnumFontFamiliesA" _ (ByVal hDC As Long, ByVal lpszFamily As String, _ ByVal lpEnumFontFamProc As Long, LParam As Any) As Long Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, _ ByVal hDC As Long) As Long Function EnumFontFamProc(lpNLF As LOGFONT, lpNTM As NEWTEXTMETRIC, _ ByVal FontType As Long, LParam As ListBox) As Long Dim FaceName As String Dim FullName As String FaceName = StrConv(lpNLF.lfFaceName, vbUnicode) LParam.AddItem Left$(FaceName, InStr(FaceName, vbNullChar) - 1) EnumFontFamProc = 1 End Function Sub FillListWithFonts(LB As ListBox) Dim hDC As Long LB.Clear hDC = GetDC(LB.hWnd) EnumFontFamilies hDC, vbNullString, AddressOf EnumFontFamProc, LB ReleaseDC LB.hWnd, hDC End Sub 另请参阅 无效的 AddressOf 运算符使用 运算符摘要 支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有