package {
import flash.desktop.*;
import flash.display.*;
import flash.events.*;
import flash.text.*;
//コピー&ペースト
[SWF(width=240, height=240, backgroundColor=0xFFFFFF)]
public class ClipboardEx extends Sprite {
private var textField:TextField;//テキストフィールド
private var btnCopy :TextField;//コピーボタン
private var btnPaste :TextField;//貼り付けボタン
//コンストラクタ
public function ClipboardEx() {
//ベースの追加
var base:Sprite = new Sprite();
base.graphics.beginFill(0xffffff);
base.graphics.drawRect(0, 0, 240, 240);
base.graphics.endFill();
addChild(base);
//テキストフィールドの追加
textField = addTextField(base, "" ,10 ,10);
//ボタンの追加
btnCopy = addButton(base, "コピー", 10, 36);
btnPaste = addButton(base, "貼り付け", 70, 36);
}
//テキストフィールドの追加
private function addTextField(doc:DisplayObjectContainer,
text:String, x:int, y:int):TextField {
var textField:TextField = new TextField();
doc.addChild(textField);
textField.text = text;
textField.x = x;
textField.y = y;
textField.width = 200;
textField.height = 16;
textField.selectable = true;
textField.border = true;
textField.type = TextFieldType.INPUT;
return textField;
}
//ボタンの追加
private function addButton(doc:DisplayObjectContainer,
text:String, x:int, y:int):TextField {
var button:TextField = new TextField();
doc.addChild(button);
button.text = text;
button.x = x;
button.y = y;
button.autoSize = TextFieldAutoSize.LEFT;
button.selectable = false;
button.border = true;
button.background = true;
button.backgroundColor = 0xdddddd;
button.addEventListener(MouseEvent.CLICK, onClick);
return button;
}
//クリックイベントの処理
private function onClick(evt:Event):void {
//クリップボードオブジェクトの取得
var cb:Clipboard = Clipboard.generalClipboard;
var text:String;
//コピー
if (evt.target == btnCopy) {
//選択テキストの取得
text = textField.text.substr(
textField.selectionBeginIndex,
textField.selectionEndIndex);
//クリップボードのクリア
cb.clear();
//クリップボードへのデータ指定
cb.setData(
ClipboardFormats.TEXT_FORMAT,
text, false);
}
//貼り付け
if (evt.target == btnPaste) {
//ファイル形式のチェック
if (Clipboard.generalClipboard.hasFormat(
ClipboardFormats.TEXT_FORMAT)) {
//クリップボードからのデータ取得
text = String(cb.getData(ClipboardFormats.TEXT_FORMAT));
//選択テキストとの置換
textField.text =
textField.text.substr(0, textField.selectionBeginIndex) +
text +
textField.text.substr(textField.selectionEndIndex);
}
}
}
}
}
|