代码之家  ›  专栏  ›  技术社区  ›  Thorin Oakenshield

如何在C中锁定/解锁windows应用程序窗体#

  •  2
  • Thorin Oakenshield  · 技术社区  · 14 年前

    如果进程正在运行,则所有控件都应处于禁用状态

    现在我正在使用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);
    

    但它不能正常工作。

    提前谢谢

    4 回复  |  直到 14 年前
        1
  •  14
  •   Jürgen Steinblock    14 年前

    你说锁是什么意思?

    如果要防止用户输入,可以设置

    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;
        }
    
    
    }
    
        2
  •  4
  •   leppie    14 年前
    Form.Enabled = false;
    

    不起作用?

        3
  •  2
  •   Franci Penov    14 年前

    它所有的孩子都是残疾人。您可以在场景中使用它,方法是将所有控件放置在ContainerControl父级中,并将其Enabled设置为false。

    事实上,你已经有这样一个容器本体-你的形式。

        4
  •  2
  •   Catalin DICU    14 年前

    this.Enable=假; 线程。睡眠(5000); this.Enable=真;

    BackgroundWorker .

    一个又快又脏的解决办法就是打电话 Application.DoEvents() 就在启用窗体之前。

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