[js]javascript與剪貼板交互

[js]JavaScript與剪貼板交互

1.怎樣操作剪貼板,從而實現復制、剪切與粘貼?同時判斷剪貼板里邊的數據是否是文本?
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 the
// window.

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

2.可以使用javascript獲得windows剪貼板里的字符串嗎?
比如在網頁中實現點擊一個文本框 就把剪貼板里的字符粘貼進去

當然可以



3.javascript和剪貼板的交互

一般可以這樣將id為‘objid’的對象的內容copy到剪貼板

var rng = document.body.createTextRange();
rng.moveToElementText(document.getElementById(“objid”));
rng.scrollIntoView();
rng.select();
rng.execCommand(“Copy”);
rng.collapse(false);
setTimeout(“window.status=””,1800)

也可以用rng.execCommand(“Past”);將剪貼板的內容粘到光標當前位置。

內容參見msdn 的textRange對象。

不過,copy到剪貼板的都是不帶html標簽的,所有html標簽都將被過濾。

4.window.clipboardData.getData(“Text”) //可以獲得剪貼版的文字
window.clipboardData.setData(“Text”,”你的內容”) //向剪貼板里寫文本信息

5.怎么判斷剪貼板中的數據是否為字符串而不是圖片或別的信息?

Private Sub Command1_Click()
If Clipboard.GetFormat(vbCFText) Or Clipboard.GetFormat(vbCFRTF) Then
MsgBox “ok”
End If
End Sub

6.請問如何判斷剪貼板中不為空?

一、

Eg
判斷windows剪貼板里是否為空,沒有則讀取圖片到Image中
uses clipbrd;

if ClipBoard.HasFormat(CF_Picture) then
Image1.Picture.Assign(ClipBoard);
二、

uses Clipbrd;

procedure TForm1.Button1Click(Sender: TObject);
begin
if Clipboard.FormatCount { TODO : 空 };
end;

7.怎樣確定剪貼板中的數據是否為圖象?

GetFormat 方法示例
本示例使用 GetFormat 方法確定 Clipboard 對象上數據的格式。要檢驗此示例,可將本例代碼粘貼到一個窗體的聲明部分,然后按 F5 鍵并單擊該窗體。

Private Sub Form_Click ()
‘ 定義位圖各種格式。
Dim ClpFmt, Msg ‘ 聲明變量。
On Error Resume Next ‘ 設置錯誤處理。
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 ‘ 顯示信息。
End Sub

? 版權聲明
THE END
喜歡就支持一下吧
點贊10 分享