MessageBoxForm.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. this.Show();
  54. }
  55. /// <summary>
  56. /// 显示模态窗口,默认不进行倒计时,如果为true,时间默认5秒
  57. /// </summary>
  58. /// <param name="text"></param>
  59. /// <param name="count_down"></param>
  60. /// <param name="time"></param>
  61. public void ShowDialog(string text, bool is_count_down = true, int time = 5)
  62. {
  63. this.msgBox.Text = text;
  64. if (is_count_down)
  65. {
  66. count_down(time);
  67. }
  68. this.ShowDialog();
  69. }
  70. //在这里关闭窗口就会报创建窗口句柄msgTextNew != msgBox.Text错误
  71. private void OKbtn_Click(object sender, EventArgs e)
  72. {
  73. this.Close();
  74. }
  75. /// <summary>
  76. /// 倒计时
  77. /// </summary>
  78. /// <param name="time"></param>
  79. private void count_down(int time)
  80. {
  81. Task.Factory.StartNew(() =>
  82. {
  83. try
  84. {
  85. int i = Convert.ToInt32(time_label.Text);
  86. while (i >= 0)
  87. {
  88. if (this.InvokeRequired)
  89. {
  90. this.Invoke(new Action(() =>
  91. {
  92. time_label.Text = i.ToString();
  93. if (i <= 0)
  94. this.Close();
  95. }));
  96. }
  97. else
  98. {
  99. time_label.Text = i.ToString();
  100. if (i <= 0)
  101. this.Close();
  102. }
  103. i--;
  104. Thread.Sleep(1000);
  105. }
  106. }
  107. catch (Exception e)
  108. { }
  109. });
  110. }
  111. }
  112. }