andy8496 发表于 2022-4-12 10:42:53

fortran读写剪贴板

VS+ivf+Win环境下,Fortran能否读、写系统的剪贴板呢?

楚香饭 发表于 2022-4-12 12:02:31

本帖最后由 楚香饭 于 2022-4-12 12:15 编辑

这个以前恰好写过。不过只能读取粘贴板里的纯文本。对于 图像,文件,其他数据类型没有用。

Program www_fcode_cn
implicit none
character(len=100) :: c
integer :: j
j = getClipboardText(c)
if(j>0) write(*,'(a)') trim(c)

contains

Integer Function getClipboardText( text ) result( length )
    use , intrinsic :: ISO_C_Binding
    use user32
    use kernel32
    Character(len=*) :: text
    integer   :: hData , j
    type(c_ptr) :: cp
    character(len=len(text)) , pointer :: p
    length = 0
    text   = ""
    if ( 0==OpenClipboard(0) ) return
    hData = GetClipboardData(CF_TEXT)
    if ( hData /= 0 ) then
      call c_f_pointer(transfer(GlobalLock(hData),cp),p)
      length = index(p,c_null_char)-1
      if(length<0) length = len(text)
      text = p(:length)
      j = GlobalUnlock(hData)
    end if
    j = CloseClipboard()
End Function getClipboardText

End Program www_fcode_cn

andy8496 发表于 2022-4-12 15:16:02

楚香饭 发表于 2022-4-12 12:02
这个以前恰好写过。不过只能读取粘贴板里的纯文本。对于 图像,文件,其他数据类型没有用。



ISO_C_Binding真是大神器啊!
多谢老群主!

andy8496 发表于 2023-2-28 10:02:46

本帖最后由 andy8496 于 2023-2-28 10:07 编辑

我依葫芦画瓢写了个设置剪贴板内容的子程序,Win 7能正常运行,但是Win 10就不行了……烦请大神帮给看看,多谢多谢!





subroutine setClipboardText( text )
use , intrinsic :: ISO_C_Binding
use user32
use kernel32
Character(len=*) :: text
integer   :: hData , j
type(c_ptr) :: cp
character(len=len(text)) , pointer :: p

if ( 0==OpenClipboard(0) ) return
j = EmptyClipboard()

hData = GlobalAlloc(GMEM_MOVEABLE,len(text)+1)
if ( hData /= 0 ) then
    call c_f_pointer(transfer(GlobalLock(hData),cp),p)
    p = text
    j = GlobalUnlock(hData)
    j = SetClipboardData(CF_TEXT,hData)
end if
j = CloseClipboard()
End subroutine

fcode 发表于 2023-2-28 16:07:58

这......自己挖自己的坟?一年了,老哥。

andy8496 发表于 2023-2-28 16:42:00

fcode 发表于 2023-2-28 16:07
这......自己挖自己的坟?一年了,老哥。

;-P 一直用的Win 7
页: [1]
查看完整版本: fortran读写剪贴板