/// <summary> /// 保存设置项 /// </summary> /// <returns></returns> private bool SaveSetting() { string path = txtSavePath.Text; bool result = true; try { DirectoryInfo di = new DirectoryInfo(path); } catch (Exception e) { result = false; Console.WriteLine(e.ToString()); } if (!checkPathChar(path)) { result = false; } if (!result) { new Toast(1, "截图文件保存路径不是有效的文件路径,请重新输入!").Show(); return(false); } else { if (!Directory.Exists(path)) { new Toast(1, "截图文件保存路径不存在,请重新输入!").Show(); ScanFolder(); } #region 保存配置 IniFile.WriteIniData(Util.CONFIG_SECTION, Util.START_ON_POWER_ON, chkSPO.Checked.ToString()); RegistryUtil.setStart(chkSPO.Checked); IniFile.WriteIniData(Util.CONFIG_SECTION, Util.SAVE_FILE_EXTENSION, cbbExtension.SelectedItem.ToString()); IniFile.WriteIniData(Util.CONFIG_SECTION, Util.SAVE_PIC_PATH, txtSavePath.Text); if (!Util.HOTKEY_WHOLE.setHotkey(new bool[] { chkAlt1.Checked, chkCtrl1.Checked, chkShift1.Checked, chkWin1.Checked }, cbbKeys1.SelectedIndex)) { cbbKeys1.Select(); new Toast(1, "请至少选择一个辅助键(Ctrl/Alt/Shift/Win)!").Show(); return(false); } if (!Util.HOTKEY_ACTIVE.setHotkey(new bool[] { chkAlt2.Checked, chkCtrl2.Checked, chkShift2.Checked, chkWin2.Checked }, cbbKeys2.SelectedIndex)) { cbbKeys2.Select(); new Toast(1, "请至少选择一个辅助键(Ctrl/Alt/Shift/Win)!").Show(); return(false); } if (!Util.HOTKEY_FREE.setHotkey(new bool[] { chkAlt3.Checked, chkCtrl3.Checked, chkShift3.Checked, chkWin3.Checked }, cbbKeys3.SelectedIndex)) { cbbKeys3.Select(); new Toast(1, "请至少选择一个辅助键(Ctrl/Alt/Shift/Win)!").Show(); return(false); } if (!Util.HOTKEY_LAST.setHotkey(new bool[] { chkAlt4.Checked, chkCtrl4.Checked, chkShift4.Checked, chkWin4.Checked }, cbbKeys4.SelectedIndex)) { cbbKeys4.Select(); new Toast(1, "请至少选择一个辅助键(Ctrl/Alt/Shift/Win)!").Show(); return(false); } if (!Util.HOTKEY_CLIP.setHotkey(new bool[] { chkAlt5.Checked, chkCtrl5.Checked, chkShift5.Checked, chkWin5.Checked }, cbbKeys5.SelectedIndex)) { cbbKeys5.Select(); new Toast(1, "请至少选择一个辅助键(Ctrl/Alt/Shift/Win)!").Show(); return(false); } if (!Util.HOTKEY_EXIT.setHotkey(new bool[] { chkAlt6.Checked, chkCtrl6.Checked, chkShift6.Checked, chkWin6.Checked }, cbbKeys6.SelectedIndex)) { cbbKeys6.Select(); new Toast(1, "请至少选择一个辅助键(Ctrl/Alt/Shift/Win)!").Show(); return(false); } #endregion if (!checkHotkeyConflit()) { return(false); } #region 注册新设置的快捷键 if (Util.HOTKEY_WHOLE.RegistedHotkey != Util.HOTKEY_WHOLE.Hotkey) { IniFile.WriteIniData(Util.CONFIG_SECTION, Util.HOTKEY_WHOLE.KeyName, Util.HOTKEY_WHOLE.Hotkey); result &= HotKey.RegisteSetHotkey(Program.mainForm.Handle, Util.HOTKEY_WHOLE); } if (Util.HOTKEY_ACTIVE.RegistedHotkey != Util.HOTKEY_ACTIVE.Hotkey) { IniFile.WriteIniData(Util.CONFIG_SECTION, Util.HOTKEY_ACTIVE.KeyName, Util.HOTKEY_ACTIVE.Hotkey); result &= HotKey.RegisteSetHotkey(Program.mainForm.Handle, Util.HOTKEY_ACTIVE); } if (Util.HOTKEY_FREE.RegistedHotkey != Util.HOTKEY_FREE.Hotkey) { IniFile.WriteIniData(Util.CONFIG_SECTION, Util.HOTKEY_FREE.KeyName, Util.HOTKEY_FREE.Hotkey); result &= HotKey.RegisteSetHotkey(Program.mainForm.Handle, Util.HOTKEY_FREE); } if (Util.HOTKEY_LAST.RegistedHotkey != Util.HOTKEY_LAST.Hotkey) { IniFile.WriteIniData(Util.CONFIG_SECTION, Util.HOTKEY_LAST.KeyName, Util.HOTKEY_LAST.Hotkey); result &= HotKey.RegisteSetHotkey(Program.mainForm.Handle, Util.HOTKEY_LAST); } if (Util.HOTKEY_CLIP.RegistedHotkey != Util.HOTKEY_CLIP.Hotkey) { IniFile.WriteIniData(Util.CONFIG_SECTION, Util.HOTKEY_CLIP.KeyName, Util.HOTKEY_CLIP.Hotkey); result &= HotKey.RegisteSetHotkey(Program.mainForm.Handle, Util.HOTKEY_CLIP); } if (Util.HOTKEY_EXIT.RegistedHotkey != Util.HOTKEY_EXIT.Hotkey) { IniFile.WriteIniData(Util.CONFIG_SECTION, Util.HOTKEY_EXIT.KeyName, Util.HOTKEY_EXIT.Hotkey); result &= HotKey.RegisteSetHotkey(Program.mainForm.Handle, Util.HOTKEY_EXIT); } if (!result) { new Toast(2, "保存失败,请检查是否有快捷键和其他应用程序存在冲突").Show(); } #endregion isSaved = result; return(isSaved); } }
private void loadSetting() { Control.CheckForIllegalCrossThreadCalls = false; #region 读取配置文件设置项 string autorun = IniFile.ReadIniData(Util.CONFIG_SECTION, Util.START_ON_POWER_ON, "true").ToLower(); string path = IniFile.ReadIniData(Util.CONFIG_SECTION, Util.SAVE_PIC_PATH, Util.DEFAULT_SAVE_PIC_PATH); string fileextension = IniFile.ReadIniData(Util.CONFIG_SECTION, Util.SAVE_FILE_EXTENSION, Util.DEFAULT_FILE_EXTENSION).ToLower(); #endregion #region 设置开机启动项 if (autorun.Equals("true")) { chkSPO.Checked = true; } else if (autorun.Equals("false")) { chkSPO.Checked = false; } else { IniFile.WriteIniData(Util.CONFIG_SECTION, Util.START_ON_POWER_ON, "true"); chkSPO.Checked = true; } chkSPO.OriState = chkSPO.Checked; #endregion #region 设置截图文件保存目录 if (Directory.Exists(path)) { txtSavePath.Text = path; } else { path = Application.StartupPath + "\\picture"; IniFile.WriteIniData(Util.CONFIG_SECTION, Util.SAVE_PIC_PATH, path); txtSavePath.Text = path; } txtSavePath.OriText = txtSavePath.Text; #endregion #region 设置截图文件扩展名 if (keys.Contains(fileextension)) { cbbExtension.SelectedItem = fileextension; } else { IniFile.WriteIniData(Util.CONFIG_SECTION, Util.SAVE_FILE_EXTENSION, extensions[0]); cbbExtension.SelectedIndex = 0; } cbbExtension.OriIndex = cbbExtension.SelectedIndex; #endregion #region 加载快捷键配置 Util.HOTKEY_WHOLE.IsValueChanged = false; int[] hotKeyValue = HotKey.getHotkeyFromIni(Util.HOTKEY_WHOLE); chkAlt1.OriState = chkAlt1.Checked = hotKeyValue[0] == 1; chkCtrl1.OriState = chkCtrl1.Checked = hotKeyValue[1] == 1; chkShift1.OriState = chkShift1.Checked = hotKeyValue[2] == 1; chkWin1.OriState = chkWin1.Checked = hotKeyValue[3] == 1; cbbKeys1.OriIndex = cbbKeys1.SelectedIndex = hotKeyValue[4]; Util.HOTKEY_ACTIVE.IsValueChanged = false; hotKeyValue = HotKey.getHotkeyFromIni(Util.HOTKEY_ACTIVE); chkAlt2.OriState = chkAlt2.Checked = hotKeyValue[0] == 1; chkCtrl2.OriState = chkCtrl2.Checked = hotKeyValue[1] == 1; chkShift2.OriState = chkShift2.Checked = hotKeyValue[2] == 1; chkWin2.OriState = chkWin2.Checked = hotKeyValue[3] == 1; cbbKeys2.OriIndex = cbbKeys2.SelectedIndex = hotKeyValue[4]; Util.HOTKEY_FREE.IsValueChanged = false; hotKeyValue = HotKey.getHotkeyFromIni(Util.HOTKEY_FREE); chkAlt3.OriState = chkAlt3.Checked = hotKeyValue[0] == 1; chkCtrl3.OriState = chkCtrl3.Checked = hotKeyValue[1] == 1; chkShift3.OriState = chkShift3.Checked = hotKeyValue[2] == 1; chkWin3.OriState = chkWin3.Checked = hotKeyValue[3] == 1; cbbKeys3.OriIndex = cbbKeys3.SelectedIndex = hotKeyValue[4]; Util.HOTKEY_LAST.IsValueChanged = false; hotKeyValue = HotKey.getHotkeyFromIni(Util.HOTKEY_LAST); chkAlt4.OriState = chkAlt4.Checked = hotKeyValue[0] == 1; chkCtrl4.OriState = chkCtrl4.Checked = hotKeyValue[1] == 1; chkShift4.OriState = chkShift4.Checked = hotKeyValue[2] == 1; chkWin4.OriState = chkWin4.Checked = hotKeyValue[3] == 1; cbbKeys4.OriIndex = cbbKeys4.SelectedIndex = hotKeyValue[4]; Util.HOTKEY_CLIP.IsValueChanged = false; hotKeyValue = HotKey.getHotkeyFromIni(Util.HOTKEY_CLIP); chkAlt5.OriState = chkAlt5.Checked = hotKeyValue[0] == 1; chkCtrl5.OriState = chkCtrl5.Checked = hotKeyValue[1] == 1; chkShift5.OriState = chkShift5.Checked = hotKeyValue[2] == 1; chkWin5.OriState = chkWin5.Checked = hotKeyValue[3] == 1; cbbKeys5.OriIndex = cbbKeys5.SelectedIndex = hotKeyValue[4]; Util.HOTKEY_EXIT.IsValueChanged = false; hotKeyValue = HotKey.getHotkeyFromIni(Util.HOTKEY_EXIT); chkAlt6.OriState = chkAlt6.Checked = hotKeyValue[0] == 1; chkCtrl6.OriState = chkCtrl6.Checked = hotKeyValue[1] == 1; chkShift6.OriState = chkShift6.Checked = hotKeyValue[2] == 1; chkWin6.OriState = chkWin6.Checked = hotKeyValue[3] == 1; cbbKeys6.OriIndex = cbbKeys6.SelectedIndex = hotKeyValue[4]; #endregion }