一.简介
简单的Windows窗体设计
运用button,axwindowsplayer,label等控件
实现背景图片,视频播放,爱心绘制等功能
二.效果展示
三.控件代码实现
button1:实现对其他button的隐藏,打开爱心绘制部分
private void button1_Click(object sender, EventArgs e)
{
button1.Visible= false;
button2.Visible= false;
timer1.Start();
}
timer1:绘制爱心并附带在一起多少天
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = "我们在一起 " + day + " 天了";
Bitmap bmp = new Bitmap(Width, Height);
Graphics g = Graphics.FromImage(bmp);
//如果想看到绘制的过程可以使用 e.Graphics 直接绘制在窗体上而不是绘制在图片中再替换背景
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string imagePath = Path.Combine(basePath, ".jpg");
Image backgroundImage = Image.FromFile(imagePath); //
g.DrawImage(backgroundImage, 0, 0, bmp.Width, bmp.Height);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//设置抗锯齿
List<PointF> ListP = new List<PointF>();
for (double x = -2.5; x < 2.5; x = x + 0.01)
{
double y = 0;
y = Math.Pow(x * x, (double)1 / 3) + 0.9 * Math.Sqrt(6.25 - Math.Pow(x, 2)) * Math.Sin(day / 10 * Math.PI * x); //心形曲线函数Y
y = y * -1;
double x2 = x * 100;
y = y * 100;
x2 = x2 + Width / 2;//因为Form窗体的原点在左上角,所有需要把坐标变为窗体中心
y = y + Height / 2;
PointF p = new PointF();
p.X = (float)x2;
p.Y = (float)y;
ListP.Add(p);//添加所有的点
}
Pen pen = new Pen(Color, 2);
for (int i = 1; i < ListP.Count(); i++)
{
g.DrawLine(pen, ListP[i - 1], ListP[i]);//把所有的点都练成线
}
BackgroundImage = bmp;//显示画的图片
day = day + 3;
if (day > 618)
{
day = 1;
timer1.Stop();
button3.Visible = true;
}
}
button2:显示无效的label,并实现短暂时停
private async void button2_Click(object sender, EventArgs e)
{
// 显示Label
label2.Visible = true;
// 等待一段时间
await Task.Delay(2000); // 等待2秒
// 隐藏Label
label2.Visible = false;
}
button3:实现视频的全屏播放,运用axwindowsplayer
private void button3_Click(object sender, EventArgs e)
{
button3.Visible= false;
label1.Visible = false;
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string VideoPath = Path.Combine(basePath, "5月16日.mp4");
//string path = "C:\\Users\\tian\\Desktop\\photo\\5月16日.mp4";
axWindowsMediaPlayer1.Visible = true;
axWindowsMediaPlayer1.URL = VideoPath;
axWindowsMediaPlayer1.Ctlcontrols.play();
timer2.Start();
}
不能直接在其中使用axwindowsplayer的fullscreen属性,需要使用timer2
private void timer2_Tick(object sender, EventArgs e)
{
if (Convert.ToInt32(axWindowsMediaPlayer1.playState) == 3)
{
axWindowsMediaPlayer1.fullScreen = true;
timer2.Stop(); timer2.Dispose(); timer2 = null;
}
}
其他全局设置
private Brush Color { get; set; } = Brushes.Red;
int day = 1;
四.资源(图片,视频)的调用
将一张图片作为主页面(form)的背景,将一张图片(可以是两人合照)作为爱心绘制时的背景,
将视频文件作为播放器的URL。
public Form1()
{
InitializeComponent();
//string imagepath="";
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string imagePath = Path.Combine(basePath, "520快乐.jpg");
this.BackgroundImage = Image.FromFile(imagePath);
this.BackgroundImageLayout = ImageLayout.Stretch;
}
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string imagePath = Path.Combine(basePath, "QQ图片20240117145044.jpg");
//string imagePath="";
Image backgroundImage = Image.FromFile(imagePath); //
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string VideoPath = Path.Combine(basePath, "5月16日.mp4");
//string path = "5月16日.mp4";
注意:代码中正常执行的路径是相对路径,我的图片和视频资源跟exe文件放在同一个文件内(bin/Debug/),可以在VS里添加。//之后的是本地路径的运用。