Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Clipboard functions in Fortran

jgte
Beginner
1,304 Views
I've done some searching on this issue, but with no results.

I want to retrive the contents of the clipboard (characters only) in one of my programs (built with CVF 6.6B). It appears to me that if the support was there, it would be a matter of calling some function which would return a character string with the contents of the clipboard. Does anyone have experience on this?

Thank you.
Joo
0 Kudos
6 Replies
Jugoslav_Dujic
Valued Contributor II
1,304 Views
Fragment from an actual app:
INTEGER::         iSt, i, hData
CHARACTER::       sText(*); POINTER(pText, sText)
...
IF (IsClipboardFormatAvailable(CF_TEXT)) THEN
   iSt = OpenClipboard(NULL)
   hData = GetClipboardData(CF_TEXT)
   pText = LocalLock(hData)
   !Do something with sText here
   iSt = LocalUnlock(hData)
   iSt = CloseClipboard()
END IF
Basically, sText is a string of "infinite" length, CHAR(0) terminated. You can copy it to your own string variable up to CHAR(0).

Jugoslav
0 Kudos
hbell
Beginner
1,304 Views
So is pasting text to the clipboard similar to this code fragment?

Harry Bell
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,304 Views
Um, pasting to? You mean, copying to? Yes, should be (but I didn't try). You have to allocate the memory using GlobalAlloc, obtain the normal pointer to it via GlobalLock (I think Cray (integer) pointers have to be used), copy the contents of your data into that pointer and call SetClipboardData. Don't forget to call GlobalUnlock, but do not call GlobalFree -- system will do it.

Jugoslav
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,304 Views
Ha, I did find a sample (it's for copying a bitmap so it's a tad complicated):
INTEGER::              iSizeImage, cClrBits, iSt, hmemBmBits, hClipboard
INTEGER(1)::           bmBits(*); POINTER(pbmBits, bmBits)
TYPE(T_BITMAPINFO)::   BI; POINTER(pBI,BI)
...
!Allocate memory for BITMAPINFO structure. It consists of BITMAPINFOHEADER
!structure followed immediately in memory by array of color bits.
hmemBmBits = GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, iSizeImage+SIZEOF(BI%bmiHeader))
pbmBits = GlobalLock(hmemBmBits)
pBI = pbmBits

!Fill in the BITMAPINFOHEADER.
BI%bmiHeader%biSize          = SIZEOF(BI%bmiHeader)
BI%bmiHeader%biWidth         = 100
BI%bmiHeader%biHeight        = 100
BI%bmiHeader%biPlanes        = 1                  !1 plane (e.g. icons have 2)
BI%bmiHeader%biBitCount      = cClrBits           !color depth
BI%bmiHeader%biCompression   = BI_RGB             !no compression
BI%bmiHeader%biSizeImage     = iSizeImage
BI%bmiHeader%biXPelsPerMeter = 96*100/2.54+1
BI%bmiHeader%biYPelsPerMeter = 96*100/2.54+1
BI%bmiHeader%biClrImportant  = 0

!Get the color bits into BI. Actual bits start at this offset--v
iSt = GetDIBits(hdcComp, hBmp, 0, 100, pbmBits+SIZEOF(BI%bmiHeader), &
pBI, DIB_RGB_COLORS)

!Release the resources for buffer
iSt = DeleteObject(hBmp)
iSt = DeleteDC(hdcComp)

hClipboard = OpenClipboard(GETHWNDQQ(QWIN$FRAMEWINDOW))

!Copy the contents onto clipboard
iSt = SetClipboardData(CF_DIB, hmemBmBits)

!Clean up
iSt = CloseClipboard()
iSt = GlobalUnlock(hmemBmBits)
0 Kudos
hbell
Beginner
1,304 Views
I'm having trouble following Jugoslav's second example and how it can be morphed into code to copy text onto the clipboard (after eliminating what already exists on the clipboard). Does anybody have a simpler code fragment for copying text onto the Windows clipboard?

Harry Bell
0 Kudos
hbell
Beginner
1,304 Views
Below is a far simpler CVF code fragment that I finally
figured out for pasting text onto the clipboard.

Clipstring is a zero terminated string, all other variables are integer*4.
Ilen is the length of clipstring from the first character through the null byte:

...
pstring = LOC(clipstring)
iSt = OpenClipboard(NULL)
iSt = EmptyClipboard()
hData = GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, ilen)
pdummy = GlobalLock(hData)
call CopyMemory(pdummy,pstring,ilen)
iSt = SetClipboardData(CF_TEXT,hData)
iSt = CloseClipboard()
iSt = GlobalUnlock(hData)   
...


Harry Bell
0 Kudos
Reply