2011-06-23 46 views
0

你们大多数人都经历过,开发一个控制台应用程序是一样简单:移植控制台基于UI到GUI?

void mainloop(){ 
    while (1){ 
     giveInstructions(); 
     getInput(); 
     if (!process()) break; 
     printOutput(); 
    } 
} 

int main(){ 
    mainloop(); 
    return 0; 
} 

然而,在GUI就成为一个问题。

我们仍然可以giveInstructions()process()printOutput(),但getInput()是行不通的,因为它依赖于一个事件,通常是按一下按钮或按下按键。

如何以最少的代码更改将控制台应用程序移植到gui应用程序? (最好不要改变main方法,并尽可能少地改变mainloop函数)

注意:我不太舒服的线程呢。

+0

这个问题并没有写出很多意义。如果您想使用控制台应用程序,请使用控制台应用程序。 –

+0

您可能使用控制台应用程序进行原型制作,但您的真实应用程序是GUI应用程序,但您不一定要重写'getInput',因为它可能很长很丑。 – Pwnna

+0

在你心中,什么是控制台应用程序和GUI应用程序之间的分界线? –

回答

1

由于没有提供特定的语言,我将在C#中展示一个示例,您将可以使用与简单GUI一样的控制台应用程序使用相同的代码。

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      //using form-editor, double-click buttons or use the following 
      btnInput.Click += new EventHandler(btnInput_Click); 
      btnContinue.Click += new EventHandler(btnContinue_Click); 
      giveInstructions(); 
     } 

     private void giveInstructions() 
     { 
      txtInfo.Text = ""; 
      txtInput.Text = ""; 
      //display instructions to multi-line textbox 
     } 

     private void btnInput_Click(object sender, EventArgs e) 
     { 
      //or you can just add another button for exit. 
      if (txtInput.Text == "expected value for exit") 
      { 
       Application.Exit(); 
      } 
      else 
      { 
       getInput(); 
      } 
     } 

     private void getInput() 
     { 
      string strInput = txtInput.Text; 
      //do stuff 

      printOutput(); 
     } 

     private void printOutput() 
     { 
      //display output to multi-line textbox 
     } 

     private void btnContinue_Click(object sender, EventArgs e) 
     { 
      giveInstructions(); 
     } 
    }