[英]How to lock/Unlock a windows application form in C#
如果正在執行特定過程,我需要鎖定整個表單。
我的表單包含許多控件,如按鈕,組合框。 如果進程正在運行,則所有控件都應處於禁用狀態
現在我使用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;
}
}
Form.Enabled = false;
不工作?
當控件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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.