2014-09-19 69 views
0

我试图从C#中的控制台应用程序显示WinForm(输入框),并等待用户关闭窗体。输入框在打开时处于活动状态并处于活动状态对我来说很重要。 ShowDialog()在我的情况下不起作用,因为在某些情况下它不会显示为活动表单。所以我想改变我的代码并使用Show()。通过这种方式,我可以手动查找表单是否处于活动状态,如果不是自己激活它。用ShowDialog()。我的代码停止了,并且在关闭之前我什么也做不了。从C#控制台应用程序显示WinFrm并等待关闭

以下是我的代码。它显示输入框,但它被冻结。请问我做错了什么?清除“inputBox.Show();”之后的while循环没有做任何事情,但如果我可以设法运行循环,并且输入框不冻结,我会自己整理其余部分。我感谢您的帮助。

public static string mInputBox(string strPrompt, string strTitle, string strDefaultResponse) 
    { 
     string strResponse = null; 
     Form inputBox = new Form(); 
     inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; 
     inputBox.ClientSize = new Size(500, 85); 
     inputBox.Text = strTitle; 
     inputBox.StartPosition = System.Windows.Forms.FormStartPosition.Manual; 
     inputBox.Left = (Screen.PrimaryScreen.Bounds.Size.Width/2) - (inputBox.ClientSize.Width/2); 
     inputBox.Top = (Screen.PrimaryScreen.Bounds.Size.Height/2) - (inputBox.ClientSize.Height/2); 

     Label lblPrompt = new Label(); 
     lblPrompt.Text = strPrompt; 
     inputBox.Controls.Add(lblPrompt); 

     TextBox textBox = new TextBox(); 
     textBox.Text = strDefaultResponse; 
     inputBox.Controls.Add(textBox); 

     Button okButton = new Button(); 
     okButton.Text = "&OK"; 
     inputBox.Controls.Add(okButton); 

     Button cancelButton = new Button(); 
     cancelButton.Text = "&Cancel"; 
     inputBox.Controls.Add(cancelButton); 

     okButton.Click += (sender, e) => 
     { 
      strResponse = textBox.Text; 
      inputBox.Close(); 
     }; 

     cancelButton.Click += (sender, e) => 
     { 
      inputBox.Close(); 
     }; 
     inputBox.AcceptButton = okButton; 
     inputBox.CancelButton = cancelButton; 

     SetWindowPos(inputBox.Handle, HWND_TOPMOST, inputBox.Left, inputBox.Top, inputBox.Width, inputBox.Height, NOACTIVATE); 

     inputBox.Show(); 

     while {true} 
      Thread.Sleep(100); 

     Application.DoEvents(); 
     return strResponse; 
    } 
+0

'而{TRUE} Thread.sleep代码(100);'只会无限循环锁定您的应用程序。实际上,它不会编译......那些应该是括号,而不是花括号。 – 2014-09-19 03:05:44

+0

是真的,那么我怎样才能使它不锁定工作?我应该使用另一个线程吗?怎么样? – NESHOM 2014-09-19 03:08:42

回答

1

我不知道为什么你正在服用的这条路,我敢肯定有更好的方法来做到这一点(解释一个在结束)。然而,为了使你的代码运行,你应该添加Application.DoEvents()您的循环内

代码应该是这样的:

 var formActive = true; 
     inputBox.FormClosed += (s, e) => formActive = false; 
     inputBox.Show(); 
     inputBox.TopMost = true; 
     inputBox.Activate(); 

     while (formActive) 
     { 
      Thread.Sleep(10); 
      Application.DoEvents(); 
     } 

,整个方法是:

public static string mInputBox(string strPrompt, string strTitle, string strDefaultResponse) 
    { 
     string strResponse = null; 
     Form inputBox = new Form(); 
     inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; 
     inputBox.ClientSize = new Size(500, 85); 
     inputBox.Text = strTitle; 
     inputBox.StartPosition = System.Windows.Forms.FormStartPosition.Manual; 
     inputBox.Left = (Screen.PrimaryScreen.Bounds.Size.Width/2) - (inputBox.ClientSize.Width/2); 
     inputBox.Top = (Screen.PrimaryScreen.Bounds.Size.Height/2) - (inputBox.ClientSize.Height/2); 

     Label lblPrompt = new Label(); 
     lblPrompt.Text = strPrompt; 
     inputBox.Controls.Add(lblPrompt); 

     TextBox textBox = new TextBox(); 
     textBox.Text = strDefaultResponse; 
     inputBox.Controls.Add(textBox); 

     Button okButton = new Button(); 
     okButton.Text = "&OK"; 
     inputBox.Controls.Add(okButton); 

     Button cancelButton = new Button(); 
     cancelButton.Text = "&Cancel"; 
     inputBox.Controls.Add(cancelButton); 

     okButton.Click += (sender, e) => 
     { 
      strResponse = textBox.Text; 
      inputBox.Close(); 
     }; 

     cancelButton.Click += (sender, e) => 
     { 
      inputBox.Close(); 
     }; 
     inputBox.AcceptButton = okButton; 
     inputBox.CancelButton = cancelButton; 


     var formActive = true; 
     inputBox.FormClosed += (s, e) => formActive = false; 
     inputBox.Show(); 
     inputBox.TopMost = true; 
     inputBox.Activate(); 

     while (formActive) 
     { 
      Thread.Sleep(10); 
      Application.DoEvents(); 
     } 

     return strResponse; 
    } 

但我认为这将是一个更好的想法,从Form派生并创建一个InputBox并设置TopMost并致电ActivateOnLoad创建您所需的内容,然后简单地拨打ShowDialog,是这样的:

class Inputbox : Form 
     { 

      protected override void OnLoad(EventArgs e) 
      { 
       base.OnLoad(e); 

       TopMost = true; 
       Activate(); 
      } 
     } 

,并更好地把UI代码的InputBox类作为整个代码和用法是这样的:

class Inputbox : Form 
{ 
    public string Response { get; set; } 

    public Inputbox(string strTitle, string strPrompt, string strDefaultResponse) 
    { 
     //add UI and Controls here 

     FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; 
     ClientSize = new Size(500, 85); 
     Text = strTitle; 
     StartPosition = System.Windows.Forms.FormStartPosition.Manual; 
     Left = (Screen.PrimaryScreen.Bounds.Size.Width/2) - (ClientSize.Width/2); 
     Top = (Screen.PrimaryScreen.Bounds.Size.Height/2) - (ClientSize.Height/2); 

     Label lblPrompt = new Label(); 
     lblPrompt.Text = strPrompt; 
     Controls.Add(lblPrompt); 

     TextBox textBox = new TextBox(); 
     textBox.Text = strDefaultResponse; 
     Controls.Add(textBox); 

     Button okButton = new Button(); 
     okButton.Text = "&OK"; 
     Controls.Add(okButton); 

     Button cancelButton = new Button(); 
     cancelButton.Text = "&Cancel"; 
     Controls.Add(cancelButton); 

     okButton.Click += (sender, e) => 
     { 
      Response = textBox.Text; 
      Close(); 
     }; 

     cancelButton.Click += (sender, e) => 
     { 
      Close(); 
     }; 
     AcceptButton = okButton; 
     CancelButton = cancelButton; 
    } 

    protected override void OnLoad(EventArgs e) 
    { 
     base.OnLoad(e); 

     TopMost = true; 
     Activate(); 
    } 
} 

public static string mInputBox(string strPrompt, string strTitle, string strDefaultResponse) 
{ 
    string strResponse = null; 
    Inputbox inputBox = new Inputbox(strPrompt,strTitle,strDefaultResponse); 

    inputBox.ShowDialog(); 

    return inputBox.Response; 
} 
+0

太好了,非常感谢! – NESHOM 2014-09-19 15:32:25

0

你需要运行一个消息循环:

Application.Run(inputBox); 
+0

我在我的代码中使用HotKeyManager类,它使用Application.Run(new MessageWindow());.所以当我使用Application.Run(inputBox);它给了我这个错误:在单个线程上启动第二个消息循环不是一个有效的操作。 – NESHOM 2014-09-19 03:07:35

+0

@ M0HS3N:然后你想'ShowDialog()'。 – SLaks 2014-09-19 14:14:05

相关问题