c#异形窗体遮罩效果

c#异形窗体遮罩效果,移动,关闭,最大化,最小化,还原操作

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

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private Point mouseOffset;                 //记录鼠标指针的坐标 
        private bool isMouseDown = false;      //记录鼠标按键是否按下
        public Form1()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
           
            InitializeComponent();
        }
       

            private void Form1_Load(object sender, EventArgs e)
        {
            TransparencyKey = Color.White;
            Image BackgroundImage = Bitmap.FromFile("111.png");
            BitmapRegion BitmapRegion = new BitmapRegion();//此为生成不规则窗体和控件的类
          //  BitmapRegion.CreateControlRegion(this, new Bitmap("feichuan5.jpg"));
            BitmapRegion.CreateControlRegion(this, new Bitmap("feichuan5a.png"));
        }

        private void button_start_Click(object sender, EventArgs e)
        {
            MessageBox.Show("程序以启动");
        }

        private void button_close_Click(object sender, EventArgs e)
        {
            MessageBox.Show("程序已将关闭");
            this.Close();
        }

        private void button_max_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        private void button_min_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void button_to_default_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

        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, mouseOffset.Y);
                Location = mousePos;
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            //   修改鼠标状态isMouseDown的值
            //   确保只有鼠标左键按下并移动时,才移动窗体
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = false;
            }
        }
    }
}

工程代码:

https://download.csdn.net/download/txwtech/88746025https://download.csdn.net/download/txwtech/88746025

相关推荐
Nita.20 分钟前
设计模式| 观察者模式 Observer Pattern详解
观察者模式·设计模式·c#
水瓶丫头站住29 分钟前
Qt中QRadioButton的样式设置
开发语言·qt
关关钧2 小时前
【R语言】绘图
开发语言·python·r语言
大吱佬2 小时前
解决每次 Maven Rebuild 后 Java 编译器版本变为 1.5
java·开发语言·maven
C#Thread3 小时前
C#上位机--循环语句
开发语言·c#
diemeng11193 小时前
2024系统编程语言风云变幻:Rust持续领跑,Zig与Ada异军突起
开发语言·前端·后端·rust
软件黑马王子4 小时前
Unity游戏制作中的C#基础(3)加减乘除算术操作符,比较运算符,逻辑与,或运算符
开发语言·unity·c#
张太行_4 小时前
Qt Creator 设计界面后的预览方法
开发语言·qt