MessageBoxForm.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. namespace chutian_parking_terminal
  8. {
  9. public partial class MessageBoxForm : Form
  10. {
  11. [DllImport("user32.dll")]
  12. public static extern bool ReleaseCapture();
  13. [DllImport("user32.dll")]
  14. public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  15. protected override void OnMouseMove(MouseEventArgs e)
  16. {
  17. base.OnMouseMove(e);
  18. if (e.Button == MouseButtons.Left)
  19. {
  20. //这里一定要判断鼠标左键按下状态,否则会出现一个很奇葩的BUG,不信邪可以试一下~~
  21. ReleaseCapture();
  22. SendMessage(Handle, 0x00A1, 2, 0);
  23. }
  24. }
  25. public MessageBoxForm()//弹窗初始化
  26. {
  27. InitializeComponent();
  28. }
  29. public void setCenter()//设置位置
  30. {
  31. int gLeft = this.Width / 2 - msgBox.Width / 2;
  32. int gTop = (this.Height / 2 - msgBox.Height / 2) - 20;
  33. msgBox.Location = new Point(gLeft, gTop);
  34. }
  35. public void setText(string text)//设置文本
  36. {
  37. this.msgBox.Text = text;
  38. }
  39. /// <summary>
  40. /// 显示窗口,默认为倒计时,时间5秒
  41. /// </summary>
  42. /// <param name="text"></param>
  43. /// <param name="count_down"></param>
  44. /// <param name="time"></param>
  45. public void Show(string text, bool is_count_down = true, int time = 5)
  46. {
  47. this.msgBox.Text = text;
  48. this.time_label.Text = time.ToString();
  49. if (is_count_down)
  50. {
  51. count_down(time);
  52. }
  53. else
  54. {
  55. this.time_label.Visible = false;
  56. }
  57. this.Show();
  58. }
  59. /// <summary>
  60. /// 显示模态窗口,默认不进行倒计时,如果为true,时间默认5秒
  61. /// </summary>
  62. /// <param name="text"></param>
  63. /// <param name="count_down"></param>
  64. /// <param name="time"></param>
  65. public void ShowDialog(string text, bool is_count_down = true, int time = 5)
  66. {
  67. this.msgBox.Text = text;
  68. if (is_count_down)
  69. {
  70. count_down(time);
  71. }
  72. else
  73. {
  74. this.time_label.Visible = false;
  75. }
  76. this.ShowDialog();
  77. }
  78. //在这里关闭窗口就会报创建窗口句柄msgTextNew != msgBox.Text错误
  79. private void OKbtn_Click(object sender, EventArgs e)
  80. {
  81. this.Close();
  82. }
  83. /// <summary>
  84. /// 倒计时
  85. /// </summary>
  86. /// <param name="time"></param>
  87. private void count_down(int time)
  88. {
  89. Task.Factory.StartNew(() =>
  90. {
  91. try
  92. {
  93. int i = Convert.ToInt32(time_label.Text);
  94. while (i >= 0)
  95. {
  96. if (this.InvokeRequired)
  97. {
  98. this.Invoke(new Action(() =>
  99. {
  100. time_label.Text = i.ToString();
  101. if (i <= 0)
  102. this.Close();
  103. }));
  104. }
  105. else
  106. {
  107. time_label.Text = i.ToString();
  108. if (i <= 0)
  109. this.Close();
  110. }
  111. i--;
  112. Thread.Sleep(1000);
  113. }
  114. }
  115. catch (Exception e)
  116. { }
  117. });
  118. }
  119. }
  120. }