[Js] interaction between javascript and clipboard

Source: Internet
Author: User

1. How can I copy, cut, and paste the clipboard? At the same time, determine whether the data in the clipboard is text?
If (! IsClipboardFormatAvailable (CF_TEXT ))
Return;
If (! OpenClipboard (hwndMain ))
Return;

Hglb = GetClipboardData (CF_TEXT );
If (hglb! = NULL)
{
Lptstr = GlobalLock (hglb );
If (lptstr! = NULL)
{
// Call the application-defined ReplaceSelection
// Function to insert the text and repaint
// Window.

ReplaceSelection (hwndSelected, pbox, lptstr );
GlobalUnlock (hglb );
}
}
CloseClipboard ();

2. Can I use javascript to obtain the string in the windows clipboard?
For example, you can click a text box on a webpage to paste the characters in the clipboard.

Of course.
<Form>
<P>
<Input name = txtSearch value = "">
<Input type = button value = Copy2Clip onclick = 'javascript: var textRange = txtSearch. createTextRange (); textRange.exe cCommand ("Copy") '>
</P>
<P>
<Input name = "copyto" type = "text" id = "copyto">
<Input type = button value = PastefromClip onclick = 'javascript: var textRange = copyto. createTextRange (); textRange.exe cCommand ("Paste") '>
</P>
</Form>

3. Interaction between javascript and clipboard

You can copy the content of an object with the id of 'objid' to the clipboard.

Var rng = document. body. createTextRange ();
Rng. moveToElementText (document. getElementById ("objid "));
Rng. scrollIntoView ();
Rng. select ();
Rng.exe cCommand ("Copy ");
Rng. collapse (false );
SetTimeout ("window. status ='' ", 1800)

You can also use rng.exe cCommand ("Past"); to paste the content of the clipboard to the current position of the cursor.

For more information, see the textRange object of msdn.

However, all the html tags that are copied to the clipboard are filtered out without html tags.


4. window. clipboardData. getData ("Text") // you can obtain the edited Text.
Window. clipboardData. setData ("Text", "your content") // write Text information to the clipboard

5. How can I determine whether the data in the clipboard is a string, not an image or other information?

Private Sub commandementclick ()
If Clipboard. GetFormat (vbCFText) Or Clipboard. GetFormat (vbCFRTF) Then
MsgBox "OK"
End If
End Sub





6. How can I judge whether the clipboard is not empty?


I,

Eg
Checks whether the windows clipboard is empty. If not, reads the Image to the Image.
Uses clipbrd;

If ClipBoard. HasFormat (CF_Picture) then
Image1.Picture. Assign (ClipBoard );
II,

Uses Clipbrd;

Procedure TForm1.Button1Click (Sender: TObject );
Begin
If Clipboard. FormatCount <= 0 then
{TODO: NULL };
End;



7. How do I determine whether the data in the clipboard is an image?


Example of the GetFormat Method
In this example, the GetFormat method is used to determine the data format on the Clipboard object. To verify this example, paste the sample code to the declaration section of a form, press F5, and click the form.

Private Sub Form_Click ()
'Defines various bitmap formats.
Dim ClpFmt, Msg 'declares the variable.
On Error Resume Next 'sets Error handling.
If Clipboard. GetFormat (vbCFText) Then ClpFmt = ClpFmt + 1
If Clipboard. GetFormat (vbCFBitmap) Then ClpFmt = ClpFmt + 2
If Clipboard. GetFormat (vbCFDIB) Then ClpFmt = ClpFmt + 4
If Clipboard. GetFormat (vbCFRTF) Then ClpFmt = ClpFmt + 8
Select Case ClpFmt
Case 1
Msg = "The Clipboard contains only text ."
Case 2, 4, 6
Msg = "The Clipboard contains only a bitmap ."
Case 3, 5, 7
Msg = "The Clipboard contains text and a bitmap ."
Case 8, 9
Msg = "The Clipboard contains only rich text ."
Case Else
Msg = "There is nothing on the Clipboard ."
End Select
MsgBox Msg 'displays information.
End Sub

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.