home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 August / PCWorld_2000-08_cd.bin / Software / TemaCD / xbasic / xbpro.exe / xb / api.x < prev    next >
Text File  |  1999-12-14  |  3KB  |  94 lines

  1. '
  2. ' ####################
  3. ' #####  PROLOG  #####
  4. ' ####################
  5. '
  6. ' This program gets the contents of the system clipboard twice and
  7. ' prints it in the console window.  First it calls a GraphicsDesigner
  8. ' function to get the clipboard text.  Then it calls the GetClipText()
  9. ' function to get the clipboard text.  GetClipText() calls Windows API
  10. ' functions to accomplish its mission, which demonstrates the process
  11. ' of calling API functions.  The API functions are in the "user32" and
  12. ' "kernel32" libraries, which means they need to be imported - which
  13. ' they are in two of the IMPORT statements below.  "xgr" is imported
  14. ' because XgrGetClipboard() is part of the GraphicsDesigner library.
  15. '
  16. ' Make sure you've got text in the clipboard or this will be BORING !!!
  17. '
  18. PROGRAM "api"
  19. VERSION "0.0001"
  20. '
  21. IMPORT    "xgr"           ' GraphicsDesigner
  22. IMPORT    "user32"        ' Windows API
  23. IMPORT    "kernel32"    ' Windows API
  24. '
  25. DECLARE  FUNCTION  Entry       ()
  26. INTERNAL FUNCTION  GetClipText (@text$)
  27. '
  28. '
  29. ' ######################
  30. ' #####  Entry ()  #####
  31. ' ######################
  32. '
  33. FUNCTION  Entry ()
  34.     UBYTE  image[]
  35. '
  36.     PRINT
  37.     PRINT "#####  program started  #####"
  38.     text$ = ""
  39.     DIM image[]
  40.     PRINT "*****  XgrGetClipboard(0)  *****"
  41.     XgrGetClipboard (0, $$ClipTypeText, @text$, @image[])
  42.     PRINT "<"; text$; ">", UBOUND (image[])
  43. '
  44.     text$ = ""
  45.     DIM image[]
  46.     PRINT "*****  XgrGetClipboard(1)  *****"
  47.     XgrGetClipboard (1, $$ClipTypeText, @text$, @image[])
  48.     PRINT "<"; text$; ">", UBOUND (image[])
  49. '
  50.     text$ = ""
  51.     DIM image[]
  52.     PRINT "*****  Windows API funcs  *****"
  53.     GetClipText (@text$)
  54.     PRINT "<"; text$; ">", UBOUND (image[])
  55. '
  56.     PRINT "#####  program completed  #####"
  57. END FUNCTION
  58. '
  59. '
  60. ' ############################
  61. ' #####  GetClipText ()  #####
  62. ' ############################
  63. '
  64. FUNCTION  GetClipText (text$)
  65.     $Text = 1
  66. '
  67.     text$ = ""                                                            ' default text$ is empty
  68.     ok = OpenClipboard (0)                                    ' open clipboard
  69.     IFZ ok THEN CloseClipboard() : RETURN        ' bad clipboard
  70.     handle = GetClipboardData ($Text)                ' get clipboard data handle
  71.     IF (handle <= 0) THEN CloseClipboard() : RETURN    ' bad handle
  72.     IFZ handle THEN RETURN                                                    ' no text in clipboard
  73.     addr = GlobalLock (handle)                                            ' get address of text
  74.     IFZ addr THEN CloseClipboard() : RETURN                    ' bad address
  75.     upper = GlobalSize (handle)                                            ' get upper bound of text$
  76.     IF (upper <= 0) THEN CloseClipboard() : RETURN    ' valid size ???
  77.     text$ = NULL$(upper)                                        ' create return string
  78.     d = -1                                                                    ' destination offset
  79.     lastByte = 0                                                        ' nothing to start
  80.     FOR s = 0 TO upper                                            ' for whole loop
  81.         byte = UBYTEAT (addr, s)                            ' get next byte
  82.         IFZ byte THEN EXIT FOR                                ' done on null terminator
  83.         IF (byte = '\n') THEN                                    ' if this byte is a newline
  84.             IF (lastByte = '\r') THEN DEC d            ' overwrite if lastByte was a <cr>
  85.         END IF                                                                '
  86.         INC d
  87.         text${d} = byte                                                ' byte to text string
  88.         lastByte = byte                                                ' lastByte = this byte
  89.     NEXT s                                                                    ' next source byte
  90.     text$ = LEFT$(text$,d+1)                                ' give text$ correct length
  91.     CloseClipboard ()
  92. END FUNCTION
  93. END PROGRAM
  94.