使用VB.net调用WinAPI方法介绍

发表于:2007-06-30来源:作者:点击数: 标签:
本贴精华段: 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 = nbsp;Const MB_
本贴精华段:
    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


以下附上本CALLAPI.VB的所有代码。各位可根据自己的要求自行修改。


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

原文转自:http://www.ltesting.net