2015-02-11 44 views
1

如何創建允許換行的文本框(通過按Enter鍵)並將其複製到字符串?對話框中的多行文本框

我正在嘗試製作一個類似於Digital Micrograph中Image Info文本框的對話框。此文本框將用於創建在最前面的圖像上創建的文本註釋。

DLGCreateTextBox可以創建一個多行文本框,但它似乎不允許換行符。

我搜索了DM ScriptingTU Graz script database頁面,但沒有看到任何使用這種文本框的內容。

回答

0

不幸的是,對話框文本框並不是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") 
}