在C#開發的WinForm窗體程序開發的時候,常常會使用多線程處理一些比較耗時之類的操做。不過會有一個問題:就是涉及到跨線程操做UI元素。
相信纔開始接觸的人必定會趕上這個問題。git
爲了解決這個問題,能夠經過委託來實現。github
我爲了後期使用更加方便,就將經常使用的幾個控件(TextBox、Label、CheckBox、RadioButton、ProgressBar)UI操做進行了封裝,源碼能夠在github上查看:GitHub倉庫地址。
源碼在github倉庫中的XY.WINUI.ControlEx.InvokeEx文件夾中網絡
在測試項目中有使用的方法,這裏只作簡單介紹。
調用方法在Invokes類中,也能夠使用單獨的類調用。多線程
例如:在窗體中爲tbName的TextBox設置和獲取值測試
//首先,使用命名空間 using XY.WINUI.ControlEx.InvokeEx; //第一種 //設置TextBox的值 Invokes.SetTextBoxText(tbName,"TextBox文本"); //獲取TextBox的值 string value = Invokes.GetTextBoxText(tbName); //第二種 //設置TextBox的值 TextBoxInvoke.SetTextBoxText(tbName,"TextBox文本"); //獲取TextBox的值 string value = TextBoxInvoke.GetTextBoxText(tbName);
GitHub源碼spa
集合類(Invokes.cs)
Invokes.cs線程
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace XY.WINUI.ControlEx.InvokeEx { public class Invokes { #region //Controls #region // TextBox /// <summary> /// 設置文本框文本 /// </summary> /// <param name="Ctrl"></param> /// <param name="Text"></param> public static void SetTextBoxText(TextBox Ctrl, string Text) { TextBoxInvoke.SetTextBoxText(Ctrl, Text); } /// <summary> /// 獲取文本框文本方法 /// </summary> /// <param name="Ctrl"></param> public static string GetTextBoxText(TextBox Ctrl) { return TextBoxInvoke.GetTextBoxText(Ctrl); ; } #endregion #region // Label /// <summary> /// 設置標籤文本 /// </summary> /// <param name="Ctrl"></param> /// <param name="Text"></param> public static void SetLabelText(Label Ctrl, string Text) { LabelInvoke.SetLabelText(Ctrl, Text); } /// <summary> /// 獲取標籤文本方法 /// </summary> /// <param name="Ctrl"></param> public static string GetLabelText(Label Ctrl) { return LabelInvoke.GetLabelText(Ctrl); ; } #endregion #region // RadioButton /// <summary> /// 獲取單選按鈕選中狀態 /// </summary> /// <param name="Ctrl"></param> /// <returns></returns> public static bool GetRadioButtonChecked(RadioButton Ctrl) { return RadioButtonInvoke.GetRadioButtonChecked(Ctrl); } /// <summary> /// 設置單選按鈕選中狀態 /// </summary> /// <param name="Ctrl"></param> /// <param name="Checked"></param> public static void SetRadioButtonChecked(RadioButton Ctrl, bool Checked) { RadioButtonInvoke.SetRadioButtonChecked(Ctrl, Checked); } #endregion #region // CheckBox /// <summary> /// 獲取選擇按鈕選中狀態 /// </summary> /// <param name="Ctrl"></param> /// <returns></returns> public static bool GetCheckBoxChecked(CheckBox Ctrl) { return CheckBoxInvoke.GetCheckBoxChecked(Ctrl); } /// <summary> /// 設置選擇按鈕選中狀態 /// </summary> /// <param name="Ctrl"></param> /// <param name="Checked"></param> public static void SetCheckBoxChecked(CheckBox Ctrl, bool Checked) { CheckBoxInvoke.SetCheckBoxChecked(Ctrl, Checked); } #endregion #region // ProgressBar /// <summary> /// 獲取進度條值 /// </summary> /// <param name="Ctrl"></param> public static int GetProgressBarValue(ProgressBar Ctrl) { return ProgressBarInvoke.GetProgressBarValue(Ctrl); } /// <summary> /// 設置進度條值 /// </summary> /// <param name="Ctrl"></param> /// <param name="Value"></param> public static void SetProgressBarValue(ProgressBar Ctrl, int Value) { ProgressBarInvoke.SetProgressBarValue(Ctrl, Value); } /// <summary> /// 獲取進度條最小值 /// </summary> /// <param name="Ctrl"></param> public static int GetProgressBarMinValue(ProgressBar Ctrl) { return ProgressBarInvoke.GetProgressBarMinValue(Ctrl); } /// <summary> /// 設置進度條最小值 /// </summary> /// <param name="Ctrl"></param> /// <param name="Value"></param> public static void SetProgressBarMinValue(ProgressBar Ctrl, int Value) { ProgressBarInvoke.SetProgressBarMinValue(Ctrl, Value); } /// <summary> /// 獲取進度條最大值 /// </summary> /// <param name="Ctrl"></param> public static int GetProgressBarMaxValue(ProgressBar Ctrl) { return ProgressBarInvoke.GetProgressBarMaxValue(Ctrl); } /// <summary> /// 設置進度條最大值 /// </summary> /// <param name="Ctrl"></param> /// <param name="Value"></param> public static void SetProgressBarMaxValue(ProgressBar Ctrl, int Value) { ProgressBarInvoke.SetProgressBarMaxValue(Ctrl, Value); } #endregion #endregion #region // Common #region // Visible /// <summary> /// 獲取控件是否可見委託 /// </summary> /// <param name="Ctrl"></param> private delegate bool GetControlVisibleDelegate(Control Ctrl); /// <summary> /// 設置控件是否可見委託 /// </summary> /// <param name="Ctrl"></param> /// <param name="Visible"></param> private delegate void SetControlVisibleDelegate(Control Ctrl, bool Visible); /// <summary> /// 獲取控件是否可見方法 /// </summary> /// <param name="Ctrl"></param> private static bool GetControlVisibleFunc(Control Ctrl) { return Ctrl.Visible; } /// <summary> /// 獲取控件是否可見 /// </summary> /// <param name="flag"></param> public static bool GetControlVisible(Control Ctrl) { return (bool)Ctrl.Invoke(new SetControlVisibleDelegate(SetControlVisibleFunc), new object[] { Ctrl }); } /// <summary> /// 設置控件是否可見方法 /// </summary> /// <param name="Ctrl"></param> /// <param name="Visible"></param> private static void SetControlVisibleFunc(Control Ctrl, bool Visible) { Ctrl.Visible = Visible; Ctrl.Refresh(); } /// <summary> /// 設置控件是否可見 /// </summary> /// <param name="Ctrl"></param> /// <param name="Visible"></param> public static void SetControlVisible(Control Ctrl, bool Visible) { Ctrl.Invoke(new SetControlVisibleDelegate(SetControlVisibleFunc), new object[] { Ctrl, Visible }); } #endregion #endregion } }
文本框(TextBox)
TextBoxInvoke.cscode
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace XY.WINUI.ControlEx.InvokeEx { /// <summary> /// TextBoxInvoke /// </summary> public class TextBoxInvoke { /// <summary> /// 獲取文本框文本委託 /// </summary> /// <param name="Ctrl"></param> private delegate string GetTextBoxTextDelegate(TextBox Ctrl); /// <summary> /// 設置文本框文本委託 /// </summary> /// <param name="Ctrl"></param> /// <param name="Text"></param> private delegate void SetTextBoxTextDelegate(TextBox Ctrl, string Text); /// <summary> /// 獲取文本框文本方法 /// </summary> /// <param name="Ctrl"></param> private static string GetTextBoxTextFunc(TextBox Ctrl) { return Ctrl.Text; } /// <summary> /// 獲取文本框文本 /// </summary> /// <param name="Ctrl"></param> /// <returns></returns> public static string GetTextBoxText(TextBox Ctrl) { return (string)Ctrl.Invoke(new GetTextBoxTextDelegate(GetTextBoxTextFunc), new object[] { Ctrl }); } /// <summary> /// 設置文本框文本方法 /// </summary> /// <param name="Ctrl"></param> /// <param name="Text"></param> private static void SetTextBoxTextFunc(TextBox Ctrl, string Text) { Ctrl.Text = Text; Ctrl.Refresh(); } /// <summary> /// 設置文本框文本 /// </summary> /// <param name="Ctrl"></param> /// <param name="Text"></param> public static void SetTextBoxText(TextBox Ctrl, string Text) { Ctrl.Invoke(new SetTextBoxTextDelegate(SetTextBoxTextFunc), new object[] { Ctrl, Text }); } } }
標籤(Label)
LabelInvoke.csorm
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace XY.WINUI.ControlEx.InvokeEx { /// <summary> /// LabelInvoke /// </summary> public class LabelInvoke { /// <summary> /// 獲取標籤文本委託 /// </summary> /// <param name="Ctrl"></param> private delegate string GetLabelTextDelegate(Label Ctrl); /// <summary> /// 設置標籤文本委託 /// </summary> /// <param name="Ctrl"></param> /// <param name="Text"></param> private delegate void SetLabelTextDelegate(Label Ctrl, string Text); /// <summary> /// 獲取標籤文本方法 /// </summary> /// <param name="Ctrl"></param> private static string GetLabelTextFunc(Label Ctrl) { return Ctrl.Text; } /// <summary> /// 獲取標籤文本 /// </summary> /// <param name="Ctrl"></param> /// <returns></returns> public static string GetLabelText(Label Ctrl) { return (string)Ctrl.Invoke(new GetLabelTextDelegate(GetLabelTextFunc), new object[] { Ctrl }); } /// <summary> /// 設置標籤文本方法 /// </summary> /// <param name="Ctrl"></param> /// <param name="Text"></param> private static void SetLabelTextFunc(Label Ctrl, string Text) { Ctrl.Text = Text; Ctrl.Refresh(); } /// <summary> /// 設置標籤文本 /// </summary> /// <param name="Ctrl"></param> /// <param name="Text"></param> public static void SetLabelText(Label Ctrl, string Text) { Ctrl.Invoke(new SetLabelTextDelegate(SetLabelTextFunc), new object[] { Ctrl, Text }); } } }
複選框(CheckBox)
CheckBoxInvoke.csblog
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace XY.WINUI.ControlEx.InvokeEx { /// <summary> /// CheckBoxInvoke /// </summary> public class CheckBoxInvoke { /// <summary> /// 獲取選擇按鈕選中狀態委託 /// </summary> /// <param name="Ctrl"></param> private delegate bool GetCheckBoxCheckedDelegate(CheckBox Ctrl); /// <summary> /// 設置選擇按鈕選中狀態委託 /// </summary> /// <param name="Ctrl"></param> /// <param name="Checked"></param> private delegate void SetCheckBoxCheckedDelegate(CheckBox Ctrl, bool Checked); /// <summary> /// 獲取選擇按鈕選中狀態方法 /// </summary> /// <param name="Ctrl"></param> private static bool GetCheckBoxCheckedFunc(CheckBox Ctrl) { return Ctrl.Checked; } /// <summary> /// 獲取選擇按鈕選中狀態 /// </summary> /// <param name="Ctrl"></param> /// <returns></returns> public static bool GetCheckBoxChecked(CheckBox Ctrl) { return (bool)Ctrl.Invoke(new GetCheckBoxCheckedDelegate(GetCheckBoxCheckedFunc), new object[] { Ctrl }); } /// <summary> /// 設置選擇按鈕選中狀態方法 /// </summary> /// <param name="Ctrl"></param> /// <param name="Checked"></param> private static void SetCheckBoxCheckedFunc(CheckBox Ctrl, bool Checked) { Ctrl.Checked = Checked; Ctrl.Refresh(); } /// <summary> /// 設置選擇按鈕選中狀態 /// </summary> /// <param name="Ctrl"></param> /// <param name="Checked"></param> public static void SetCheckBoxChecked(CheckBox Ctrl, bool Checked) { Ctrl.Invoke(new SetCheckBoxCheckedDelegate(SetCheckBoxCheckedFunc), new object[] { Ctrl, Checked }); } } }
單選按鈕(RadioButton)
RadioButtonInvoke.cs
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace XY.WINUI.ControlEx.InvokeEx { /// <summary> /// RadioButtonInvoke /// </summary> public class RadioButtonInvoke { /// <summary> /// 獲取單選按鈕選中狀態委託 /// </summary> /// <param name="Ctrl"></param> private delegate bool GetRadioButtonCheckedDelegate(RadioButton Ctrl); /// <summary> /// 設置單選按鈕選中狀態委託 /// </summary> /// <param name="Ctrl"></param> /// <param name="Checked"></param> private delegate void SetRadioButtonCheckedDelegate(RadioButton Ctrl, bool Checked); /// <summary> /// 獲取單選按鈕選中狀態方法 /// </summary> /// <param name="Ctrl"></param> private static bool GetRadioButtonCheckedFunc(RadioButton Ctrl) { return Ctrl.Checked; } /// <summary> /// 獲取單選按鈕選中狀態 /// </summary> /// <param name="Ctrl"></param> /// <returns></returns> public static bool GetRadioButtonChecked(RadioButton Ctrl) { return (bool)Ctrl.Invoke(new GetRadioButtonCheckedDelegate(GetRadioButtonCheckedFunc), new object[] { Ctrl }); } /// <summary> /// 設置單選按鈕選中狀態方法 /// </summary> /// <param name="Ctrl"></param> /// <param name="Checked"></param> private static void SetRadioButtonCheckedFunc(RadioButton Ctrl, bool Checked) { Ctrl.Checked = Checked; Ctrl.Refresh(); } /// <summary> /// 設置單選按鈕選中狀態 /// </summary> /// <param name="Ctrl"></param> /// <param name="Checked"></param> public static void SetRadioButtonChecked(RadioButton Ctrl, bool Checked) { Ctrl.Invoke(new SetRadioButtonCheckedDelegate(SetRadioButtonCheckedFunc), new object[] { Ctrl, Checked }); } } }
進度條(ProgressBar)
ProgressBarInvoke.cs
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace XY.WINUI.ControlEx.InvokeEx { /// <summary> /// ProgressBarInvoke /// </summary> public class ProgressBarInvoke { /// <summary> /// 獲取進度條當前值委託 /// </summary> /// <param name="Ctrl"></param> private delegate int GetProgressBarValueDelegate(ProgressBar Ctrl); /// <summary> /// 設置進度條當前值委託 /// </summary> /// <param name="Ctrl"></param> /// <param name="Value"></param> private delegate void SetProgressBarValueDelegate(ProgressBar Ctrl, int Value); /// <summary> /// 獲取進度條最小值委託 /// </summary> /// <param name="Ctrl"></param> private delegate int GetProgressBarMinValueDelegate(ProgressBar Ctrl); /// <summary> /// 設置進度條最小值委託 /// </summary> /// <param name="Ctrl"></param> /// <param name="Value"></param> private delegate void SetProgressBarMinValueDelegate(ProgressBar Ctrl, int Value); /// <summary> /// 獲取進度條最大值委託 /// </summary> /// <param name="Ctrl"></param> private delegate int GetProgressBarMaxValueDelegate(ProgressBar Ctrl); /// <summary> /// 設置進度條最大值委託 /// </summary> /// <param name="Ctrl"></param> /// <param name="Value"></param> private delegate void SetProgressBarMaxValueDelegate(ProgressBar Ctrl, int Value); /// <summary> /// 獲取進度條當前值方法 /// </summary> /// <param name="Ctrl"></param> private static int GetProgressBarValueFunc(ProgressBar Ctrl) { return Ctrl.Value; } /// <summary> /// 獲取進度條當前值 /// </summary> /// <param name="Ctrl"></param> /// <returns></returns> public static int GetProgressBarValue(ProgressBar Ctrl) { return (int)Ctrl.Invoke(new GetProgressBarValueDelegate(GetProgressBarValueFunc), new object[] { Ctrl }); } /// <summary> /// 設置進度條當前值方法 /// </summary> /// <param name="Ctrl"></param> /// <param name="Value"></param> private static void SetProgressBarValueFunc(ProgressBar Ctrl, int Value) { Ctrl.Value = Value; Ctrl.Refresh(); } /// <summary> /// 設置進度條當前值 /// </summary> /// <param name="Ctrl"></param> /// <param name="Value"></param> public static void SetProgressBarValue(ProgressBar Ctrl, int Value) { Ctrl.Invoke(new SetProgressBarValueDelegate(SetProgressBarValueFunc), new object[] { Ctrl, Value }); } /// <summary> /// 獲取進度條最小值方法 /// </summary> /// <param name="Ctrl"></param> private static int GetProgressBarMinValueFunc(ProgressBar Ctrl) { return Ctrl.Minimum; } /// <summary> /// 獲取進度條最小值 /// </summary> /// <param name="Ctrl"></param> /// <returns></returns> public static int GetProgressBarMinValue(ProgressBar Ctrl) { return (int)Ctrl.Invoke(new GetProgressBarValueDelegate(GetProgressBarMinValueFunc), new object[] { Ctrl }); } /// <summary> /// 設置進度條最小值方法 /// </summary> /// <param name="Ctrl"></param> /// <param name="Value"></param> private static void SetProgressBarMinValueFunc(ProgressBar Ctrl, int Value) { Ctrl.Minimum = Value; Ctrl.Refresh(); } /// <summary> /// 設置進度條最小值 /// </summary> /// <param name="Ctrl"></param> /// <param name="Value"></param> public static void SetProgressBarMinValue(ProgressBar Ctrl, int Value) { Ctrl.Invoke(new SetProgressBarValueDelegate(SetProgressBarMinValueFunc), new object[] { Ctrl, Value }); } /// <summary> /// 獲取進度條最大值方法 /// </summary> /// <param name="Ctrl"></param> private static int GetProgressBarMaxValueFunc(ProgressBar Ctrl) { return Ctrl.Maximum; } /// <summary> /// 獲取進度條最大值 /// </summary> /// <param name="Ctrl"></param> /// <returns></returns> public static int GetProgressBarMaxValue(ProgressBar Ctrl) { return (int)Ctrl.Invoke(new GetProgressBarValueDelegate(GetProgressBarMaxValueFunc), new object[] { Ctrl }); } /// <summary> /// 設置進度條最大值方法 /// </summary> /// <param name="Ctrl"></param> /// <param name="Value"></param> private static void SetProgressBarMaxValueFunc(ProgressBar Ctrl, int Value) { Ctrl.Maximum = Value; Ctrl.Refresh(); } /// <summary> /// 設置進度條最大值 /// </summary> /// <param name="Ctrl"></param> /// <param name="Value"></param> public static void SetProgressBarMaxValue(ProgressBar Ctrl, int Value) { Ctrl.Invoke(new SetProgressBarValueDelegate(SetProgressBarMaxValueFunc), new object[] { Ctrl, Value }); } } }
因爲時間關係,暫時就封裝了這麼多。
會持續更新……
歡迎你們補充。