NewItemForm.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using BulePointLilac.Controls;
  2. using BulePointLilac.Methods;
  3. using System;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace ContextMenuManager.Controls
  7. {
  8. class NewItemForm : ResizbleForm
  9. {
  10. public NewItemForm()
  11. {
  12. this.AcceptButton = btnOk;
  13. this.CancelButton = btnCancel;
  14. this.Font = SystemFonts.MenuFont;
  15. this.MaximizeBox = this.MinimizeBox = false;
  16. this.ShowIcon = this.ShowInTaskbar = false;
  17. this.StartPosition = FormStartPosition.CenterParent;
  18. this.SizeGripStyle = SizeGripStyle.Hide;
  19. this.VerticalResizable = false;
  20. InitializeComponents();
  21. }
  22. public string ItemText { get => txtText.Text; set => txtText.Text = value; }
  23. public string Command { get => txtCommand.Text; set => txtCommand.Text = value; }
  24. public string Arguments { get => txtArguments.Text; set => txtArguments.Text = value; }
  25. public string FullCommand
  26. {
  27. get
  28. {
  29. if(Arguments.IsNullOrWhiteSpace()) return Command;
  30. else if(Command.IsNullOrWhiteSpace()) return Arguments;
  31. else return $"\"{Command}\" \"{Arguments}\"";
  32. }
  33. }
  34. protected readonly Label lblText = new Label
  35. {
  36. Text = AppString.Dialog.ItemText,
  37. AutoSize = true
  38. };
  39. protected readonly Label lblCommand = new Label
  40. {
  41. Text = AppString.Dialog.ItemCommand,
  42. AutoSize = true
  43. };
  44. protected readonly Label lblArguments = new Label
  45. {
  46. Text = AppString.Dialog.CommandArguments,
  47. AutoSize = true
  48. };
  49. protected readonly TextBox txtText = new TextBox();
  50. protected readonly TextBox txtCommand = new TextBox();
  51. protected readonly TextBox txtArguments = new TextBox();
  52. protected readonly Button btnBrowse = new Button
  53. {
  54. Text = AppString.Dialog.Browse,
  55. AutoSize = true
  56. };
  57. protected readonly Button btnOk = new Button
  58. {
  59. Text = AppString.Dialog.Ok,
  60. AutoSize = true
  61. };
  62. protected readonly Button btnCancel = new Button
  63. {
  64. DialogResult = DialogResult.Cancel,
  65. Text = AppString.Dialog.Cancel,
  66. AutoSize = true
  67. };
  68. private static Size LastSize = new Size();
  69. protected virtual void InitializeComponents()
  70. {
  71. this.Controls.AddRange(new Control[] { lblText, lblCommand, lblArguments,
  72. txtText, txtCommand, txtArguments, btnBrowse, btnOk, btnCancel });
  73. int a = 20.DpiZoom();
  74. btnBrowse.Anchor = btnOk.Anchor = btnCancel.Anchor = AnchorStyles.Right | AnchorStyles.Top;
  75. txtText.Top = lblText.Top = lblText.Left = lblCommand.Left = lblArguments.Left = a;
  76. btnBrowse.Top = txtCommand.Top = lblCommand.Top = txtText.Bottom + a;
  77. lblArguments.Top = txtArguments.Top = txtCommand.Bottom + a;
  78. btnOk.Top = btnCancel.Top = txtArguments.Bottom + a;
  79. btnCancel.Left = btnBrowse.Left = this.ClientSize.Width - btnCancel.Width - a;
  80. btnOk.Left = btnCancel.Left - btnOk.Width - a;
  81. int b = Math.Max(Math.Max(lblText.Width, lblCommand.Width), lblArguments.Width) + btnBrowse.Width + 4 * a;
  82. this.ClientSize = new Size(250.DpiZoom() + b, btnOk.Bottom + a);
  83. this.MinimumSize = this.Size;
  84. this.Resize += (sender, e) =>
  85. {
  86. txtText.Width = txtCommand.Width = txtArguments.Width = this.ClientSize.Width - b;
  87. txtText.Left = txtCommand.Left = txtArguments.Left = btnBrowse.Left - txtCommand.Width - a;
  88. LastSize = this.Size;
  89. };
  90. if(LastSize != null) this.Size = LastSize;
  91. this.OnResize(null);
  92. }
  93. }
  94. }