VB.NET调用WinAPI实例探讨

开发 后端
对于初学者来说,要想掌握VB.NET的应用技巧,就应该从实践中去积累经验。今天为大家带来的VB.NET调用WinAPI这一操作方法就能帮助大家提高这方面的经验积累。

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

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

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

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

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

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

【编辑推荐】

  1. 两种VB.NET发送邮件方法解读
  2. 如何对VB.NET流进行正确操作
  3. VB.NET创建过程相关步骤详解
  4. VB.NET可执行语句示例代码解读
  5. VB.NET声明语句具体应用方法详解
责任编辑:曹凯 来源: wewill.cn
相关推荐

2009-11-04 11:32:20

VB.NET回调函数

2010-01-19 14:42:43

VB.NET调用过程重

2009-10-30 16:31:55

VB.NET重载方法

2009-10-28 14:13:32

VB.NET File

2009-10-28 14:34:44

VB.NET Tree

2009-10-13 11:22:46

VB.NET调用Web

2009-10-26 16:53:00

VB.NET常用代码

2009-10-21 09:40:23

VB.NET搜索

2009-10-22 09:20:46

VB.NET Proc

2010-01-19 13:36:16

VB.NET可选参数

2009-11-03 12:52:38

VB.NET Wind

2009-11-10 16:55:05

VB.NET调用API

2010-01-11 17:24:19

VB.NET异步调用

2009-10-29 11:26:28

VB.NET调用Web

2009-10-26 19:22:29

VB.NET使用Log

2009-10-19 17:42:31

VB.NET API函

2009-04-30 13:24:45

VB.NET 2008窗体应用实例

2010-01-21 14:27:46

VB.NET判断数组维

2009-10-15 17:50:48

VB.NET调用API

2010-01-19 09:48:22

VB.NET调用过程
点赞
收藏

51CTO技术栈公众号