*画像を使ったフォーム [#a97bce14] フォームには画像コントロールがありますがそのほかのコントロールとは違ってデータフィールドに関連付けて表示できません。 #contents **テーブルデザイン [#s15e5452] |フィールド|種類|| |ID|INTEGER|自動| |NAME|VARCHAR|| |INFO|VARCHAR|| |IMAGE|LONGVARBINARY|| **ダイアログ [#n9d7f5c4] &ref(dform.png,nolink); -NAME -ID 読み込み専用 -INFO -IMAGE 画像 -[[削除>#wf038348]] 現在のレコードを削除 -[[<>#q0e3221d]] 前のレコードを表示 -[[>>#h1c20e2b]] 次のレコードを表示 -[[更新>#y85a31c0]] 現在のレコードの変更を更新 -[[変更>#f6640388]] 画像の変更 -[[新規>#pe1706ce]] 新規レコード -[[リロード>#l4c94e20]] 再読み込み -[[保存>#s1c486e4]] -閉じる #code(ob){{ Dim oDialog As Object Dim oDBConnection As Object Dim oRowSet As Object Const sSourceName = "icons" Const sTableName = "Images" Dim nID As Long, nNAME As Long Dim nINFO As Long, nIMAGE As Long }} ダイアログの表示およびデータソースへの接続、切断 #code(ob){{ ' main and show dialog Sub Main sLibName = "Standard" sDialogName = "Dialog1" DialogLibraries.loadLibrary(sLibName) oDialog = CreateUnoDialog( _ DialogLibraries.Standard.getByName(sDialogName)) oDialog.getControl("BTN_PREV").Model.Enabled = False oDialog.getControl("BTN_NEXT").Model.Enabled = False oDialog.getControl("IMG_IMAGE").Model.ScaleImage = False vaMIDDLE = com.sun.star.style.VerticalAlignment.MIDDLE oDialog.getControl("L_NAME").Model.VerticalAlign = vaMIDDLE oDialog.getControl("L_ID").Model.VerticalAlign = vaMIDDLE oDialog.getControl("L_INFO").Model.VerticalAlign = vaMIDDLE oDialog.getControl("L_IMAGE").Model.VerticalAlign = vaMIDDLE Connect oDialog.execute() oDialog.dispose() DisConnect End Sub }} **接続 [#ra9c7be0] 行セットを登録されたデータソースに接続。 #code(ob){{ ' connect to datasource Sub Connect oDBCtx = CreateUnoService( _ "com.sun.star.sdb.DatabaseContext") If oDBCtx.hasByName(sSourceName) Then ' connect to the data source oDataSource = oDBCtx.getRegisteredObject(sSourceName) oDBConnection = oDataSource.getConnection("","") ' query data oRowSet = CreateUnoService("com.sun.star.sdb.RowSet") oRowSet.ActiveConnection = oDBConnection oRowSet.CommandType = com.sun.star.sdb.CommandType.COMMAND oRowSet.Command = "SELECT * FROM """ & sTableName & """" oRowSet.execute() ' find column ids nID = oRowSet.findColumn("ID") nNAME = oRowSet.findColumn("NAME") nINFO = oRowSet.findColumn("INFO") nIMAGE = oRowSet.findColumn("IMAGE") 'msgbox "Connected." oDialog.getControl("BTN_NEXT").Model.Enabled = True Else msgbox "No Data Source." End If End Sub }} **切断 [#yacf89f8] 接続を閉じます。 #code(ob){{ ' disconnect from the datasource Sub DisConnect oDBConnection.close() oDBConnection.dispose() End Sub }} **データの表示 [#ifee2e2e] ID、NAME、INFO はそれぞれの型で表示。画像データは getBytes メソッドでは配列データとして取得できるため Pipe に書き込み、そこからグラフィックを作成、画像コントロールに設定。 #code(ob){{ ' set data to the dialog fields Sub ShowData ' get current data and set the datas oDialog.getControl("TXT_ID")._ setText(oRowSet.getInt(nID)) oDialog.getControl("TXT_NAME")._ setText(oRowSet.getString(nNAME)) oDialog.getControl("TXT_INFO")._ setText(oRowSet.getString(nINFO)) vData = oRowSet.getBytes(nIMAGE) ' make a new pipe and write byte to it oPipe = CreateUnoService("com.sun.star.io.Pipe") oPipe.writeBytes(vData) oPipe.flush() oPipe.closeOutput() Dim aImgDesc(0) As New com.sun.star.beans.PropertyValue aImgDesc(0).Name = "InputStream" aImgDesc(0).Value = oPipe ' make its graphics oGP = CreateUnoService("com.sun.star.graphic.GraphicProvider") oGraphic = oGP.queryGraphic(aImgDesc) oDialog.getControl("IMG_IMAGE").Model.Graphic = oGraphic oPipe.closeInput() End Sub }} #code(ob){{ ' change buttons state Sub SetButtonState If oRowSet.isFirst() Then oDialog.getControl("BTN_PREV").Model.Enabled = False Else oDialog.getControl("BTN_PREV").Model.Enabled = True End If If oRowSet.isLast() Then oDialog.getControl("BTN_NEXT").Model.Enabled = False Else oDialog.getControl("BTN_NEXT").Model.Enabled = True End If End Sub }} **前のレコード [#q0e3221d] previous メソッドで前のレコードへ移動。 #code(ob){{ ' show previous record Sub PreviousRecord If NOT oRowSet.isFirst() Then oRowSet.previous() ShowData SetButtonState End If End Sub }} **次のレコード [#h1c20e2b] next メソッドで次のレコードへ移動。 #code(ob){{ ' show next record Sub NextRecord If NOT oRowSet.isLast() Then oRowSet.next() ShowData SetButtonState End If End Sub }} **新規追加 [#pe1706ce] 各フィールドを空に変更。 #code(ob){{ ' add new record Sub AddNew ChangeImage oDialog.getControl("TXT_ID")._ setText("") oDialog.getControl("TXT_NAME")._ setText("") oDialog.getControl("TXT_INFO")._ setText("") End Sub }} **更新 [#y85a31c0] データの更新ををデータソースに適用。ID フィールドは読み込み専用にしておき自動入力。 新規レコードの登録は moveToInsertRow により新規行へ移動、各データを更新した後に行を insertRow メソッドで挿入。既存行の更新は updateRow メソッドで行う。 #code(ob){{ ' update current record Sub Update sNewId = oDialog.getControl("TXT_ID").getText() sNewName = oDialog.getControl("TXT_NAME").getText() sNewInfo = oDialog.getControl("TXT_INFO").getText() vNewImage = oDialog.getControl("IMG_IMAGE").Model.Graphic.getDIB() If sNewName = "" Then ' needs NAME msgbox "Input the NAME field." Exit Sub End If If sNewId = "" Then ' move to new row oRowSet.moveToInsertRow() ' set data oRowSet.updateString(nNAME,sNewName) oRowSet.updateString(nINFO,sNewInfo) oRowSet.updateBytes(nIMAGE,vNewImage) ' insert new row oRowSet.insertRow() ShowData SetButtonState Else ' update existing data oRowSet.updateString(nNAME,sNewName) oRowSet.updateString(nINFO,sNewInfo) oRowSet.updateBytes(nIMAGE,vNewImage) oRowSet.updateRow() End If End Sub }} **削除 [#wf038348] レコードの削除は行を deleteRow メソッドで削除。 #code(ob){{ ' delete current record Sub Remove oRowSet.deleteRow() PreviousRecord End Sub }} **再読み込み [#l4c94e20] #code(ob){{ ' reload currentvrecord Sub ReLoad() ShowData SetButtonState End Sub }} **画像の変更 [#f6640388] 画像コントロールへ指定した画像を表示。 #code(ob){{ ' change the image data Sub ChangeImage ' get file url oFP = CreateUnoService("com.sun.star.ui.dialogs.FilePicker") If oFP.execute() = 0 Then Exit Sub sFiles = oFP.getFiles() sImgURL = sFiles(0) 'oDialog.getControl("IMG_IMAGE").Model.ImageURL = sImgURL 'mri oDialog.getControl("IMG_IMAGE") oSFA = CreateUnoService("com.sun.star.ucb.SimpleFileAccess") oImgInput = oSFA.openFileRead(sImgURL) Dim aImgDesc(0) As New com.sun.star.beans.PropertyValue aImgDesc(0).Name = "InputStream" aImgDesc(0).Value = oImgInput oGP = CreateUnoService("com.sun.star.graphic.GraphicProvider") oGraphic = oGP.queryGraphic(aImgDesc) oDialog.getControl("IMG_IMAGE").Model.Graphic = oGraphic oImgInput.closeInput() End Sub }} **画像の保存 [#s1c486e4] 表示中の画像をファイルとしてローカルへ保存。 #code(ob){{ ' store the current image of the current record Sub StoreImage oFP = CreateUnoService("com.sun.star.ui.dialogs.FilePicker") oFP.initialize(Array( _ com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_SIMPLE)) If oFP.execute() = 0 Then Exit Sub sFiles = oFP.getFiles() sImgURL = sFiles(0) oSFA = CreateUnoService("com.sun.star.ucb.SimpleFileAccess") oOutput = oSFA.openFileWrite(sImgURL) vDIB = oDialog.getControl("IMG_IMAGE").Model.Graphic.getDIB() oOutput.writeBytes(vDIB) oOutput.closeOutput() End Sub }} |