api函数有什么作用?为什么必须声明呢?不明白
如果不用它编程所得的程序有什么不一样呢?
希望举例说明,我很白痴的!!!

解决方案 »

  1.   

    API 就是编程接口,也就是函数库。
    你声明了,编译程序就能生成对它的调用。如果不用,有些事情你就干不了,或者必须自己写代码。
      

  2.   

    api是windows提供的一组编程接口,主要用于在vb自身的语句不能完成的地方,或用vb完成起来很复杂,或很低效的情况下。当然这不是硬性的,如果你喜欢赋个值都用 copymemory 来搞一下,也没什么不可。比如你要知道目前鼠标在屏幕的上的坐标,如果鼠标不在你的窗体上,或虽然在上面但没有任何动作,vb提供的功能是捕获不到的,这时就要用 api GetCursorPos()了。为什么要声明?那是因为语法要求。你说什么赋值要用a=1  而不是 a~1 呢?没有为什么,编译器要求的格式而已。有些语言如 delphi 使用 api 就不需要声明,也是编译器的原因。
      

  3.   

    哪有api例子?具体的!!!!
    你们说的,我只能明白一点,我要看了具体的例子,才知道!
      

  4.   

    建议楼主先学好基础。vb自身提供的功能都不熟的话,还去搞API干什么?比如下面的代码,是用 api 显示打印设置框:(语句看起来很吓人)Option Explicit' Global constants for Win32 API
    Private Const CCHDEVICENAME = 32
    Private Const CCHFORMNAME = 32
    Private Const GMEM_FIXED = &H0
    Private Const GMEM_MOVEABLE = &H2
    Private Const GMEM_ZEROINIT = &H40' Add appripriate Constants for what you want to change
    Private Const DM_DUPLEX = &H1000&
    Private Const DM_ORIENTATION = &H1&
    Private Const DM_COPIES = &H100&
    Private Const DMDUP_HORIZONTAL = 3
    Private Const DMDUP_SIMPLEX = 1
    Private Const DMDUP_VERTICAL = 2' type definitions:
    Private Type RECT
          Left As Long
          Top As Long
          Right As Long
          Bottom As Long
    End TypePrivate Type POINTAPI
        X As Long
        Y As Long
    End TypePrivate Type PRINTSETUPDLG_TYPE
        lStructSize As Long
        hWndOwner As Long
        hDevMode As Long
        hDevNames As Long
        flags As Long
        ptPaperSize As POINTAPI
        rtMinMargin As RECT
        rtMargin As RECT
        hInstance As Long
        lCustData As Long
        lpfnPageSetupHook As Long ' LPPAGESETUPHOOK
        lpfnPagePaintHook As Long ' LPPAGESETUPHOOK
        lpPageSetupTemplateName As String
        hPageSetupTemplate As Long ' HGLOBAL
    End TypePrivate Type PRINTDLG_TYPE
            lStructSize As Long
            hWndOwner As Long
            hDevMode As Long
            hDevNames As Long
            hdc As Long
            flags As Long
            nFromPage As Integer
            nToPage As Integer
            nMinPage As Integer
            nMaxPage As Integer
            nCopies As Integer
            hInstance As Long
            lCustData As Long
            lpfnPrintHook As Long
            lpfnSetupHook As Long
            lpPrintTemplateName As String
            lpSetupTemplateName As String
            hPrintTemplate As Long
            hSetupTemplate As Long
    End TypePrivate Type DEVNAMES_TYPE
            wDriverOffset As Integer
            wDeviceOffset As Integer
            wOutputOffset As Integer
            wDefault As Integer
            extra As String * 100
    End TypePrivate Type DEVMODE_TYPE
            dmDeviceName As String * CCHDEVICENAME
            dmSpecVersion As Integer
            dmDriverVersion As Integer
            dmSize As Integer
            dmDriverExtra As Integer
            dmFields As Long
            dmOrientation As Integer
            dmPaperSize As Integer
            dmPaperLength As Integer
            dmPaperWidth As Integer
            dmScale As Integer
            dmCopies As Integer
            dmDefaultSource As Integer
            dmPrintQuality As Integer
            dmColor As Integer
            dmDuplex As Integer
            dmYResolution As Integer
            dmTTOption As Integer
            dmCollate As Integer
            dmFormName As String * CCHFORMNAME
            dmUnusedPadding As Integer
            dmBitsPerPel As Integer
            dmPelsWidth As Long
            dmPelsHeight As Long
            dmDisplayFlags As Long
            dmDisplayFrequency As Long
    End Type' API declarations:
    Private Declare Function PrintDialog Lib "comdlg32.dll" Alias "PrintDlgA" (pPrintdlg As PRINTDLG_TYPE) As Long
    Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
        lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)Public Sub PrinterSetupDlg(hwnd As Long, Optional PrintFlags As Long = &H40&)      Dim PrintDlg As PRINTDLG_TYPE
          Dim DevMode As DEVMODE_TYPE
          Dim DevName As DEVNAMES_TYPE      Dim lpDevMode As Long, lpDevName As Long
          Dim bReturn As Integer
          Dim objPrinter As Printer, NewPrinterName As String
          Dim strSetting As String      ' Use PrintSetupDialog to get the handle to a memory
          ' block with a DevMode and DevName structures      PrintDlg.lStructSize = Len(PrintDlg)
          PrintDlg.hWndOwner = hwnd      PrintDlg.flags = PrintFlags      ' Set the current orientation and duplex setting
          DevMode.dmDeviceName = Printer.DeviceName
          DevMode.dmSize = Len(DevMode)
          DevMode.dmFields = DM_ORIENTATION Or DM_DUPLEX Or DM_COPIES
          DevMode.dmOrientation = Printer.Orientation
          DevMode.dmCopies = Printer.Copies
          
          On Error Resume Next
          DevMode.dmDuplex = Printer.Duplex
          On Error GoTo 0      ' Allocate memory for the initialization hDevMode structure
          ' and copy the settings gathered above into this memory
          PrintDlg.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or _
             GMEM_ZEROINIT, Len(DevMode))
          lpDevMode = GlobalLock(PrintDlg.hDevMode)
          If lpDevMode > 0 Then
              CopyMemory ByVal lpDevMode, DevMode, Len(DevMode)
              bReturn = GlobalUnlock(PrintDlg.hDevMode)
          End If      ' Set the current driver, device, and port name strings
          With DevName
              .wDriverOffset = 8
              .wDeviceOffset = .wDriverOffset + 1 + Len(Printer.DriverName)
              .wOutputOffset = .wDeviceOffset + 1 + Len(Printer.Port)
              .wDefault = 0
          End With
          With Printer
              DevName.extra = .DriverName & Chr(0) & .DeviceName & Chr(0) & .Port & Chr(0)
          End With      ' Allocate memory for the initial hDevName structure
          ' and copy the settings gathered above into this memory
          PrintDlg.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or _
              GMEM_ZEROINIT, Len(DevName))
          lpDevName = GlobalLock(PrintDlg.hDevNames)
          If lpDevName > 0 Then
              CopyMemory ByVal lpDevName, DevName, Len(DevName)
              bReturn = GlobalUnlock(lpDevName)
          End If      ' Call the print dialog up and let the user make changes
          If PrintDialog(PrintDlg) Then          ' First get the DevName structure.
              lpDevName = GlobalLock(PrintDlg.hDevNames)
                  CopyMemory DevName, ByVal lpDevName, 45
              bReturn = GlobalUnlock(lpDevName)
              GlobalFree PrintDlg.hDevNames          ' Next get the DevMode structure and set the printer
              ' properties appropriately
              lpDevMode = GlobalLock(PrintDlg.hDevMode)
                  CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
              bReturn = GlobalUnlock(PrintDlg.hDevMode)
              GlobalFree PrintDlg.hDevMode
              NewPrinterName = UCase$(Left(DevMode.dmDeviceName, _
                  InStr(DevMode.dmDeviceName, Chr$(0)) - 1))
              If Printer.DeviceName <> NewPrinterName Then
                  For Each objPrinter In Printers
                     If UCase$(objPrinter.DeviceName) = NewPrinterName Then
                          Set Printer = objPrinter
                     End If
                  Next
              End If
              On Error Resume Next          ' Set printer object properties according to selections made
              ' by user
              DoEvents
              With Printer
                  .Copies = DevMode.dmCopies
                  .Duplex = DevMode.dmDuplex
                  .Orientation = DevMode.dmOrientation
              End With
              
              On Error GoTo 0
          End If
    End Sub