Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Windows.Forms;
- using Newtonsoft.Json;
- using System.IO;
- using System.Linq;
- using System.Drawing;
- using System.Collections.Generic;
- using System;
- namespace Assignment2
- {
- public partial class Form1 : Form
- {
- private static readonly string UserSettingsPath = Path.Combine(Environment.CurrentDirectory, @"../../UserSettings.json");
- private static readonly string idcanymore = Path.Combine(Environment.CurrentDirectory, @"../../magic.html");
- public static Settings UserSettings;
- public Form1() => InitializeComponent();
- private void Form1_Load(object sender, EventArgs e)
- {
- string[] Lines = File.ReadAllLines(UserSettingsPath);
- string AllFileLines = string.Join("", Lines);
- UserSettings = JsonConvert.DeserializeObject<Settings>(AllFileLines);
- ApplySettings();
- webView21.Source = new Uri(idcanymore);
- foreach (Control Control in GetAllControlsOfType(this, typeof(Label)))
- {
- Label Label = Control as Label;
- Label.MaximumSize = new Size(Label.Parent.Width - 4, 1000);
- }
- foreach (Control Control in GetAllControlsOfType(this, typeof(TextBox)))
- {
- TextBox TextBox = Control as TextBox;
- TextBox.Height = 13 * (TextBox.Text.Split('\n').Length + 1);
- TextBox.MaximumSize = new Size(TextBox.Parent.Width - 4, 1000);
- }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- JsonSerializer JsonSerializer = new JsonSerializer
- {
- Formatting = Formatting.Indented
- };
- using (StreamWriter SW = new StreamWriter(UserSettingsPath))
- {
- using (JsonWriter Writer = new JsonTextWriter(SW))
- {
- JsonSerializer.Serialize(Writer, UserSettings);
- }
- }
- }
- private float GetValidValue(string val)
- {
- if (NullEmptyOrWhitespace(val) || val == "0")
- return 8f;
- return float.Parse(val);
- }
- private bool NullEmptyOrWhitespace(string str) =>
- str == null || str.Trim() == "";
- public void ApplySettings()
- {
- Font TitleFont = new Font(UserSettings.TitleFontName == "" ? "Microsoft Sans Serif" : UserSettings.TitleFontName, GetValidValue(UserSettings.TitleFontSize));
- Font GeneralFont = new Font(UserSettings.GeneralFontName == "" ? "Microsoft Sans Serif" : UserSettings.GeneralFontName, GetValidValue(UserSettings.GeneralFontSize));
- Font CodeFont = new Font(UserSettings.CodeFontName == "" ? "Microsoft Sans Serif" : UserSettings.CodeFontName, GetValidValue(UserSettings.CodeFontSize));
- foreach (Control Control in GetAllControlsOfType(tabControl1, typeof(Label)))
- {
- Label Label = Control as Label;
- Label.Margin = new Padding(Label.Margin.Left, Label.Margin.Top, Label.Margin.Right, UserSettings.TextBoxMargining == "" ? 10 : int.Parse(UserSettings.TextBoxMargining));
- if ((string)Control.Tag == "title")
- {
- Label.Font = TitleFont;
- Label.ForeColor = Color.FromName(UserSettings.TitleFontColor == "" ? "black" : UserSettings.TitleFontColor);
- }
- else if ((string)Control.Tag == "general")
- {
- Label.Font = GeneralFont;
- Label.ForeColor = Color.FromName(UserSettings.GeneralFontColor == "" ? "black" : UserSettings.GeneralFontColor);
- }
- }
- foreach (Control Control in GetAllControlsOfType(tabControl1, typeof(TextBox)))
- {
- TextBox TextBox = Control as TextBox;
- TextBox.Margin = new Padding(TextBox.Margin.Left, TextBox.Margin.Top, TextBox.Margin.Right, UserSettings.TextBoxMargining == "" ? 10 : int.Parse(UserSettings.TextBoxMargining));
- if ((string)Control.Tag == "code")
- {
- TextBox.Font = CodeFont;
- TextBox.ForeColor = Color.FromName(UserSettings.CodeFontColor == "" ? "black" : UserSettings.CodeFontColor);
- }
- }
- }
- public IEnumerable<Control> GetAllControlsOfType(Control Control, Type Type)
- {
- IEnumerable<Control> Controls = Control.Controls.Cast<Control>();
- return Controls
- .SelectMany(Ctrl => GetAllControlsOfType(Ctrl, Type))
- .Concat(Controls)
- .Where(c => c.GetType() == Type);
- }
- public void Alert(object obj) => MessageBox.Show(obj.ToString());
- private void OpenSettings_Click(object sender, EventArgs e)
- {
- Form2 F2 = new Form2
- {
- Owner = this
- };
- F2.Show();
- }
- }
- public class Settings
- {
- public string TextBoxMargining { get; set; }
- public string TitleFontName { get; set; }
- public string TitleFontSize { get; set; }
- public string TitleFontColor { get; set; }
- public string GeneralFontName { get; set; }
- public string GeneralFontSize { get; set; }
- public string GeneralFontColor { get; set; }
- public string CodeFontName { get; set; }
- public string CodeFontSize { get; set; }
- public string CodeFontColor { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement