Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Clipboard Class


  • Please log in to reply
No replies to this topic
nnnik
  • Members
  • 1625 posts
  • Last active: Jan 24 2019 02:19 PM
  • Joined: 28 Jul 2012

I've been working with Clipboard Dlls recently. The result is this Class.

It is mainly to save and restore but i could also add Features if you request them.

There is no warranty since I didnt had enough time to test it.

If you come across a bug just post it here.

 

Here is the Code:

    Class clip
    {
    maxsaves:=20	
     
    __new()
    {
    this.saves:=[]
    this.types:=[15,2,A_IsUnicode?13:1]
    }	
    Save(){
    If ((this.saves.MaxIndex()?this.saves.MaxIndex():0)+1>this.maxsaves)
    this.Deletesave()
    a:={}
    For each,type in this.types
    If c:=this.SaveClipboard(type)
    a[type]:=c
    this.saves.Insert(a)
    return this.saves.Maxindex()
    }
     
    Restore(index=1,preferedtype=0)
    {
    If this.saves[index,preferedtype]
    return this.RestoreClipboard(preferedtype,this.saves[index,preferedtype])
    For each,typesave in this.saves[index]
    If typesave
    return this.RestoreClipboard(each,typesave)
    }
     
    Deletesave(nr=1)
    {
    For each, savestype in this.saves[nr]
    DllCall("GlobalFree","ptr",savestype)
    this.saves.remove(nr)
    return nr
    }
     
    SaveClipboard(type)
    {
    If !DllCall("OpenClipboard")
    return -1
    hMem:=0
    If DllCall("IsClipboardFormatAvailable","UInt",type)
    hMem:=DllCall("GetClipboardData","UInt",type)
    if !hmem
    return 0*(DllCall("CloseClipboard"))
    if !s:=DllCall("GlobalSize","ptr",hmem)
    return 0*(DllCall("CloseClipboard"))
    pm:=DllCall("GlobalAlloc","UInt",0,"ptr",s)
    pmem:=DllCall("GlobalLock","ptr",hmem)
    DllCall("RtlMoveMemory","ptr",pm,"ptr",pmem,"ptr",s)
    DllCall("GlobalUnlock","ptr",hMem)
    DllCall("CloseClipboard")
    return pm
    }
     
    RestoreClipboard(type,pm)
    {
    If !DllCall("OpenClipboard")
    return -1
    DllCall("EmptyClipboard")
    s:=DllCall("GlobalSize","ptr",pm)
    hMem:=DllCall("GlobalAlloc","UInt",2,"ptr",s)
    pmem:=DllCall("GlobalLock","ptr",hmem)
    DllCall("RtlMoveMemory","ptr",pmem,"ptr",pm,"ptr",s)
    DllCall("GlobalUnlock","ptr",hMem)
    hMem:=DllCall("SetClipboardData","UInt",type,"ptr",hMem)
    DllCall("CloseClipboard")
    return hMem
    }
    }

And here is a simple explination Script:

    cli:=new clip()
    a:=clipboard
    cli.save()
    cli.restore()
    msgbox % (a=clipboard)?"It's the same.":"Error"

Todo:

Better Error handling and more Documentation


Visit the new forum ahkscript.org.

http://ahkscript.org