private void btnAddGroup_Click(object sender, EventArgs e) { var g = new GroupRule(); currentConfig.groups.Add(g); g.name = "双击改名" + currentConfig.groups.Count; var tab = addTab(g); myRuleContainer.SelectTab(tab); SaveRules(); }
/// <summary> /// 加载配置文件 /// </summary> private void LoadRules() { try { if (System.IO.File.Exists(CONFIGPATH)) { var json = System.IO.File.ReadAllText(CONFIGPATH, Encoding.UTF8); //表示是旧的,为了兼容,重新生成一个 if (!string.IsNullOrWhiteSpace(json) && json.Trim().StartsWith("[")) { var config = new RuleConfig(); config.enabled = currentConfig.enabled; //config.groups = new List<GroupRule>(); var g = new GroupRule(); g.name = "默认"; g.rules = Utils.JsonToModel <List <Rule> >(json); currentConfig.groups.Add(g); } else { currentConfig = Utils.JsonToModel <RuleConfig>(json); chkEnabled.Checked = currentConfig.enabled; } } if (currentConfig.groups.Count == 0) { var g = new GroupRule(); g.name = "默认"; currentConfig.groups.Add(g); } if (currentConfig.groups.Count > 0) { foreach (var g in currentConfig.groups) { addTab(g); } } } catch (Exception ex) { Utils.FiddlerLog("加载配置文件失败:" + ex.Message); } }
/// <summary> /// 添加一个tab /// </summary> /// <param name="g"></param> private TabPage addTab(GroupRule g) { var tab = new TabPage(); var grid = createRuleGrid(); if (!string.IsNullOrWhiteSpace(g.name)) { tab.Text = g.name; } tab.Text += " "; if (g.rules != null) { foreach (var r in g.rules) { SetRule(grid, r, -1); } } var chk = new CheckBox(); chk.Location = new Point(6, 3); chk.TabIndex = 1; //chk.Text = "生效"; chk.Checked = g.enabled; chk.Size = new System.Drawing.Size(30, 16); tab.Controls.Add(chk); tab.Controls.Add(grid); chk.CheckedChanged += chk_CheckedChanged; //可以拖放 // tab.AllowDrop = true; myRuleContainer.TabPages.Add(tab); return(tab); }
/// <summary> /// 获取所有规则 /// </summary> /// <returns></returns> public RuleConfig GetRules() { currentConfig.enabled = chkEnabled.Checked; if (currentConfig.groups == null) { currentConfig.groups = new List <GroupRule>(); foreach (TabPage tab in myRuleContainer.TabPages) { if (tab == null) { continue; } var grid = getCurrentGrid(tab); if (grid == null) { continue; } var g = new GroupRule(); g.name = tab.Text.Trim(); var chk = getCurrentChk(tab); if (chk != null) { g.enabled = chk.Checked; } foreach (DataGridViewRow row in grid.Rows) { var r = GetRule(row); g.rules.Add(r); } currentConfig.groups.Add(g); } } return(currentConfig); }