c#项目表白神器

1.项目分析
①添加背景图片form1的属性backgroundimage插入图片;
②设置背景图片的样式backgroundimagelayout属性,属性值为stretch;
在这里插入图片描述
③添加label1设置属性,text添加文本,font设置文本的字体样式
在这里插入图片描述
④添加三个button按钮,设置text属性分别为喜欢,不喜欢不喜欢;设置font属性
在这里插入图片描述
⑤页面加载时让button2隐藏,鼠标进入button3时让button3隐藏button2显现,鼠标进入button2时让button2隐藏button3显现,
点击button1时弹窗MessageBox
代码:

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

namespace _01表白神器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;//设置窗口的水平居中
            this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;//设置窗口的垂直居中
            label1.Left = this.Width / 2 - label1.Width / 2;//设置label在form1里的水平居中
            label1.BackColor = Color.Transparent;//设置label背景为透明
            button2.Visible = false;//让button2隐藏 visible
            label1.Text = "你喜欢我吗?";//设置label的文本内容
            label1.Font = new Font("仿宋",40);//设置label1的文本的字体样式
        }

        //MouseEnter设置鼠标进入事件
        private void button2_MouseEnter(object sender, EventArgs e)
        {
            button2.Visible = false;
            button3.Visible = true;
        }

        private void button3_MouseEnter(object sender, EventArgs e)
        {
            button2.Visible = true;
            button3.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("真巧!我也喜欢你.");//添加弹窗
        }

        private void Form1_SizeChanged(object sender, EventArgs e)//设置form1的size改变时label仍水平居中
        {
            label1.Left = this.Width / 2 - label1.Width / 2;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43434300/article/details/85267116