using System; using System.Drawing; using System.Windows.Forms; using System.Threading; using GDNXFD.Data; namespace IntelligentControlForsx { public partial class MiniForm : UserControl { private BigForm2 bigForm = null; private Thread monitorAlarmThread = null; private System.Drawing.Point mouseOffset; private ToolStripMenuItem currentOpacityItem = null; public MiniFormLocation miniFormLocation; private bool isMouseDown = false; public bool isMouseEnter = false; public int miniBigFormSpace = 5; public int miniFormWidth = 371; public int miniFormHeight = 94; private int maxRank = 0; private int preMaxRank = 0; private int totalAlarms = 0; /*移动时小球出现在bigForm窗体的位置方向枚举*/ public enum MiniFormLocation { topLeft, topRigh, bottomLeft, bottomRight } public MiniForm() { // Control.CheckForIllegalCrossThreadCalls = false; InitializeComponent(); StartAlarmMonitor(); //启动报警监视 } #region 报警监视 /*开始监视报警数据*/ private void StartAlarmMonitor() { AlertBackWorker.Instance.Start(); refreshTimer.Start(); } private void refreshTimer_Tick(object sender, EventArgs e) { alarmMonitor(); } private void alarmMonitor() { string thingName = ""; string alarmText = ""; DateTime? alarmDate = null; Color? color = null; int rank = 0; int total = 0; if (ClientCache.Instance.AlertSnaps != null && ClientCache.Instance.AlertSnaps.Count > 0) { AlertSnap snap = ClientCache.Instance.AlertSnaps[0]; thingName = snap.ObjectName; alarmText = snap.AlertText; alarmDate = snap.LastUpdateTime; color = snap.getBackGroundColor(); int.TryParse(snap.Rank, out rank); total = ClientCache.Instance.AlertSnaps.Count; } string timeString = alarmDate.HasValue ? alarmDate.Value.ToString("HH:mm:ss") : "-"; lblAlarmTime.Text = timeString; lblObjectName.Text = thingName; lblAlarmText.Text = alarmText; lblAlarmTime.BackColor = color.HasValue ? color.Value : Color.LightBlue; lblObjectName.BackColor = color.HasValue ? color.Value : Color.LightBlue; lblAlarmText.BackColor = color.HasValue ? color.Value : Color.LightBlue; maxRank = rank; totalAlarms = total; paintMiniBallControl(); } /*刷新报警紧急程度*/ private void paintMiniBallControl() { if (totalAlarms < 10) { // lblAlarmCount.Location = new System.Drawing.Point(33, 29); } else if (totalAlarms < 100) { // lblAlarmCount.Location = new System.Drawing.Point(29, 29); } else if (totalAlarms < 1000) { //lblAlarmCount.Location = new System.Drawing.Point(24, 29); } else if (totalAlarms < 10000) { //lblAlarmCount.Location = new System.Drawing.Point(19, 29); } // lblAlarmCount.Text = totalAlarms + ""; if (preMaxRank != maxRank) { preMaxRank = maxRank; //if (maxRank == 5) //{ // this.pnlBall.BackgroundImage = global::AlarmUI.Properties.Resources.红色球; //} //else if (maxRank == 4) //{ // this.pnlBall.BackgroundImage = global::AlarmUI.Properties.Resources.橙色球; //} //else //{ // this.pnlBall.BackgroundImage = global::AlarmUI.Properties.Resources.绿色球; //} } } #endregion #region 小球的鼠标事件 private void miniBigFormSpace_MouseEnter(object sender, EventArgs e) { isMouseEnter = true; if (bigForm == null || !bigForm.Visible) showDetailFormTimer.Enabled = true; } private void miniBigFormSpace_MouseLeave(object sender, EventArgs e) { System.Drawing.Point p = MousePosition; if (p.X - 10 <= this.Left || p.X + 10 >= this.Left + miniFormWidth || p.Y - 10 <= this.Top || p.Y + 10 >= this.Bottom) { isMouseEnter = false; hideDetailFormTimer.Enabled = true; } } private void miniBigFormSpace_MouseDown(object sender, MouseEventArgs e) { //if (e.Button == MouseButtons.Left) //{ // isMouseDown = true; // mouseOffset = new System.Drawing.Point(MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y); // this.Cursor = Cursors.SizeAll; //} } private void miniBigFormSpace_MouseUp(object sender, MouseEventArgs e) { isMouseDown = false; this.Cursor = Cursors.Default; } private void miniBigFormSpace_MouseMove(object sender, MouseEventArgs e) { //if (isMouseDown == true) //{ // System.Drawing.Point old = this.Location; // this.Location = getMiniBallMoveLocation(); // if (old.X != this.Location.X || old.Y != this.Location.Y) // { // if (bigForm != null && bigForm.Visible) // hideDetailsForm(); // } // else // { // if (bigForm != null && !bigForm.Visible) // { // isMouseEnter = true; // showDetailFormTimer.Enabled = true; // } // } //} } #endregion #region 小球和bigForm的位置方法 /*小球出现的位置*/ private System.Drawing.Point getMiniBallMoveLocation() { int x = MousePosition.X - mouseOffset.X; int y = MousePosition.Y - mouseOffset.Y; if (x < 0) { x = 0; } if (y < 0) { y = 0; } if (Screen.PrimaryScreen.WorkingArea.Width - x < miniFormWidth) { x = Screen.PrimaryScreen.WorkingArea.Width - miniFormWidth; } if (Screen.PrimaryScreen.WorkingArea.Height - y < miniFormHeight) { y = Screen.PrimaryScreen.WorkingArea.Height - miniFormHeight; } return new System.Drawing.Point(x, y); } /*获取bigForm出现的位置*/ private System.Drawing.Point getDetailsFormLocation() { int x = 0, y = 0; //System.Drawing.Point miniBallLocation = this.Location; //if (this.Location.Y >= bigForm.Height) //minBall在bigBall下面 //{ // if (Screen.PrimaryScreen.WorkingArea.Width - this.Location.X <= bigForm.Width) // { // x = this.Location.X + miniFormWidth - bigForm.Width; // miniFormLocation = MiniFormLocation.bottomRight; // } // else // { // x = this.Location.X; // miniFormLocation = MiniFormLocation.bottomLeft; // } // y = this.Location.Y - bigForm.Height - miniBigFormSpace; //} //else if (this.Location.Y < bigForm.Height) //minBall在bigBall上面 //{ // if (Screen.PrimaryScreen.WorkingArea.Width - this.Location.X > bigForm.Width) // { // x = this.Location.X; // miniFormLocation = MiniFormLocation.topLeft; // } // else // { // x = this.Location.X + miniFormWidth - bigForm.Width; // miniFormLocation = MiniFormLocation.topRigh; // } // y = this.Location.Y + miniFormHeight + miniBigFormSpace; //} x = 1920 - bigForm.Width; y = 1038 - bigForm.Height; return new System.Drawing.Point(x, y); } #endregion #region 显示和隐藏detailForm的方法和定时器 /*隐藏bigForm*/ private void hideDetailsForm() { if (bigForm != null && bigForm.Visible) { bigForm.Hide(); } } /*显示bigForm*/ private void showDetailsForm() { if (bigForm == null) { bigForm = new BigForm2(this); bigForm.Show(); bigForm.Location = getDetailsFormLocation(); } else if (!bigForm.Visible) { bigForm.Location = getDetailsFormLocation(); bigForm.Show(); } bigForm.refreshThread(); } /*显示bigForm的定时器*/ private void showDetailFormTimer_Tick(object sender, EventArgs e) { if (isMouseEnter) { showDetailsForm(); } showDetailFormTimer.Enabled = false; } /*隐藏bigForm的定时器*/ private void hideDetailFormTimer_Tick(object sender, EventArgs e) { hideDetailFormTimer.Enabled = false; if (bigForm != null && bigForm.Visible && !bigForm.isMouseEnter && !isMouseEnter) { hideDetailsForm(); } if (bigForm != null && bigForm.isMouseEnter) isMouseEnter = false; } #endregion } }