QBasic / QB64 Discussion Forum      Other Subforums, Links and Downloads
 

 Return to Index  

Accessing the Windows clipboard from Qbasic..

January 21 2002 at 11:52 AM
Alipha  (no login)

 
FAQ003 = How can I access the Windows clipboard from QBasic?

1) You must have QBasic 4.5+

2) You must remember to use the /L switch to include the qb.qlb.

3) If you have NT, 2K, or XP then (thanks, Plasma) you need to run NTOLDAPP before calling QBasic /L. If you don't have this program, download:
ftp://ftp.muc.de/people/cfaerber/comp/xp/ntold09b.zip

Here is a demo program showing how the clipboard is accessed.

This accesses interrupt &H2F, functions &H17## to manipulate the text clipboard (there are different clipboards for the different types of data). Note that, if the clipboard contains a lot of data, then you may not be able to open it.

Your program needs to open the clipboard before using the rest of the subs/functions. It should keep the clipboard open for as little time as possible, because other programs can't use the clipboard while it is open.

  -/\lipha
<aliphax@hotmail.com>
http://www.geocities.com/aliphax



DEFINT A-Z

DECLARE FUNCTION ClipboardText$ ()
DECLARE SUB OpenClipboard ()
DECLARE SUB CloseClipboard ()
DECLARE SUB SetClipboardText (text$)
DECLARE SUB EmptyClipboard ()

'$INCLUDE: 'qb.bi'

DIM SHARED regs AS regTypeX

CLS
PRINT "Demo of read and write clipboard"
PRINT
OpenClipboard
PRINT "Previous clipboard text: "; ClipboardText$
SetClipboardText "This will be replaced"
SetClipboardText "Hello, World!"
PRINT "New clipboard text: "; ClipboardText$
CloseClipboard
SYSTEM

FUNCTION ClipboardText$
  regs.ax = &H1704
  regs.dx = 1
  CALL INTERRUPTX(&H2F, regs, regs)

  IF regs.dx <> 0 OR regs.ax < 0 THEN
    PRINT "Too much data in clipboard."
    END
  END IF

  IF regs.ax = 0 THEN
    ClipboardText$ = ""
    EXIT FUNCTION
  END IF

  buffer$ = SPACE$(regs.ax)

  regs.ax = &H1705
  regs.dx = 1
  regs.es = VARSEG(buffer$)
  regs.bx = SADD(buffer$)
  CALL INTERRUPTX(&H2F, regs, regs)

  IF regs.ax = 0 THEN
    PRINT "Error while accessing clipboard data."
    END
  END IF

  ClipboardText$ = LEFT$(buffer$, INSTR(buffer$, CHR$(0)) - 1)
END FUNCTION

SUB CloseClipboard
  regs.ax = &H1708
  CALL INTERRUPTX(&H2F, regs, regs)

  IF regs.ax = 0 THEN
    PRINT "Unable to close"
    END
  END IF

END SUB

SUB EmptyClipboard

  regs.ax = &H1702
  CALL INTERRUPTX(&H2F, regs, regs)

  IF regs.ax = 0 THEN
    PRINT "Unable to empty clipboard."
    END
  END IF

END SUB

SUB OpenClipboard
  regs.ax = &H1701
  CALL INTERRUPTX(&H2F, regs, regs)

  IF regs.ax = 0 THEN
    PRINT "clipboard in use"
    END
  END IF

END SUB

SUB SetClipboardText (text$)

  t$ = text$ + CHR$(0)

  regs.ax = &H1703
  regs.dx = 1
  regs.es = VARSEG(t$)
  regs.bx = SADD(t$)
  regs.cx = LEN(t$)
  regs.si = 0
  CALL INTERRUPTX(&H2F, regs, regs)

  IF regs.ax = 0 THEN
    PRINT "Error while trying to place text in clipboard."
    END
  END IF

END SUB



    
This message has been edited by iorr5t on Mar 15, 2004 6:05 PM


 
 Respond to this message   
Responses

 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement