123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using System;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace chutian_parking_terminal
- {
- public partial class MessageBoxForm : Form
- {
- [DllImport("user32.dll")]
- public static extern bool ReleaseCapture();
- [DllImport("user32.dll")]
- public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
- if (e.Button == MouseButtons.Left)
- {
- //这里一定要判断鼠标左键按下状态,否则会出现一个很奇葩的BUG,不信邪可以试一下~~
- ReleaseCapture();
- SendMessage(Handle, 0x00A1, 2, 0);
- }
- }
- public MessageBoxForm()//弹窗初始化
- {
- InitializeComponent();
- }
- public void setCenter()//设置位置
- {
- int gLeft = this.Width / 2 - msgBox.Width / 2;
- int gTop = (this.Height / 2 - msgBox.Height / 2) - 20;
- msgBox.Location = new Point(gLeft, gTop);
- }
- public void setText(string text)//设置文本
- {
- this.msgBox.Text = text;
- }
- /// <summary>
- /// 显示窗口,默认为倒计时,时间5秒
- /// </summary>
- /// <param name="text"></param>
- /// <param name="count_down"></param>
- /// <param name="time"></param>
- public void Show(string text, bool is_count_down = true, int time = 5)
- {
- this.msgBox.Text = text;
- this.time_label.Text = time.ToString();
- if (is_count_down)
- {
- count_down(time);
- }
- this.Show();
- }
- /// <summary>
- /// 显示模态窗口,默认不进行倒计时,如果为true,时间默认5秒
- /// </summary>
- /// <param name="text"></param>
- /// <param name="count_down"></param>
- /// <param name="time"></param>
- public void ShowDialog(string text, bool is_count_down = true, int time = 5)
- {
- this.msgBox.Text = text;
- if (is_count_down)
- {
- count_down(time);
- }
- this.ShowDialog();
- }
- //在这里关闭窗口就会报创建窗口句柄msgTextNew != msgBox.Text错误
- private void OKbtn_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- /// <summary>
- /// 倒计时
- /// </summary>
- /// <param name="time"></param>
- private void count_down(int time)
- {
- Task.Factory.StartNew(() =>
- {
- try
- {
- int i = Convert.ToInt32(time_label.Text);
- while (i >= 0)
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new Action(() =>
- {
- time_label.Text = i.ToString();
- if (i <= 0)
- this.Close();
- }));
- }
- else
- {
- time_label.Text = i.ToString();
- if (i <= 0)
- this.Close();
- }
- i--;
- Thread.Sleep(1000);
- }
- }
- catch (Exception e)
- { }
- });
- }
- }
- }
|