ControlStorageService.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3. namespace FastReport.Utils
  4. {
  5. /// <summary>
  6. /// Storage service for form's controls.
  7. /// </summary>
  8. public class ControlStorageService : StorageService
  9. {
  10. private Control control;
  11. /// <summary>
  12. /// Use device independent pixels (measured in 96dpi mode).
  13. /// </summary>
  14. public bool UseDips { get; set; }
  15. private int ToDip(int value) => UseDips ? (int)(value / control.DpiMultiplier()) : value;
  16. private int ToPix(int value) => UseDips ? (int)(value * control.DpiMultiplier()) : value;
  17. /// <summary>
  18. /// Gets value in device-independent pixels (dips) and converts it to pixels. Takes <see cref="UseDips"/> property into account.
  19. /// </summary>
  20. /// <param name="key">The key.</param>
  21. /// <param name="defaultValue">Default value.</param>
  22. /// <param name="minValue">Minimum value.</param>
  23. /// <param name="maxValue">Maximum value.</param>
  24. /// <returns>Value in dips.</returns>
  25. public int GetDip(string key, int defaultValue = 0, int minValue = int.MinValue, int maxValue = int.MaxValue)
  26. {
  27. int result = ToPix(GetInt(key, defaultValue));
  28. if (result < minValue)
  29. result = minValue;
  30. if (result > maxValue)
  31. result = maxValue;
  32. return result;
  33. }
  34. /// <summary>
  35. /// Stores value in device-independent pixels (dips). Takes <see cref="UseDips"/> property into account.
  36. /// </summary>
  37. /// <param name="key">The key.</param>
  38. /// <param name="value">Value.</param>
  39. public void SetDip(string key, int value) => SetInt(key, ToDip(value));
  40. /// <summary>
  41. /// Gets font from font storage.
  42. /// </summary>
  43. /// <param name="path">Path to font storage.</param>
  44. /// <param name="defaultFont">Default value.</param>
  45. /// <returns>The font object.</returns>
  46. public Font GetFont(string path, Font defaultFont)
  47. {
  48. var fs = new StorageService("Designer,Fonts," + path);
  49. string fontName = fs.GetStr("font-name", defaultFont.Name);
  50. float fontSize = fs.GetFloat("font-size", defaultFont.Size);
  51. bool bold = fs.GetBool("font-bold");
  52. bool italic = fs.GetBool("font-italic");
  53. return new Font(fontName, fontSize, (italic ? FontStyle.Italic : 0) | (bold ? FontStyle.Bold : 0));
  54. }
  55. /// <summary>
  56. /// Initializes a new instance of storage class.
  57. /// </summary>
  58. /// <param name="control">The control which dpi setting is used to save/restore dips.</param>
  59. /// <param name="path">The root path.</param>
  60. public ControlStorageService(Control control, string path) : base(path)
  61. {
  62. this.control = control;
  63. #if (WPF || AVALONIA)
  64. UseDips = true;
  65. #endif
  66. }
  67. }
  68. }