不幸的是,对话框文本框并不是DigitalMicrograph脚本对话框中最方便的对话框项目。有以下几点需要知道:
- 在显示对话框时,文本只能设置为(或从中读取)该项目。
- 文本框是由宽度 & 高度而且通过一个长度参数指定。
- 只要文本框中的文本长度小于长度,任何return-keystroke都将作为字符存储。
- 但是,如果达到了限制,则会将return-keystroke传递给对话框。如果它是模态对话框(即由
pose()
显示),则返回击键将立即作为'好'并且关闭对话!
考虑到这一点,以下示例脚本将创建一个可能有换行符(返回)的文本框字段。但是,只有通过使用附加到按钮上的“复制”动作,才能在关闭对话框后访问变量中的文本“存储”。
class myDLG : UIFrame
{
string textBuffer
tagGroup CreateDLGSelf(object self)
{
number width = 20 // line width of box
number height = 10 // number of lines
number length = 500 // maximum length (characters) of content
TagGroup textBox = DLGCreateTextBox(width, height, length)
textBox.DLGIdentifier("textBox")
TagGroup StoreButton = DLGCreatePushButton("Store" , "StoreText")
TagGroup DLG, DLGitems
DLG = DLGCreateDialog("TestBox", DLGitems)
DLGitems.DLGAddElement(textBox)
DLGitems.DLGAddElement(StoreButton)
return DLG
}
void StoreText(object self)
{
textBuffer = self.GetTextElementData("textBox")
}
string GetBufferedText(object self)
{
return textBuffer
}
object Init(object self)
{
return self.Super.Init(self.CreateDLGSelf())
}
}
// Main script
{
object testDLG = Alloc(myDLG).Init()
if (testDLG.pose())
OKDialog("Text:" + testDLG.GetBufferedText())
}
如果对话框是无模式(即经由display()
显示)中的字段可被直接访问。以下脚本演示了这一点 - 以及这样的对话框如何在图像上创建注释标记。
class annoTool : UIFrame
{
tagGroup CreateToolDlg(object self)
{
number width = 50 // line width of box
number height = 5 // number of lines
number length = 300 // maximum length (characters) of content.
TagGroup textBox = DLGCreateTextBox(width, height, length)
textBox.DLGIdentifier("textBox")
TagGroup CreatAnnoButton = DLGCreatePushButton("Copy to Image" , "CreateAnno")
TagGroup InitTextButton = DLGCreatePushButton("Intitialze" , "InitTextField")
TagGroup DLG, DLGitems
DLG = DLGCreateDialog("TestBox", DLGitems)
DLGitems.DLGAddElement(textBox)
DLGitems.DLGAddElement(DLGGroupItems(InitTextButton, CreatAnnoButton).DLGTableLayout(2,1,0) )
return DLG
}
void InitTextField(object self)
{
string init = ""
init += "Date:" + GetDate(1) + "\n"
init += "Time:" + GetTime(0) + "\n"
init += "(c) My TEM Institute"
self.SetTextElementData("textBox", init)
}
void CreateAnno(object self)
{
image front
string textBuffer = self.GetTextElementData("textBox")
if (GetFrontImage(front))
{
CreateTextAnnotation(front, 0, 0, textBuffer)
}
}
object Init(object self)
{
return self.Super.Init(self.CreateToolDlg())
}
}
// Main script
{
Alloc(annoTool).Init().display("Annotation Creater")
}