Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Win32 API SetClipboardData
#1
@Spriggsy

I tried to add a SetClipboardData to your WinAPI routine below, but no luck. No luck as in my addition of SetClipboardData caused the program to crash before the console even opened. I posted your routine below. Could it be modified so instead of just reading the present clipboard contents, it gets the highlighted text from the active window and adds that highlighted text to the clipboard? If that would be a PITA to code, just let me know and don't go to the trouble. What I'm looking for is an API alternative to using _SCREENPRINT CHR$(3) because in our IDE, if applied from another program, it doesn't copy the QB64 text, it just removes the highlighted text and leaves  lowercase c in its place.

Code: (Select All)
' Display Clipboard Contents by Spriggsy.
OPTION _EXPLICIT
$CONSOLE:ONLY
_DEST _CONSOLE
'CONSTANTS
CONST CF_TEXT = 1
CONST CF_BITMAP = 2
CONST CF_METAFILEPICT = 3
CONST CF_SYLK = 4
CONST CF_DIF = 5
CONST CF_TIFF = 6
CONST CF_OEMTEXT = 7
CONST CF_DIB = 8
CONST CF_PALETTE = 9
CONST CF_PENDATA = 10
CONST CF_RIFF = 11
CONST CF_WAVE = 12
CONST CF_UNICODETEXT = 13
CONST CF_ENHMETAFILE = 14
CONST CF_HDROP = 15
CONST CF_LOCALE = 16
CONST CF_DIBV5 = 17
CONST CF_MAX = 18

DECLARE DYNAMIC LIBRARY "User32"
    FUNCTION OpenClipboard%% (BYVAL hWndNewOwner AS LONG)
    'FUNCTION CountClipboardFormats% ()
    FUNCTION GetClipboardData& (BYVAL uFormat AS _UNSIGNED INTEGER)
    FUNCTION CloseClipboard%% ()
END DECLARE

DECLARE DYNAMIC LIBRARY "Kernel32"
    FUNCTION GlobalLock%& (BYVAL hMem AS LONG)
    FUNCTION GlobalUnlock%% (BYVAL hMem AS LONG)
END DECLARE

DECLARE CUSTOMTYPE LIBRARY "peekpoke"
    FUNCTION peekb~%% (BYVAL p AS _UNSIGNED _OFFSET) 'Byte
END DECLARE

PRINT Clipboard$
SLEEP

FUNCTION Clipboard$
    DIM a AS LONG
    a = OpenClipboard(0)
    a = GetClipboardData(CF_TEXT)
    DIM b AS _UNSIGNED _OFFSET
    b = GlobalLock(a)
    DIM x AS _UNSIGNED INTEGER
    DIM clip AS STRING
    DO
        clip = clip + CHR$(peekb(b + x))
        x = x + 1
    LOOP UNTIL CHR$(peekb(b + x)) = CHR$(0)
    DIM closeclip AS _BYTE
    closeclip = GlobalUnlock(a)
    closeclip = CloseClipboard
    Clipboard = clip
END FUNCTION

Pete
Reply
#2
To be honest, I don't remember much about this code. I made it a long time ago. I'd need to get familiar with it again. The peekpoke library is a bit unstable and I'm betting that's where the issue is coming from. I have better routines now that could be used instead. Not sure how quickly I can get back to you on this, though.
Tread on those who tread on you

Reply
#3
(11-03-2022, 12:00 PM)Pete Wrote: No luck as in my addition of SetClipboardData caused the program to crash before the console even opened.
Because Freebasic has had no such functionality as "_CLIPBOARD$", a few years ago I tried my hand at a solution with Win32 API. It had to be text only, and it crashed periodically. It was infuriating.

But could "_SCREENPRINT" yet do an "alt" key combination? If so could do [ALT][E] then "c" in the QB64PE IDE. Otherwise would have to figure out where the window is and "_SCREENCLICK" with the mouse to open the "Edit" menu and then "_SCREENPRINT" the "c". I know, getting complicated now.
Reply
#4
Idea Idea Idea  I love talking things out on a forum. I'm going to try a workaround with a SENDKEY altrnative. Now sure, that would just print the "c" to the screen just like _SCREENPRINT but mn gave reminded me of something I used to do before we had that keyword. Yes, I could open the menu but the good news is I don't have to rely on _SCREENCLICK anymore. What I'm going to take a shot at is using SENDKEY to open the QB64 IDE edit menu and then send the "c" selection to it. I have some stuff to do around the house first, and then I'll report back.

@Spriggsy, my bet is you are doing other stuff this afternoon, so please don't bother yourself with this, as I'm pretty certain I can work this out now.

Thanks +1 both!

Pete
Reply
#5
Okay workaround works!

Code: (Select All)
' To test have another QB64 IDE open and some text you want copied highlighted.
' Run this program and click the title bar of the other open IDE, so it is active. You have 4-seconds to do so.
' When you hear the beep, open notepad and do a paste. You should see the content of your IDE highlighted text.

DECLARE DYNAMIC LIBRARY "user32"
    SUB SENDKEYS ALIAS keybd_event (BYVAL bVk AS LONG, BYVAL bScan AS LONG, BYVAL dwFlags AS LONG, BYVAL dwExtraInfo AS LONG)
END DECLARE

CONST KEYEVENTF_KEYUP = &H2
CONST VK_ALT = &H12 'Alt key

SLEEP 4 ' Hurry, click on your other QB64 highlighted text IDE.

SENDKEYS VK_ALT, 0, 0, 0
SENDKEYS &H45, 0, 0, 0 ' Alt+E open IDE edit menu.
SENDKEYS &H43, 0, 0, 0 ' C

SENDKEYS &H43, 0, KEYEVENTF_KEYUP, 0
SENDKEYS &H45, 0, KEYEVENTF_KEYUP, 0
SENDKEYS VK_ALT, 0, KEYEVENTF_KEYUP, 0

BEEP ' All done, check your clipboard contents!
_DELAY 3
END

Pete
Reply




Users browsing this thread: 1 Guest(s)