1. 程式人生 > >c#專案表白神器

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;
        }
    }
}