如何锁定/解锁C#中的Windows应用程序窗体#

问题描述:

如果正在执行特定进程,我需要锁定整个窗体。如何锁定/解锁C#中的Windows应用程序窗体#

我的表单包含许多控件,如按钮,组合框。 所有控件应该是禁止状态,如果正在运行的进程

现在i'n使用从user32.dll中

[DllImport("user32.dll")] 
    public static extern IntPtr FindWindow(String sClassName, String sAppName); 

    [DllImport("user32.dll")] 
    public static extern bool EnableWindow(IntPtr hwnd, bool bEnable); 

但它不能正常工作的两种方法。

是否有任何其他的想法提前

你是什么意思与锁?

如果你想阻止使输入的用户可以设置

this.Enabled = false; 

主要形式,这将禁用所有子控件,太。

阻止事件发生的解决方案是实现一个消息过滤器:http://msdn.microsoft.com/en-us/library/system.windows.forms.application.addmessagefilter.aspx并拦截鼠标左键。

// Creates a message filter. 
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] 
public class TestMessageFilter : IMessageFilter 
{ 
    public bool PreFilterMessage(ref Message m) 
    { 
     // Blocks all the messages relating to the left mouse button. 
     if (m.Msg >= 513 && m.Msg <= 515) 
     { 
      Console.WriteLine("Processing the messages : " + m.Msg); 
      return true; 
     } 
     return false; 
    } 
} 


public void SomeMethod() 
{ 

    this.Cursor = Cursors.WaitCursor; 
    this.Enabled = false; 
    Application.AddMessageFilter(new TestMessageFilter(this)); 

    try 
    { 
     Threading.Threat.Sleep(10000); 
    } 
    finally 
    { 
     Application.RemoveMessageFilter(new TestMessageFilter(this)); 
     this.Enabled = true; 
     this.Cursor = Cursors.Default; 
    } 


} 
+0

如果我在(表单)禁用状态中单击一个按钮,该动作发生在表单启用后 – 2010-09-16 04:54:27

+0

这是真的,没有想到,但从我的角度来看,这是一个预期的行为(如果我点击一个按钮并且威胁被阻止,我预计事件将随之而来)。但是如果你真的想,我添加了一个解决方案来拦截左键点击(你可能需要拦截更多的消息) – 2010-09-16 10:11:45

Form.Enabled = false; 

不工作要做到这一点

感谢?

+0

其工作,但如果我在(形式)禁用状态点击一个按钮,该操作将表格后发生启用 – 2010-09-15 10:54:07

+0

@Pramodh:这是不应该的。其他的一定是错的。你做任何其他winapi电话? – leppie 2010-09-15 11:42:10

+0

不...这里是一个示例代码 this.Enable = false; Thread.sleep代码(5000); this.Enable = TRUE; – 2010-09-16 05:04:01

当控制Enabled属性设置为false,与对照所有儿童的互动被禁用。您可以在您的方案中使用它,方法是将所有控件放在ContainerControl父级中,并将其设置为Enabled = false。

事实上,你已经有了这样一个ContainerContol--你的表单。

this.Enable = false; Thread.sleep代码(5000); this.Enable = TRUE;

在GUI线程中进行处理是不好的做法,您应该使用BackgroundWorker

一个快速而脏的修补程序将在启用表单之前调用Application.DoEvents()

this.Enable=false; Thread.Sleep(5000); Application.DoEvents(); this.Enable=true;