不规则窗体和控件的创建

看了很多关于不规则窗体的创建的文章,发现了一个现象。那就是必须在24bit下运行。但是在现实中我们不可能告诉客户我们的程序必须运行在24bit模式下。因此我在网上查看了好多的资料,并且在自己的实践下实现在正常颜色模式下得不规则窗体的处理。下面将简单进行一下说明,希望对您有帮助。

 

命名空间:using System.Drawing;
                  using System.Drawing.Drawing2D;
                  using System.Windows.Forms;

 

代码实例:

        //创建不规则窗体的方法 参数1 :窗体或者控件  参数2: Bit格式的图片

        public static void CreateControlRegion(Control control, Bitmap bitmap)
        {

            //控件或图片不存在的场合 返回
            if (control == null || bitmap == null)
                return;

            //获得图片的宽度 设置给控件

            control.Width = bitmap.Width;

            //获得图片的高度 设置给控件
            control.Height = bitmap.Height;

            //窗体的情况下 (还可以继续增加其他的控件)
            if (control is System.Windows.Forms.Form)
            {

                //窗体的设置
                Form form = (Form)control;
                form.Width = control.Width;
                form.Height = control.Height;
                form.FormBorderStyle = FormBorderStyle.None;

                //窗体背景图片的设置
                form.BackgroundImage = bitmap;

                //对窗体进行重画
                GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);

                //窗体的装入
                form.Region = new Region(graphicsPath);
            }

            //按钮的情况下
            else if (control is System.Windows.Forms.Button)
            {
                Button button = (Button)control;
                button.Text = string.Empty;
                button.Cursor = Cursors.Hand;
                button.BackgroundImage = bitmap;
                GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);
                button.Region = new Region(graphicsPath);
            }
        }

 

        private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)
        {
            GraphicsPath graphicsPath = new GraphicsPath();

            //获取第一个点的原始颜色。在该图中这个颜色将被替换
            Color colorTransparent = bitmap.GetPixel(0, 0);

            //列像素位置的纪录
            int colOpaquePixel = 0;

            //循环横像素
            for (int row = 0; row < bitmap.Height; row++)
            {
                colOpaquePixel = 0;

                //循环列像素
                for(int col = 0 ; col < bitmap.Width ; col++)
                {

                    //如果当前像素颜色不等于原始颜色的情况下
                    if (bitmap.GetPixel(col, row) != colorTransparent)
                    {

                        //生成当前像素点
                        colOpaquePixel = col;
                        int colNext = col;
                        graphicsPath.AddRectangle(new Rectangle(col, row, 1, 1));
                    }
                }
            }
            return graphicsPath;
        }

 

窗体的移动处理:

        //鼠标点纪录

        private Point mouseOffset;

        //鼠标点击状态纪录
        private bool isMouseDown = false;

        //鼠标按下当前窗体位置记录

         private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            int xOffset;
            int yOffset;
            if (e.Button == MouseButtons.Left)
            {
                xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
                yOffset = -e.Y - SystemInformation.CaptionHeight -
                SystemInformation.FrameBorderSize.Height;
                mouseOffset = new Point(xOffset, yOffset);
                isMouseDown = true;
            }

        }

        //鼠标移动时窗体位置的重新计算

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouseOffset.X+3 , mouseOffset.Y + 30);
                Location = mousePos;
            }
        }

        //鼠标放下是鼠标时间结束

        private void AppManage_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = false;
            }
        }

通过以上两个方法可以实现窗体的重画 也可以增加其他控件。通过上述代码做出的窗体漂亮美观。比.Net自带的窗体好看多了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值