类似QQ消息提示框

From窗体的内容

 

private bool isMouseDown = false;
        private Point FormLocation;     //form的location
        private Point mouseOffset;      //鼠标的按下位置
        private  int xPosition, yPosition;
        //声明委托
        delegate void DisposeForm();
        delegate void SetForm(double value);

        public frmMessageInfo(string strTitle,string strMessageInfo)
        {
            InitializeComponent();
            this.labTitle.Text = strTitle;
            this.labMessageInfo.Text = strMessageInfo;
        }

        private void frmMessageInfo_Load(object sender, EventArgs e)
        {
            //获取消息提示位置
            PosVar.GetNewMessageInfoPostion(out xPosition, out yPosition);
            //int x = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - 262;
            //int y = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height - 152;
            this.SetDesktopLocation(xPosition, yPosition);

        }

        /// <summary>
        /// 关闭窗体效果
        /// </summary>
        private void CloseForm()
        {
            try
            {
                System.Threading.Thread.Sleep(cls.PosVar.INT_WINDOWSHOWTIME);
                double t = 1.0;
                while (true)
                {
                    //改变窗体透明度
                    if (this.InvokeRequired)
                    {
                        SetForm d = delegate(double value)
                        {
                            this.Opacity = value;
                        };
                        this.Invoke(d, new object[1] { t });
                    }
                    else
                    {
                        this.Opacity = t;
                    }
                    t -= 0.05;
                    System.Threading.Thread.Sleep(100);

                    //当窗体透明度为0时,关闭窗体
                    if (this.Opacity == 0.0)
                    {
                        if (this.InvokeRequired)
                        {
                            DisposeForm dForm = delegate()
                            {
                                this.Close();
                            };
                            this.Invoke(dForm);
                        }
                        else
                        {
                            this.Close();
                        }
                    }
                }
            }
            catch { }
        }


        /// <summary>
        /// 鼠标置于关闭按钮时手型光标
        /// </summary>
        private void pbClose_MouseMove(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        /// <summary>
        /// 鼠标离开关闭按钮时箭头光标
        /// </summary>
        private void pbClose_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Arrow;
        }

        /// <summary>
        /// 关闭按钮
        /// </summary>
        private void pbClose_MouseClick(object sender, MouseEventArgs e)
        {
            this.Close();
        }

        private void frmMessageInfo_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Left)
                {
                    isMouseDown = true;
                    FormLocation = this.Location;
                    mouseOffset = Control.MousePosition;
                }
            }
            catch (Exception)
            {
            }
        }

        private void frmMessageInfo_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                int _x = 0;
                int _y = 0;
                if (isMouseDown)
                {
                    Point pt = Control.MousePosition;
                    _x = mouseOffset.X - pt.X;
                    _y = mouseOffset.Y - pt.Y;

                    this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
                }
            }
            catch (Exception)
            {

            }
        }

        private void frmMessageInfo_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                isMouseDown = false;
            }
            catch (Exception)
            {
            }
        }

        private void frmMessageInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            //移除位置
            PosVar.RemoveMessageInfoPostion(xPosition, yPosition);
        }

 

PosVar.cs文件的内容

/// <summary>
        /// 消息提示位置集合
        /// </summary>
        public static Dictionary<string, string> MessageInfoList = new Dictionary<string, string>();

        /// <summary>
        /// 获取消息提示位置
        /// </summary>
        /// <param name="XPostion"></param>
        /// <param name="YPostion"></param>
        public static void GetNewMessageInfoPostion(out int XPostion,out int YPostion)
        {
            XPostion = 0;
            YPostion = 0;
            int width= System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width;
            int height= System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height;
            string key = string.Empty;
            do
            {
                key = (width-262) + ":" + (height-152);
                if (!MessageInfoList.ContainsKey(key))
                {
                    MessageInfoList.Add(key, "MessageInfo");
                    XPostion = width - 262;
                    YPostion = height - 152;
                    return;
                }
                else
                {
                    //高度判断
                    if (height - 162 >= 0)
                    {
                        height = height - 162;
                    }
                    else
                    {
                        //高度超过 则修改宽的位置,高度重新计算
                        height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height;
                        width = width - 267;
                    }
                }

            } while (true);

        }

        /// <summary>
        /// 移除消息提示位置
        /// </summary>
        /// <param name="XPostion"></param>
        /// <param name="YPostion"></param>
        public static void RemoveMessageInfoPostion(int XPostion, int YPostion)
        { 
            string key=XPostion+":"+YPostion;
            if (MessageInfoList.ContainsKey(key))
            {
                MessageInfoList.Remove(key);
            }
        }

调用

public void temp()
        {
            frmMessageInfo messageInfo = new frmMessageInfo("消息提醒", "测试内容!");
            messageInfo.Show();
            messageInfo.btnView.Click += new EventHandler(btnView_Click);
        }

        void btnView_Click(object sender, EventArgs e)
        {
            MessageBox.Show("单击查看");
        }

 

 

效果截图:

如果移除一个消息 ,新增消息会增加到移除的位置上去.

posted @ 2013-01-30 12:00  指尖的流星  Views(562)  Comments(0)    收藏  举报