创建特殊形状的窗体

本文介绍了如何在界面设计中创建不规则形状的窗体,包括椭圆形和多边形窗体。通过使用GraphicsPath和窗体的Paint事件,可以实现独特的窗体外观。此外,还提供了解决窗体移动的鼠标控制代码,增强用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         有时进行界面创作,为了达到个性鲜明标新立异的效果,往往会想到建立不同形状不同外观的窗体。
   下面就介绍两种建立不规则窗体的方法。
   首先在命名空间引用using System.Drawing.Drawing2D。
   以下建立椭圆形窗体:
   添加窗体Paint事件
   private void Form1_Paint(object sender, PaintEventArgs e)
        {
            GraphicsPath creatnewform = new GraphicsPath();
            creatnewform.AddEllipse(10, 50, this.Width - 80, this.Height - 100);  //创建椭圆形区域
            this.Region = new Region(creatnewform);
        }
    运行后即生成一个无边缘的纯椭圆形的窗体。为方便窗体的关闭,可以设置一个button作为关闭按钮。
 同时,为了增加窗体的外观颜色等,可以设置相应的窗体背景,使其美观。
 
    建立多边形窗体:
    同样添加Paint事件
    private void Form1_Paint(object sender, PaintEventArgs e)
        {   //建立5边形窗体
            GraphicsPath creatnewform = new GraphicsPath();
            Point[] myform={new Point(230,200),
        new Point(400,100),
        new Point(570,200),
        new Point(500,400),
        new Point(300,400),}
   creatnewform.AddPloygon(Myform);   //多边形函数
   this.Region=new Region(creatnewform);
        }
 
    为了使窗体可以受鼠标控制移动,则需要添加如下事件代码:
     private Point MousePos;   //纪录鼠标指针的坐标
        private bool bMouseDown = false;  //纪录鼠标左键是否按下

        private void Form1_MouseDown(object sender, MouseEventArgs e)//鼠标键按下
        {
            if (e.Button == MouseButtons.Left)
            {  
                MousePos.X = -e.X - SystemInformation.FrameBorderSize.Width;
                MousePos.Y = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
                bMouseDown = true;
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e) //鼠标移动
        {
            if (bMouseDown)
            {
                Point CurrentPos = Control.MousePosition;
                CurrentPos.Offset(MousePos.X, MousePos.Y);
                Location = CurrentPos;  //当前位置
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e) //鼠标弹起
        {
            if (e.Button == MouseButtons.Left)
            {
                bMouseDown = false;
            }
        }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值