TaskDialog.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. using Resources;
  2. using SmartCoalApplication.SystemLayer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace SmartCoalApplication.Core
  11. {
  12. public static class TaskDialog
  13. {
  14. public static int DefaultPixelWidth96Dpi = 300;
  15. public static TaskButton Show(
  16. IWin32Window owner,
  17. Icon formIcon,
  18. string formTitle,
  19. Image taskImage,
  20. string introText,
  21. TaskButton[] taskButtons,
  22. TaskButton acceptTaskButton,
  23. TaskButton cancelTaskButton)
  24. {
  25. return Show(owner, formIcon, formTitle, taskImage, true, introText,
  26. taskButtons, acceptTaskButton, cancelTaskButton, false, 0);
  27. }
  28. public static TaskButton Show(
  29. IWin32Window owner,
  30. Icon formIcon,
  31. string formTitle,
  32. Image taskImage,
  33. bool scaleTaskImageWithDpi,
  34. string introText,
  35. TaskButton[] taskButtons,
  36. TaskButton acceptTaskButton,
  37. TaskButton cancelTaskButton,
  38. bool showCheckBox,
  39. int conflictNum)
  40. {
  41. bool useChecked;
  42. return Show(owner, formIcon, formTitle, taskImage, scaleTaskImageWithDpi, introText,
  43. taskButtons, acceptTaskButton, cancelTaskButton, DefaultPixelWidth96Dpi, showCheckBox, conflictNum, out useChecked);
  44. }
  45. public static TaskButton Show(
  46. IWin32Window owner,
  47. Icon formIcon,
  48. string formTitle,
  49. Image taskImage,
  50. bool scaleTaskImageWithDpi,
  51. string introText,
  52. TaskButton[] taskButtons,
  53. TaskButton acceptTaskButton,
  54. TaskButton cancelTaskButton,
  55. int pixelWidth96Dpi,
  56. bool showCbox,
  57. int conflictNum,
  58. out bool useChecked)
  59. {
  60. return Show(owner, formIcon, formTitle, taskImage, scaleTaskImageWithDpi, introText,
  61. taskButtons, acceptTaskButton, cancelTaskButton, pixelWidth96Dpi, null, null, showCbox, conflictNum, out useChecked);
  62. }
  63. public static TaskButton Show(
  64. IWin32Window owner,
  65. Icon formIcon,
  66. string formTitle,
  67. Image taskImage,
  68. bool scaleTaskImageWithDpi,
  69. string introText,
  70. TaskButton[] taskButtons,
  71. TaskButton acceptTaskButton,
  72. TaskButton cancelTaskButton,
  73. int pixelWidth96Dpi,
  74. string auxButtonText,
  75. EventHandler auxButtonClickHandler,
  76. bool showCbox,
  77. int conflictNum,
  78. out bool useChecked)
  79. {
  80. using (TaskDialogForm form = new TaskDialogForm(showCbox, conflictNum))
  81. {
  82. form.Icon = formIcon;
  83. form.IntroText = introText;
  84. form.Text = formTitle;
  85. form.TaskImage = taskImage;
  86. form.ScaleTaskImageWithDpi = scaleTaskImageWithDpi;
  87. form.TaskButtons = taskButtons;
  88. form.AcceptTaskButton = acceptTaskButton;
  89. form.CancelTaskButton = cancelTaskButton;
  90. if (auxButtonText != null)
  91. {
  92. form.AuxButtonText = auxButtonText;
  93. }
  94. if (auxButtonClickHandler != null)
  95. {
  96. form.AuxButtonClick += auxButtonClickHandler;
  97. }
  98. int pixelWidth = UI.ScaleWidth(pixelWidth96Dpi);
  99. form.ClientSize = new Size(pixelWidth, form.ClientSize.Height);
  100. DialogResult dr = form.ShowDialog(owner);
  101. TaskButton result = form.DialogResult;
  102. useChecked = form.checkBox.Checked;
  103. return result;
  104. }
  105. }
  106. private sealed class TaskDialogForm : PdnBaseForm
  107. {
  108. public bool showCheckbox = true;
  109. private PictureBox taskImagePB;
  110. private bool scaleTaskImageWithDpi;
  111. private Label introTextLabel;
  112. private TaskButton[] taskButtons;
  113. private CommandButton[] commandButtons;
  114. private HeaderLabel separator;
  115. private TaskButton acceptTaskButton;
  116. private TaskButton cancelTaskButton;
  117. private TaskButton dialogResult;
  118. public CheckBox checkBox;
  119. private Button auxButton;
  120. public string AuxButtonText
  121. {
  122. get
  123. {
  124. return this.auxButton.Text;
  125. }
  126. set
  127. {
  128. this.auxButton.Text = value;
  129. PerformLayout();
  130. }
  131. }
  132. public event EventHandler AuxButtonClick;
  133. private void OnAuxButtonClick()
  134. {
  135. if (AuxButtonClick != null)
  136. {
  137. AuxButtonClick(this, EventArgs.Empty);
  138. }
  139. }
  140. public new TaskButton DialogResult
  141. {
  142. get
  143. {
  144. return this.dialogResult;
  145. }
  146. }
  147. public Image TaskImage
  148. {
  149. get
  150. {
  151. return this.taskImagePB.Image;
  152. }
  153. set
  154. {
  155. this.taskImagePB.Image = value;
  156. PerformLayout();
  157. Invalidate(true);
  158. }
  159. }
  160. public bool ScaleTaskImageWithDpi
  161. {
  162. get
  163. {
  164. return this.scaleTaskImageWithDpi;
  165. }
  166. set
  167. {
  168. this.scaleTaskImageWithDpi = value;
  169. PerformLayout();
  170. Invalidate(true);
  171. }
  172. }
  173. public string IntroText
  174. {
  175. get
  176. {
  177. return this.introTextLabel.Text;
  178. }
  179. set
  180. {
  181. this.introTextLabel.Text = value;
  182. PerformLayout();
  183. Invalidate(true);
  184. }
  185. }
  186. public TaskButton[] TaskButtons
  187. {
  188. get
  189. {
  190. return (TaskButton[])this.taskButtons.Clone();
  191. }
  192. set
  193. {
  194. this.taskButtons = (TaskButton[])value.Clone();
  195. InitCommandButtons();
  196. PerformLayout();
  197. Invalidate(true);
  198. }
  199. }
  200. public TaskButton AcceptTaskButton
  201. {
  202. get
  203. {
  204. return this.acceptTaskButton;
  205. }
  206. set
  207. {
  208. this.acceptTaskButton = value;
  209. IButtonControl newAcceptButton = null;
  210. for (int i = 0; i < this.commandButtons.Length; ++i)
  211. {
  212. TaskButton asTaskButton = this.commandButtons[i].Tag as TaskButton;
  213. if (this.acceptTaskButton == asTaskButton)
  214. {
  215. newAcceptButton = this.commandButtons[i];
  216. }
  217. }
  218. AcceptButton = newAcceptButton;
  219. }
  220. }
  221. public TaskButton CancelTaskButton
  222. {
  223. get
  224. {
  225. return this.cancelTaskButton;
  226. }
  227. set
  228. {
  229. this.cancelTaskButton = value;
  230. IButtonControl newCancelButton = null;
  231. for (int i = 0; i < this.commandButtons.Length; ++i)
  232. {
  233. TaskButton asTaskButton = this.commandButtons[i].Tag as TaskButton;
  234. if (this.cancelTaskButton == asTaskButton)
  235. {
  236. newCancelButton = this.commandButtons[i];
  237. }
  238. }
  239. CancelButton = newCancelButton;
  240. }
  241. }
  242. /// <summary>
  243. ///
  244. /// </summary>
  245. /// <param name="showCheckbox">是否显示checkbox</param>
  246. /// <param name="num">冲突数量</param>
  247. public TaskDialogForm(bool showCheckbox, int num)
  248. {
  249. this.showCheckbox = showCheckbox;
  250. InitializeComponent();
  251. if (!this.showCheckbox)
  252. {
  253. this.checkBox.Visible = false;
  254. }
  255. else
  256. {
  257. if (num > 0)
  258. {
  259. this.checkBox.Text = PdnResources.GetString("Menu.Forafter.text") + num + PdnResources.GetString("Menu.Fiveoperation.text");
  260. this.checkBox.Visible = true;
  261. }
  262. else
  263. {
  264. this.checkBox.Visible = false;
  265. }
  266. }
  267. }
  268. private void InitializeComponent()
  269. {
  270. SuspendLayout();
  271. this.introTextLabel = new Label();
  272. this.auxButton = new Button();
  273. this.taskImagePB = new PictureBox();
  274. this.separator = new HeaderLabel();
  275. this.checkBox = new CheckBox();
  276. //
  277. // checkBox
  278. //
  279. this.checkBox.Dock = DockStyle.Bottom;
  280. this.checkBox.Padding = new Padding(10, 0, 0, 0);
  281. //
  282. // introTextLabel
  283. //
  284. this.introTextLabel.Name = "introTextLabel";
  285. //
  286. // taskImagePB
  287. //
  288. this.taskImagePB.Name = "taskImagePB";
  289. this.taskImagePB.SizeMode = PictureBoxSizeMode.StretchImage;
  290. //
  291. // auxButton
  292. //
  293. this.auxButton.Name = "auxButton";
  294. this.auxButton.AutoSize = true;
  295. this.auxButton.FlatStyle = FlatStyle.System;
  296. this.auxButton.Visible = false;
  297. this.auxButton.Click +=
  298. delegate (object sender, EventArgs e)
  299. {
  300. OnAuxButtonClick();
  301. };
  302. //
  303. // separator
  304. //
  305. this.separator.Name = "separator";
  306. this.separator.RightMargin = 0;
  307. //
  308. // TaskDialogForm
  309. //
  310. this.Name = "TaskDialogForm";
  311. this.ClientSize = new Size(300, 100);
  312. this.FormBorderStyle = FormBorderStyle.FixedDialog;
  313. this.MinimizeBox = false;
  314. this.MaximizeBox = false;
  315. this.ShowInTaskbar = false;
  316. this.StartPosition = FormStartPosition.CenterParent;
  317. this.Controls.Add(this.introTextLabel);
  318. this.Controls.Add(this.taskImagePB);
  319. this.Controls.Add(this.auxButton);
  320. this.Controls.Add(this.separator);
  321. this.Controls.Add(this.checkBox);
  322. ResumeLayout();
  323. }
  324. protected override void OnLayout(LayoutEventArgs levent)
  325. {
  326. int leftMargin = UI.ScaleWidth(8);
  327. int rightMargin = UI.ScaleWidth(8);
  328. int topMargin = UI.ScaleHeight(8);
  329. int bottomMargin = UI.ScaleHeight(8);
  330. int imageToIntroHMargin = UI.ScaleWidth(8);
  331. int topSectionToLinksVMargin = UI.ScaleHeight(8);
  332. int commandButtonVMargin = UI.ScaleHeight(0);
  333. int afterCommandButtonsVMargin = UI.ScaleHeight(8);
  334. int insetWidth = ClientSize.Width - leftMargin - rightMargin;
  335. if (this.taskImagePB.Image == null)
  336. {
  337. this.taskImagePB.Location = new Point(0, topMargin);
  338. this.taskImagePB.Size = new Size(0, 0);
  339. this.taskImagePB.Visible = false;
  340. }
  341. else
  342. {
  343. this.taskImagePB.Location = new Point(leftMargin, topMargin);
  344. if (this.scaleTaskImageWithDpi)
  345. {
  346. this.taskImagePB.Size = UI.ScaleSize(this.taskImagePB.Image.Size);
  347. }
  348. else
  349. {
  350. this.taskImagePB.Size = this.taskImagePB.Image.Size;
  351. }
  352. this.taskImagePB.Visible = true;
  353. }
  354. this.introTextLabel.Location = new Point(this.taskImagePB.Right + imageToIntroHMargin, this.taskImagePB.Top);
  355. this.introTextLabel.Width = ClientSize.Width - this.introTextLabel.Left - rightMargin;
  356. this.introTextLabel.Height = this.introTextLabel.GetPreferredSize(new Size(this.introTextLabel.Width, 1)).Height;
  357. int y = Math.Max(this.taskImagePB.Bottom, this.introTextLabel.Bottom);
  358. y += topSectionToLinksVMargin;
  359. if (!string.IsNullOrEmpty(this.auxButton.Text))
  360. {
  361. this.auxButton.Visible = true;
  362. this.auxButton.Location = new Point(leftMargin, y);
  363. this.auxButton.PerformLayout();
  364. y += this.auxButton.Height;
  365. y += topSectionToLinksVMargin;
  366. }
  367. else
  368. {
  369. this.auxButton.Visible = false;
  370. }
  371. if (this.commandButtons != null)
  372. {
  373. this.separator.Location = new Point(leftMargin, y);
  374. this.separator.Width = insetWidth;
  375. y += this.separator.Height;
  376. for (int i = 0; i < this.commandButtons.Length; ++i)
  377. {
  378. this.commandButtons[i].Location = new Point(leftMargin, y);
  379. this.commandButtons[i].Width = insetWidth;
  380. this.commandButtons[i].PerformLayout();
  381. y += this.commandButtons[i].Height + commandButtonVMargin;
  382. }
  383. y += afterCommandButtonsVMargin;
  384. }
  385. this.ClientSize = new Size(ClientSize.Width, (this.showCheckbox ? (y + 20) : y));
  386. base.OnLayout(levent);
  387. }
  388. private void InitCommandButtons()
  389. {
  390. SuspendLayout();
  391. if (this.commandButtons != null)
  392. {
  393. foreach (CommandButton commandButton in this.commandButtons)
  394. {
  395. Controls.Remove(commandButton);
  396. commandButton.Tag = null;
  397. commandButton.Click -= CommandButton_Click;
  398. commandButton.Dispose();
  399. }
  400. this.commandButtons = null;
  401. }
  402. this.commandButtons = new CommandButton[this.taskButtons.Length];
  403. IButtonControl newAcceptButton = null;
  404. IButtonControl newCancelButton = null;
  405. for (int i = 0; i < this.commandButtons.Length; ++i)
  406. {
  407. TaskButton taskButton = this.taskButtons[i];
  408. CommandButton commandButton = new CommandButton();
  409. commandButton.ActionText = taskButton.ActionText;
  410. commandButton.ActionImage = taskButton.Image;
  411. commandButton.AutoSize = true;
  412. commandButton.ExplanationText = taskButton.ExplanationText;
  413. commandButton.Tag = taskButton;
  414. commandButton.Click += CommandButton_Click;
  415. this.commandButtons[i] = commandButton;
  416. Controls.Add(commandButton);
  417. if (this.acceptTaskButton == taskButton)
  418. {
  419. newAcceptButton = commandButton;
  420. }
  421. if (this.cancelTaskButton == taskButton)
  422. {
  423. newCancelButton = commandButton;
  424. }
  425. }
  426. AcceptButton = newAcceptButton;
  427. CancelButton = newCancelButton;
  428. if (newAcceptButton != null && newAcceptButton is Control)
  429. {
  430. ((Control)newAcceptButton).Select();
  431. }
  432. ResumeLayout();
  433. }
  434. private void CommandButton_Click(object sender, EventArgs e)
  435. {
  436. CommandButton commandButton = (CommandButton)sender;
  437. this.dialogResult = (TaskButton)commandButton.Tag;
  438. Close();
  439. }
  440. }
  441. }
  442. }