注:
1.此类属于非常用类,只因本人需要编写的...顺便刷贴一下
2.对于初学,其中的枚举使用可以为你加深一点印象
以下为操作类代码:
public class AchCtrer { #region /// <summary> /// 1010 /// </summary> static string defTdlr = Ach.defSigs; int sigCnt, defSigCnt = 2; char chr; int len, cnt, idx; bool[] ctrTdlrS = new bool[] { true, false, true, false }; Button tdlr; List<Button> tdlrL; Color defClrb, tdlrClrb = Color.Gray; bool bol; /// <summary> /// 输出:1010 /// </summary> public string IOutAchSigs = ""; /// <summary> /// AnchorStyles.Top | AnchorStyles.Left /// </summary> AnchorStyles achStls; #endregion #region Control achCtr; public Control IGetAchCtr { set { achCtr = value; achStls = achCtr.Anchor; if (achStls != Ach.TL) Sigs(achStls, out IOutAchSigs, out sigCnt); else { IOutAchSigs = defTdlr; sigCnt = defSigCnt; } if (sigCnt > 0) IniTdlrCtr(); else achStls -= AnchorStyles.None; } } private void IniTdlrCtr() { cnt = IOutAchSigs.Length; for (int i = 0; i < cnt; i++) { #region chr = IOutAchSigs[i]; tdlr = tdlrL[i]; if (chr == '0') { ctrTdlrS[i] = false; tdlr.FlatStyle = FlatStyle.Standard; tdlr.BackColor = defClrb; } else { ctrTdlrS[i] = true; tdlr.FlatStyle = FlatStyle.Popup; tdlr.BackColor = tdlrClrb; } #endregion } } /// <summary> /// /// </summary> /// <param name="tdlrS">Ancher 属性操作按钮 [共4个'方向键']</param> public AchCtrer22(Button[] tblrS) { tdlrL = new List<Button>(tblrS); Ini(); } public AchCtrer22(Button b_top,Button b_bottom,Button b_left,Button b_right) { tdlrL = new List<Button>(); tdlrL.Add(b_top); tdlrL.Add(b_bottom); tdlrL.Add(b_left); tdlrL.Add(b_right); Ini(); } private void Ini() { IniPar(); IniEvt(); } private void IniPar() { tdlr = new Button(); defClrb = tdlr.BackColor; } private void IniEvt() { #region len = tdlrL.Count; for (int i = 0; i < len; i++) { tdlr = tdlrL[i]; tdlr.BackColor = defClrb; tdlr.FlatStyle = FlatStyle.Standard; tdlr.Click += tdlr_Click; } #endregion } private void tdlr_Click(object s, EventArgs e) { tdlr = s as Button; idx = tdlrL.IndexOf(tdlr); #region if (tdlr.FlatStyle == FlatStyle.Popup) { tdlr.FlatStyle = FlatStyle.Standard; tdlr.BackColor = defClrb; ctrTdlrS[idx] = false; achStls -= Ach.stlS[idx]; sigCnt--; } else { tdlr.FlatStyle = FlatStyle.Popup; tdlr.BackColor = tdlrClrb; ctrTdlrS[idx] = true; achStls |= Ach.stlS[idx]; sigCnt++; } #endregion TdlrS(); } private void TdlrS() { #region if (sigCnt > 0) { IOutAchSigs = ""; for (int i = 0; i < cnt; i++) { bol = ctrTdlrS[i]; if (bol) IOutAchSigs += '1'; else IOutAchSigs += '0'; } if (achStls == Ach.TL) IOutAchSigs = ""; } else { achStls = AnchorStyles.None; IOutAchSigs = "-1";// defTdlr; } achCtr.Anchor = achStls; #endregion } #endregion }
以下为操作类关联函数代码:
/// <summary> /// 输出: 控件 Anchor 上下左右 01 表示标识,如:上左=1010 , None="-1",默认为:空; /// </summary> /// <param name="achStl"></param> /// <param name="sigs"></param> /// <param name="sigCnt">有多少个 sig </param> public static void Sigs(AnchorStyles achStl, out string sigs, out int sigCnt) { string achs = achStl + ""; achs = achs.Replace(" ", ""); sigCnt = 0; sigs = "-1"; if (achs == N) return; string[] achS = achs.Split(c.d); List<string> achL = new List<string>(achS); int cnt = namL.Count, idx; string ach; sigs = ""; for (int i = 0; i < cnt; i++) { ach = namL[i]; idx = achL.IndexOf(ach); if (idx < 0) sigs += '0'; else { sigs += '1'; sigCnt++; } } return; }
使用方法:
1.初始化:Achctrer ctrer=new Achctrer(四点个[方向顺序:上下左右]控件);
2.参数属性说明:
a.IGetAchChtr:此属性用于获取你要操作 Anchor 属性的控件
b.IOutAchSigs:此参数作为输出参数,其输出值为 Anchor 标识[此标识为本人自定义],例:-1=None,""=默认,1010=上左
交流 QQ : 2412366909@qq.com
手机号码:177-7499-4428
注:本人使用过程中暂未发现造成存取不正确的问题
{ #region
/// <summary> /// 1010 /// </summary> static string defTdlr = Ach.defSigs; int sigCnt, defSigCnt = 2;
char chr; int len, cnt, idx; bool[] ctrTdlrS = new bool[] { true, false, true, false }; Button tdlr; List<Button> tdlrL; Color defClrb, tdlrClrb = Color.Gray; bool bol;
/// <summary> /// 输出:1010 /// </summary> public string IOutAchSigs = ""; /// <summary> /// AnchorStyles.Top | AnchorStyles.Left /// </summary> AnchorStyles achStls;
#endregion
#region
Control achCtr; public Control IGetAchCtr { set { achCtr = value; achStls = achCtr.Anchor;
if (achStls != Ach.TL) Ach.Sigs(achStls, out IOutAchSigs, out sigCnt); else { IOutAchSigs = defTdlr; sigCnt = defSigCnt; }
if (sigCnt > 0) IniTdlrCtr(); else achStls -= AnchorStyles.None; } } private void IniTdlrCtr() { cnt = IOutAchSigs.Length; for (int i = 0; i < cnt; i++) { #region
chr = IOutAchSigs[i]; tdlr = tdlrL[i];
if (chr == '0') { ctrTdlrS[i] = false; tdlr.FlatStyle = FlatStyle.Standard; tdlr.BackColor = defClrb; } else { ctrTdlrS[i] = true; tdlr.FlatStyle = FlatStyle.Popup; tdlr.BackColor = tdlrClrb; }
#endregion } } /// <summary> /// /// </summary> /// <param name="tdlrS">Ancher 属性操作按钮 [共4个'方向键']</param> public AchCtrer22(Button[] tblrS) { tdlrL = new List<Button>(tblrS); Ini(); } public AchCtrer22(Button b_top,Button b_bottom,Button b_left,Button b_right) { tdlrL = new List<Button>(); tdlrL.Add(b_top); tdlrL.Add(b_bottom); tdlrL.Add(b_left); tdlrL.Add(b_right);
Ini(); } private void Ini() { IniPar(); IniEvt(); } private void IniPar() { tdlr = new Button(); defClrb = tdlr.BackColor; } private void IniEvt() { #region
len = tdlrL.Count; for (int i = 0; i < len; i++) { tdlr = tdlrL[i]; tdlr.BackColor = defClrb; tdlr.FlatStyle = FlatStyle.Standard;
tdlr.Click += tdlr_Click; }
#endregion } private void tdlr_Click(object s, EventArgs e) { tdlr = s as Button; idx = tdlrL.IndexOf(tdlr);
#region
if (tdlr.FlatStyle == FlatStyle.Popup) { tdlr.FlatStyle = FlatStyle.Standard; tdlr.BackColor = defClrb; ctrTdlrS[idx] = false;
achStls -= Ach.stlS[idx]; sigCnt--; } else { tdlr.FlatStyle = FlatStyle.Popup; tdlr.BackColor = tdlrClrb; ctrTdlrS[idx] = true;
achStls |= Ach.stlS[idx]; sigCnt++; }
#endregion
TdlrS(); } private void TdlrS() { #region
if (sigCnt > 0) { IOutAchSigs = ""; for (int i = 0; i < cnt; i++) { bol = ctrTdlrS[i]; if (bol) IOutAchSigs += '1'; else IOutAchSigs += '0'; }
if (achStls == Ach.TL) IOutAchSigs = ""; } else { achStls = AnchorStyles.None; IOutAchSigs = "-1";// defTdlr; }
achCtr.Anchor = achStls;
#endregion }
#endregion }