VB.NET调用WinAPI实例探讨
创始人
2024-06-21 08:41:32
0

VB.NET作为一款功能强大的面向对象语言,为我们带来了很多不一样的体验。我们今天可以通过对VB.NET调用WinAPI的操作技巧的掌握,来体验一下这款语言给我们带来的不同之处,从而初步掌握它的应用技巧。

以下为VB.NET调用WinAPI的演示实例:

  1. Declare Auto Function MBox Lib 
    "user32.dll" _  
  2. Alias "MessageBox" (ByVal hWnd 
    As Integer, _  
  3. ByVal txt As String, ByVal 
    caption As String, _  
  4. ByVal Typ As Integer) As Integer  
  5. '定义一些要调用参数  
  6. Const MB_ICONQUESTION = &H20L  
  7. Const MB_YESNO = &H4  
  8. Const IDYES = 6 
  9. Const IDNO = 7 
  10. Private Sub Button1_Click(ByVal 
    sender As System.Object, ByVal e 
    As System.EventArgs) Handles 
    Button1.Click  
  11. Dim RetVal As Integer ' 存储返回的值.  
  12. RetVal = MBox(0, "调用WinApi成功否?", 
    "Windows API 信息框", _  
  13. MB_ICONQUESTION Or MB_YESNO)  
  14. ' Check the return value.  
  15. If RetVal = IDYES Then  
  16. MsgBox("您选择了是")  
  17. Else  
  18. MsgBox("您选择了不是")  
  19. End If  
  20. End Sub 

以下附上本CALLAPI.vb的所有代码。各位可根据自己的要求对这段VB.NET调用WinAPI代码自行修改。

  1. Public Class CallApi  
  2. Inherits System.Windows.Forms.Form  
  3. Declare Auto Function MBox Lib 
    "user32.dll" _  
  4. Alias "MessageBox" (ByVal hWnd
     As Integer, _  
  5. ByVal txt As String, ByVal 
    caption As String, _  
  6. ByVal Typ As Integer) As Integer  
  7. '定义一些要调用参数  
  8. Const MB_ICONQUESTION = &H20L  
  9. Const MB_YESNO = &H4  
  10. Const IDYES = 6 
  11. Const IDNO = 7 
  12. #Region " Windows Form Designer 
    generated code "  
  13. Public Sub New()  
  14. MyBase.New()  
  15. 'This call is required by the 
    Windows Form Designer.  
  16. InitializeComponent()  
  17. 'Add any initialization after the 
    InitializeComponent() call  
  18. End Sub  
  19. 'Form overrides dispose to clean 
    up the component list.  
  20. Protected Overloads Overrides Sub 
    Dispose(ByVal disposing As Boolean)  
  21. If disposing Then  
  22. If Not (components Is Nothing) Then  
  23. components.Dispose()  
  24. End If  
  25. End If  
  26. MyBase.Dispose(disposing)  
  27. End Sub  
  28. 'Required by the Windows 
    Form Designer  
  29. Private components As System.
    ComponentModel.IContainer  
  30. 'NOTE: The following procedure is 
    required by the Windows Form Designer  
  31. 'It can be modified using the 
    Windows Form Designer.   
  32. 'Do not modify it using the code editor.  
  33. Friend WithEvents Button1 As System.
    Windows.Forms.Button  
  34.  
    Private Sub InitializeComponent()  
  35. Me.Button1 = New System.Windows.Forms.Button()  
  36. Me.SuspendLayout()  
  37. '  
  38. 'Button1  
  39. '  
  40. Me.Button1.Location = New System.
    Drawing.Point(88, 56)  
  41. Me.Button1.Name = "Button1" 
  42. Me.Button1.Size = New System.Drawing.Size(168, 48)  
  43. Me.Button1.TabIndex = 0 
  44. Me.Button1.Text = "调用API的信息框" 
  45. '  
  46. 'CallApi  
  47. '  
  48. Me.AutoScaleBaseSize = New System.
    Drawing.Size(5, 13)  
  49. Me.ClientSize = New System.Drawing.Size(384, 205)  
  50. Me.Controls.AddRange(New System.Windows.
    Forms.Control() {Me.Button1})  
  51. Me.Name = "CallApi" 
  52. Me.Text = "CallApi" 
  53. Me.ResumeLayout(False)  
  54. End Sub  
  55. #End Region  
  56. Private Sub Button1_Click(ByVal sender 
    As System.Object, ByVal e As System.
    EventArgs) Handles Button1.Click  
  57. Dim RetVal As Integer ' 存储返回的值.  
  58. RetVal = MBox(0, "调用WinApi成功否?",
     "Windows API 信息框", _  
  59. MB_ICONQUESTION Or MB_YESNO)  
  60. ' Check the return value.  
  61. If RetVal = IDYES Then  
  62. MsgBox("您选择了是")  
  63. Else  
  64. MsgBox("您选择了不是")  
  65. End If  
  66. End Sub  
  67. End Class 

VB.NET调用WinAPI代码示例就为大家介绍到这里。

【编辑推荐】

  1. 两种VB.NET发送邮件方法解读
  2. 如何对VB.NET流进行正确操作
  3. VB.NET创建过程相关步骤详解
  4. VB.NET可执行语句示例代码解读
  5. VB.NET声明语句具体应用方法详解

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...