123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System.Drawing;
- using System.Windows.Forms;
- namespace FastReport.Utils
- {
- /// <summary>
- /// Storage service for form's controls.
- /// </summary>
- public class ControlStorageService : StorageService
- {
- private Control control;
- /// <summary>
- /// Use device independent pixels (measured in 96dpi mode).
- /// </summary>
- public bool UseDips { get; set; }
- private int ToDip(int value) => UseDips ? (int)(value / control.DpiMultiplier()) : value;
- private int ToPix(int value) => UseDips ? (int)(value * control.DpiMultiplier()) : value;
- /// <summary>
- /// Gets value in device-independent pixels (dips) and converts it to pixels. Takes <see cref="UseDips"/> property into account.
- /// </summary>
- /// <param name="key">The key.</param>
- /// <param name="defaultValue">Default value.</param>
- /// <param name="minValue">Minimum value.</param>
- /// <param name="maxValue">Maximum value.</param>
- /// <returns>Value in dips.</returns>
- public int GetDip(string key, int defaultValue = 0, int minValue = int.MinValue, int maxValue = int.MaxValue)
- {
- int result = ToPix(GetInt(key, defaultValue));
- if (result < minValue)
- result = minValue;
- if (result > maxValue)
- result = maxValue;
- return result;
- }
- /// <summary>
- /// Stores value in device-independent pixels (dips). Takes <see cref="UseDips"/> property into account.
- /// </summary>
- /// <param name="key">The key.</param>
- /// <param name="value">Value.</param>
- public void SetDip(string key, int value) => SetInt(key, ToDip(value));
- /// <summary>
- /// Gets font from font storage.
- /// </summary>
- /// <param name="path">Path to font storage.</param>
- /// <param name="defaultFont">Default value.</param>
- /// <returns>The font object.</returns>
- public Font GetFont(string path, Font defaultFont)
- {
- var fs = new StorageService("Designer,Fonts," + path);
- string fontName = fs.GetStr("font-name", defaultFont.Name);
- float fontSize = fs.GetFloat("font-size", defaultFont.Size);
- bool bold = fs.GetBool("font-bold");
- bool italic = fs.GetBool("font-italic");
- return new Font(fontName, fontSize, (italic ? FontStyle.Italic : 0) | (bold ? FontStyle.Bold : 0));
- }
- /// <summary>
- /// Initializes a new instance of storage class.
- /// </summary>
- /// <param name="control">The control which dpi setting is used to save/restore dips.</param>
- /// <param name="path">The root path.</param>
- public ControlStorageService(Control control, string path) : base(path)
- {
- this.control = control;
- #if (WPF || AVALONIA)
- UseDips = true;
- #endif
- }
- }
- }
|