▼Adobe AIRメモ▼
コピー&ペースト
コピー&ペーストを行うプログラムを作成する。
ソースコード
ClipboardEx.as package { import flash.desktop.*; import flash.display.*; import flash.events.*; import flash.text.*; //コピー&ペースト public class ClipboardEx extends Sprite { private var textField:TextField; //テキストフィールド private var btnCopy:CustomButton; //コピーボタン private var btnPaste:CustomButton;//貼り付けボタン //コンストラクタ public function ClipboardEx() { //テキストフィールドの生成 textField =new TextField(); textField.text ="ClipboardEx"; textField.x =5; textField.y =5; textField.width =230; textField.height =210; textField.background =true; textField.backgroundColor=0xFFFFFF; textField.type =TextFieldType.INPUT; textField.multiline =true; addChild(textField); //コピーボタンの生成 btnCopy=new CustomButton("コピー"); btnCopy.x=5; btnCopy.y=220; btnCopy.addEventListener(MouseEvent.CLICK,onClick); addChild(btnCopy); //貼り付けボタンの生成 btnPaste=new CustomButton("貼り付け"); btnPaste.x=btnCopy.x+btnCopy.width+5; btnPaste.y=220; btnPaste.addEventListener(MouseEvent.CLICK,onClick); addChild(btnPaste); } //クリックイベントの処理 private function onClick(evt:Event):void { var text:String; //クリップボードオブジェクトの取得 var cb:Clipboard=Clipboard.generalClipboard; //コピーボタンの処理 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); } } } } }
CustomButton.as package { import flash.display.*; import flash.filters.*; import flash.text.*; //カスタムボタン public class CustomButton extends SimpleButton { //コンストラクタ public function CustomButton(label:String="",fontSize:uint=12) { //ボタンスプライト var downSprite:Sprite=makeButtonSprite( label,fontSize,0x000000,0xdddddd); var upSprite :Sprite=makeButtonSprite( label,fontSize,0xdddddd,0x000000); var hitSprite :Sprite=makeButtonSprite( label,fontSize,0x000000,0x000000); //状態 downState =downSprite;//ダウン overState =upSprite; //オーバー upState =upSprite; //アップ hitTestState=hitSprite; //当たり判定 //手アイコンを指定 useHandCursor=true; } //ボタンスプライトの生成 private function makeButtonSprite(text:String,fontSize:uint, highlightColor:uint,shadowColor:uint):Sprite { //スプライト var sp:Sprite=new Sprite(); //ラベルの指定 var label:TextField=makeLabel(text,fontSize); //描画 sp.graphics.beginFill(0xdddddd); sp.graphics.drawRect(0,0, label.textWidth+6,label.textHeight+6); sp.graphics.endFill(); sp.addChild(label); //フィルタの指定 var filter:BevelFilter=new BevelFilter(); filter.blurX=2; filter.blurY=2; filter.distance=1; filter.highlightColor=highlightColor; filter.shadowColor=shadowColor; var myFilters:Array=new Array(); myFilters.push(filter); sp.filters=myFilters; return sp; } //ラベルの生成 private function makeLabel(text:String,fontSize:uint):TextField { var i:uint; var label:TextField=new TextField(); label.autoSize=TextFieldAutoSize.LEFT; label.selectable=false; var format:TextFormat=new TextFormat(); format.font="_等幅"; label.text="■"; for (i=fontSize+10;i>=1;i--) { format.size=i; label.setTextFormat(format); if (label.textWidth<=fontSize) break; } label.text=text; label.setTextFormat(format); label.x=3-(label.width -label.textWidth)/2; label.y=3-(label.height-label.textHeight)/2; return label; } } }
−戻る−