画像を使ったフォーム 
フォームには画像コントロールがありますがそのほかのコントロールとは違ってデータフィールドに関連付けて表示できません。
テーブルデザイン 
フィールド | 種類 | |
ID | INTEGER | 自動 |
NAME | VARCHAR | |
INFO | VARCHAR | |
IMAGE | LONGVARBINARY | |
ダイアログ 

- NAME
- ID 読み込み専用
- INFO
- IMAGE 画像
- 削除 現在のレコードを削除
- < 前のレコードを表示
- > 次のレコードを表示
- 更新 現在のレコードの変更を更新
- 変更 画像の変更
- 新規 新規レコード
- リロード 再読み込み
- 保存
- 閉じる
0
1
2
3
4
5
6
7
| | 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
|
ダイアログの表示およびデータソースへの接続、切断
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| | ' 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
|
接続 
行セットを登録されたデータソースに接続。
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| | ' 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
|
切断 
接続を閉じます。
0
1
2
3
4
| | ' disconnect from the datasource
Sub DisConnect
oDBConnection.close()
oDBConnection.dispose()
End Sub
|
データの表示 
ID、NAME、INFO はそれぞれの型で表示。画像データは getBytes メソッドでは配列データとして取得できるため Pipe に書き込み、そこからグラフィックを作成、画像コントロールに設定。
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| | ' 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
|
0
1
2
3
4
5
6
7
8
9
10
11
12
| | ' 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
|
前のレコード 
previous メソッドで前のレコードへ移動。
0
1
2
3
4
5
6
7
| | ' show previous record
Sub PreviousRecord
If NOT oRowSet.isFirst() Then
oRowSet.previous()
ShowData
SetButtonState
End If
End Sub
|
次のレコード 
next メソッドで次のレコードへ移動。
0
1
2
3
4
5
6
7
| | ' show next record
Sub NextRecord
If NOT oRowSet.isLast() Then
oRowSet.next()
ShowData
SetButtonState
End If
End Sub
|
新規追加 
各フィールドを空に変更。
0
1
2
3
4
5
6
7
8
9
| | ' add new record
Sub AddNew
ChangeImage
oDialog.getControl("TXT_ID")._
setText("")
oDialog.getControl("TXT_NAME")._
setText("")
oDialog.getControl("TXT_INFO")._
setText("")
End Sub
|
更新 
データの更新ををデータソースに適用。ID フィールドは読み込み専用にしておき自動入力。
新規レコードの登録は moveToInsertRow により新規行へ移動、各データを更新した後に行を insertRow メソッドで挿入。既存行の更新は updateRow メソッドで行う。
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| | ' 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
|
削除 
レコードの削除は行を deleteRow メソッドで削除。
0
1
2
3
4
| | ' delete current record
Sub Remove
oRowSet.deleteRow()
PreviousRecord
End Sub
|
再読み込み 
0
1
2
3
4
| | ' reload currentvrecord
Sub ReLoad()
ShowData
SetButtonState
End Sub
|
画像の変更 
画像コントロールへ指定した画像を表示。
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| | ' 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
|
画像の保存 
表示中の画像をファイルとしてローカルへ保存。
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| | ' 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
|