注册式不规则窗体完整代码。

在网上有一些注册式不规则窗体的代码,代码却不完整,因些本人最近也需要这种窗体的应用,本来也想在网上找,有一篇类似的文章,但作者没有公布完整的代码,因此为了需要并成功的写成了,在这里完整公布代码。希望能为需要的人提供帮助,若有不足,希望各位多多指教。若转载请指明出处。

 

特别说明:

1、调试.Properties.Resources.Close2、调试.Properties.Resources.Close3; 表示资源图片,用户需要将图片添加到项目中,并将代码改过来。Close2表示关闭按钮失去焦点时的图片(常规图片),Close3表示关闭按钮得到焦点时的图片。

2、调用:

           MsgBox msg = new MsgBox();
            msg.Show(button3, "本控件是一个示例,你会用了吗?");  //button3表示控件对象

3、在这里分两个文件(MsgBox.Designer.cs和MsgBox.cs)

4、添加一个名为MsgBox的窗体。

5、该代码是在C#2005中编写。

 

MsgBox.Designer.cs 文件

partial class MsgBox
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.picClose = new System.Windows.Forms.PictureBox();
            this.LabInfo = new System.Windows.Forms.Label();
            this.timerAutClose = new System.Windows.Forms.Timer(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.picClose)).BeginInit();
            this.SuspendLayout();
            //
            // picClose
            //
            this.picClose.BackgroundImage = global::调试.Properties.Resources.Close2;
            this.picClose.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.picClose.Location = new System.Drawing.Point(145, 17);
            this.picClose.Name = "picClose";
            this.picClose.Size = new System.Drawing.Size(15, 15);
            this.picClose.TabIndex = 1;
            this.picClose.TabStop = false;
            this.picClose.MouseDown += new System.Windows.Forms.MouseEventHandler(this.picClose_MouseDown);
            this.picClose.MouseHover += new System.EventHandler(this.picClose_MouseHover);
            this.picClose.MouseEnter += new System.EventHandler(this.picClose_MouseEnter);
            //
            // LabInfo
            //
            this.LabInfo.Location = new System.Drawing.Point(12, 17);
            this.LabInfo.Name = "LabInfo";
            this.LabInfo.Size = new System.Drawing.Size(106, 35);
            this.LabInfo.TabIndex = 2;
            this.LabInfo.Text = "提示消息";
            this.LabInfo.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            //
            // timerAutClose
            //
            this.timerAutClose.Interval = 1000;
            this.timerAutClose.Tick += new System.EventHandler(this.timerAutClose_Tick);
            //
            // MsgBox
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.ClientSize = new System.Drawing.Size(272, 122);
            this.Controls.Add(this.LabInfo);
            this.Controls.Add(this.picClose);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "MsgBox";
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "MsgBox";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.MsgBox_Paint);
            this.Load += new System.EventHandler(this.MsgBox_Load);
            ((System.ComponentModel.ISupportInitialize)(this.picClose)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PictureBox picClose;
        private System.Windows.Forms.Label LabInfo;
        private System.Windows.Forms.Timer timerAutClose;


    }

 

 

MsgBox.cs 文件

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

//引用空间

 

    /// <summary>
    /// 注册式提示窗口
    /// </summary>
    public partial class MsgBox : Form
    {

        private const int WM_ACTIVATEAPP = 0x001C;
        private GraphicsPath gPath = new GraphicsPath();
        private InfoLocation GetInfoLocation = InfoLocation.BottomLeft;
        private int ToLeft = 10;  //定位左边距离
        private int iCount = 0;

        /// <summary>
        /// 显示定位
        /// </summary>
        private enum InfoLocation
        {
            /// <summary>
            /// 上左边
            /// </summary>
            TopLeft,
            /// <summary>
            /// 上右边
            /// </summary>
            TopRight,
            /// <summary>
            /// 下左边
            /// </summary>
            BottomLeft,
            /// <summary>
            /// 下右边
            /// </summary>
            BottomRight
        }
        /// <summary>
        /// 创建一个注册式提示窗口
        /// </summary>
        public MsgBox()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            this.StartPosition = FormStartPosition.Manual;
            this.TopMost = true;
            this.ShowInTaskbar = false;
            this.LabInfo.Text = "";
            timerAutClose.Stop();
        }

        private void MsgBox_Load(object sender, EventArgs e)
        {
            iCount = 0;
            timerAutClose.Enabled = true;
            timerAutClose.Start();
        }

        /// <summary>
        /// 打开注册式提示窗口
        /// </summary>
        /// <param name="Ctl">要提示消息控件</param>
        /// <param name="Text">提示内容</param>
        public virtual void Show(System.Windows.Forms.Control Ctl, string Text)
        {
            //取得控件在屏幕的位置
            Rectangle rect = Ctl.RectangleToScreen(new Rectangle(0, 0, Ctl.Width, Ctl.Height));
            //屏幕大小
            Rectangle ScreenRect = Screen.PrimaryScreen.WorkingArea;
            this.LabInfo.Text = Text;
            if (ScreenRect.Width - this.Width - (Ctl.Width / 2) > rect.X)
            {
                if (ScreenRect.Height - this.Height > rect.Y)
                {
                    GetInfoLocation = InfoLocation.TopLeft;
                    this.Left = rect.X + (Ctl.Width / 2);
                    this.Top = rect.Y + Ctl.Height;
                    ToLeft = rect.X + Ctl.Width - this.Left;
                }
                else
                {
                    GetInfoLocation = InfoLocation.BottomLeft;
                    this.Left = rect.X + (Ctl.Width / 2);
                    this.Top = rect.Y - this.Height;
                    ToLeft = rect.X + Ctl.Width - this.Left;

                }
            }
            else
            {
                if (ScreenRect.Height - this.Height > rect.Y)
                {
                    GetInfoLocation = InfoLocation.TopRight;
                    this.Left = rect.X + Ctl.Width - this.Width;
                    this.Top = rect.Y + Ctl.Height;
                    ToLeft = this.Width;
                }
                else
                {
                    GetInfoLocation = InfoLocation.BottomRight;
                    this.Left = (rect.X + Ctl.Width) - this.Width;
                    this.Top = rect.Y - Ctl.Height - this.Height;
                    ToLeft = this.Width;
                }
            }
            Ctl.FindForm().Move += new EventHandler(MsgBox_Move);
            Ctl.FindForm().FormClosed += new FormClosedEventHandler(MsgBox_FormClosed);
            this.Show();           
            Ctl.FindForm().Activate();
        }

        void MsgBox_FormClosed(object sender, FormClosedEventArgs e)
        {
            timerAutClose.Enabled = false;
            timerAutClose.Stop();
            this.Close();
        }

        void MsgBox_Move(object sender, EventArgs e)
        {
            timerAutClose.Enabled = false;
            timerAutClose.Stop();
            this.Close();
        }

        /// <summary>
        /// 获取控件位置
        /// </summary>
        /// <param name="InfoControl">弹出的信息控件</param>
        /// <returns></returns>
        private Point GetControlPoint(Control InfoControl)
        {
            if (InfoControl == null)
                throw new ArgumentException();
            Point CurrcontrolLocation = InfoControl.Location;
            System.Drawing.Size controlSize = InfoControl.Size;
            if (InfoControl.Parent != null)
                CurrcontrolLocation = InfoControl.Parent.PointToScreen(CurrcontrolLocation);
            return CurrcontrolLocation + new Size(controlSize.Width, controlSize.Height);
        }

        /// <summary>
        /// 设置显示信息
        /// </summary>
        private void SetShowInfo()
        {
            gPath = new GraphicsPath();
            int TempSize = 30;
            int TempAdjustment = 3;

            int ShowHeight = 30;
            int ShowLeftSize = 0;


            int SpWidth = 20;  //分隔宽度

            switch (GetInfoLocation)
            {
                case InfoLocation.TopLeft:
                    ShowLeftSize = this.Width / 4;
                    gPath.AddArc(new Rectangle(0, ShowHeight, TempSize, TempSize), 180, 90);
                    gPath.AddLine(new Point(TempSize, ShowHeight), new Point(ShowLeftSize, ShowHeight));
                    gPath.AddLine(new Point(ShowLeftSize, ShowHeight), new Point(ToLeft, 0));
                    gPath.AddLine(new Point(ToLeft, 0), new Point(ShowLeftSize + SpWidth, ShowHeight));
                    gPath.AddLine(new Point(ShowLeftSize + SpWidth, ShowHeight), new Point(this.Width - TempSize, ShowHeight));
                    gPath.AddArc(new Rectangle(this.Width - TempSize - TempAdjustment, ShowHeight, TempSize, TempSize), 270, 90);
                    gPath.AddLine(new Point(this.Width - TempAdjustment, TempSize + ShowHeight), new Point(this.Width - TempAdjustment, this.Height - TempSize));
                    gPath.AddArc(new Rectangle(this.Width - TempSize - TempAdjustment, this.Height - TempSize - TempAdjustment, TempSize, TempSize), 0, 90);
                    gPath.AddLine(new Point(this.Width - TempSize, this.Height - TempAdjustment), new Point(TempSize, this.Height - TempAdjustment));
                    gPath.AddArc(new Rectangle(1, this.Height - TempSize - TempAdjustment, TempSize, TempSize), 90, 90);
                    gPath.AddLine(new Point(1, this.Height - TempSize), new Point(1, ShowHeight + 15));

                    this.picClose.Left = this.Width - this.picClose.Width - 7;
                    this.picClose.Top = ShowHeight + 3;
                    this.LabInfo.Left = 5;
                    this.LabInfo.Top = ShowHeight + 10;
                    this.LabInfo.Width = this.Width - 30;
                    this.LabInfo.Height = this.Height - ShowHeight - 20;


                    break;
                case InfoLocation.TopRight:
                    ShowLeftSize = this.Width / 4 * 3;
                    gPath.AddArc(new Rectangle(0, ShowHeight, TempSize, TempSize), 180, 90);
                    gPath.AddLine(new Point(TempSize, ShowHeight), new Point(ShowLeftSize, ShowHeight));
                    gPath.AddLine(new Point(ShowLeftSize, ShowHeight), new Point(ToLeft, 0));
                    gPath.AddLine(new Point(ToLeft, 0), new Point(ShowLeftSize + SpWidth, ShowHeight));
                    gPath.AddLine(new Point(ShowLeftSize + SpWidth, ShowHeight), new Point(this.Width - TempSize, ShowHeight));
                    gPath.AddArc(new Rectangle(this.Width - TempSize - TempAdjustment, ShowHeight, TempSize, TempSize), 270, 90);
                    gPath.AddLine(new Point(this.Width - TempAdjustment, TempSize + ShowHeight), new Point(this.Width - TempAdjustment, this.Height - TempSize));
                    gPath.AddArc(new Rectangle(this.Width - TempSize - TempAdjustment, this.Height - TempSize - TempAdjustment, TempSize, TempSize), 0, 90);
                    gPath.AddLine(new Point(this.Width - TempSize, this.Height - TempAdjustment), new Point(TempSize, this.Height - TempAdjustment));
                    gPath.AddArc(new Rectangle(1, this.Height - TempSize - TempAdjustment, TempSize, TempSize), 90, 90);
                    gPath.AddLine(new Point(1, this.Height - TempSize), new Point(1, ShowHeight + 15));

                    this.picClose.Left = this.Width - this.picClose.Width - 7;
                    this.picClose.Top = ShowHeight + 3;
                    this.LabInfo.Left = 5;
                    this.LabInfo.Top = ShowHeight + 10;
                    this.LabInfo.Width = this.Width - 30;
                    this.LabInfo.Height = this.Height - ShowHeight - 20;

                    break;
                case InfoLocation.BottomLeft:
                    ShowLeftSize = this.Width / 4;
                    gPath.AddArc(new Rectangle(1, 1, TempSize, TempSize), 180, 90);
                    gPath.AddLine(new Point(TempSize, 1), new Point(this.Width - TempSize, 1));
                    gPath.AddArc(new Rectangle(this.Width - TempSize - TempAdjustment, 1, TempSize, TempSize), 270, 90);
                    gPath.AddLine(new Point(this.Width - TempAdjustment, TempSize), new Point(this.Width - TempAdjustment, this.Height - TempSize - ShowHeight));
                    gPath.AddArc(new Rectangle(this.Width - TempSize - TempAdjustment, this.Height - TempSize - TempAdjustment - ShowHeight, TempSize, TempSize), 0, 90);
                    gPath.AddLine(new Point(this.Width - TempSize, this.Height - TempAdjustment - ShowHeight), new Point(ShowLeftSize, this.Height - TempAdjustment - ShowHeight));
                    gPath.AddLine(new Point(ShowLeftSize, this.Height - TempAdjustment - ShowHeight), new Point(ToLeft, this.Height));
                    gPath.AddLine(new Point(ToLeft, this.Height), new Point(ShowLeftSize - SpWidth, this.Height - TempAdjustment - ShowHeight));
                    gPath.AddLine(new Point(ShowLeftSize - SpWidth, this.Height - TempAdjustment - ShowHeight), new Point(TempSize, this.Height - TempAdjustment - ShowHeight));
                    gPath.AddArc(new Rectangle(1, this.Height - TempAdjustment - TempSize - ShowHeight, TempSize, TempSize), 90, 90);
                    gPath.AddLine(new Point(1, this.Height - TempAdjustment - TempSize - ShowHeight), new Point(1, 15));

                    this.picClose.Left = this.Width - this.picClose.Width - 7;
                    this.picClose.Top = 4;
                    this.LabInfo.Left = 5;
                    this.LabInfo.Top = 9;
                    this.LabInfo.Width = this.Width - 30;
                    this.LabInfo.Height = this.Height - ShowHeight - 20;

                    break;
                case InfoLocation.BottomRight:
                    ShowLeftSize = this.Width / 4 * 3;
                    gPath.AddArc(new Rectangle(1, 1, TempSize, TempSize), 180, 90);
                    gPath.AddLine(new Point(TempSize, 1), new Point(this.Width - TempSize, 1));
                    gPath.AddArc(new Rectangle(this.Width - TempSize - TempAdjustment, 1, TempSize, TempSize), 270, 90);
                    gPath.AddLine(new Point(this.Width - TempAdjustment, TempSize), new Point(this.Width - TempAdjustment, this.Height - TempSize - ShowHeight));
                    gPath.AddArc(new Rectangle(this.Width - TempSize - TempAdjustment, this.Height - TempSize - TempAdjustment - ShowHeight, TempSize, TempSize), 0, 90);
                    gPath.AddLine(new Point(this.Width - TempSize, this.Height - TempAdjustment - ShowHeight), new Point(ShowLeftSize, this.Height - TempAdjustment - ShowHeight));
                    gPath.AddLine(new Point(ShowLeftSize, this.Height - TempAdjustment - ShowHeight), new Point(ToLeft, this.Height));
                    gPath.AddLine(new Point(ToLeft, this.Height), new Point(ShowLeftSize - SpWidth, this.Height - TempAdjustment - ShowHeight));
                    gPath.AddLine(new Point(ShowLeftSize - SpWidth, this.Height - TempAdjustment - ShowHeight), new Point(TempSize, this.Height - TempAdjustment - ShowHeight));
                    gPath.AddArc(new Rectangle(1, this.Height - TempAdjustment - TempSize - ShowHeight, TempSize, TempSize), 90, 90);
                    gPath.AddLine(new Point(1, this.Height - TempAdjustment - TempSize - ShowHeight), new Point(1, 15));

                    this.picClose.Left = this.Width - this.picClose.Width - 7;
                    this.picClose.Top = 4;
                    this.LabInfo.Left = 5;
                    this.LabInfo.Top = 9;
                    this.LabInfo.Width = this.Width - 30;
                    this.LabInfo.Height = this.Height - ShowHeight - 20;

                    break;
            }

            gPath.CloseFigure();
        }

        /// <summary>
        /// 绘制不规则窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MsgBox_Paint(object sender, PaintEventArgs e)
        {
            SetShowInfo();
            Graphics g;
            g = e.Graphics;
            g.DrawPath(new Pen(Color.Black, 2), gPath);
            SolidBrush brush = new SolidBrush(Color.OldLace);
            g.FillPath(brush, gPath);
            this.LabInfo.BackColor = Color.OldLace;
            this.picClose.BackColor = Color.OldLace;
            this.Region = new Region(gPath);
            Pen p = new Pen(Color.Black, 2);
            e.Graphics.DrawPath(p, gPath);
        }

        private void picClose_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                timerAutClose.Enabled = false;
                timerAutClose.Stop();
                this.Close();
            }
        }

        private void picClose_MouseEnter(object sender, EventArgs e)
        {
            this.picClose.BackgroundImage = global::调试.Properties.Resources.Close3;
        }

        private void picClose_MouseHover(object sender, EventArgs e)
        {
            this.picClose.BackgroundImage = global::调试.Properties.Resources.Close2;
        }

        private void timerAutClose_Tick(object sender, EventArgs e)
        {
            iCount++;
            if (iCount >= 3)
            {
                timerAutClose.Enabled = false;
                timerAutClose.Stop();
                this.Close();
            }
        }

        /// <summary>
        /// 重写 WndProc
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            //(3)如果整个程序失去就隐藏
            if (m.Msg == WM_ACTIVATEAPP && m.WParam == IntPtr.Zero)
            {
                timerAutClose.Enabled = false;
                timerAutClose.Stop();
                this.Close();
            }

            base.WndProc(ref m);
        }

    }

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值