一:创建一个winform窗体,把BackgroundImage引入一个不规则的图片,设置属性BackgroundImageLayout为Stretch
二:主要代码
using System; using System.Drawing; using System.Media; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Windows.Forms; namespace TestDemo {public partial class Form1 : Form { bool beginMove = false; int currentXPosition; int currentYPosition; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.TransparencyKey = Color.White; //设置默认透明色 this.BackColor = this.TransparencyKey; //设置当前窗体的背景色为透明 this.FormBorderStyle = FormBorderStyle.None; //隐藏窗体边框 } private void Form1_MouseDown(object sender, MouseEventArgs e) { //窗体拖拽 beginMove = true; currentXPosition = MousePosition.X; currentYPosition = MousePosition.Y; this.Refresh(); } private void Form1_MouseLeave(object sender, EventArgs e) { //设置初始状态 currentXPosition = 0; currentYPosition = 0; beginMove = false; } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (beginMove) { //根据鼠标X坐标确定窗体X坐标 this.Left += MousePosition.X - currentXPosition; //根据鼠标Y坐标确定窗体Y坐标 this.Top += MousePosition.Y - currentYPosition; currentXPosition = MousePosition.X; currentYPosition = MousePosition.Y; } } private void Form1_MouseUp(object sender, MouseEventArgs e) { beginMove = false; } private void Form1_Click(object sender, EventArgs e) { try { SoundPlayer player = new SoundPlayer(); player.SoundLocation = Application.StartupPath + "\BIGBANG.wav"; player.Load(); player.Play(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
三:下载地址
点击下载 提取码:yd14