/*
* 设置一个窗体,该窗体自动位于屏幕中央;大小不可调;最小化、最大化按钮不可用;窗体标题为“烟台大学”。
* 在该窗体上,放置一个按钮、一个标签。单击按钮时,在标签上显示当前系统时间。
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.CenterToScreen(); //使窗体自动位于屏幕中央
//窗体大小不可调
this.FormBorderStyle = FormBorderStyle.FixedSingle; //控制窗体边界的类型
this.MinimizeBox = false; //最小化禁止
this.MaximizeBox = false; //最大化禁止
this.BackColor = Color.White; //窗体背景色设置为白色
this.Text = "烟台大学"; //窗体标题为“烟台大学”
button1.Text = "单击该按钮显示系统当前时间";
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = (DateTime.Now).ToString();
}
}
}
运行结果:
当按下按钮时
/*
* 窗体上有两个按钮:一个显示文本,一个显示图片。
* 单击上面按钮或按下Alt+B键,可弹出提示框;单击下面按钮也可以弹出消息框。
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true; //窗体优先接收键盘事件 //若没有此句,则Alt+B无作用
button1.Text = "第一个按钮(B)";
button2.BackgroundImage = Image.FromFile(Application.StartupPath + @"\g.png");
button2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; //让图片适应按钮的大小
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("单击了我!");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("单击了我!");
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.B)
MessageBox.Show("单击了我!");
}
}
}
运行结果:
当单击button1、button2或者按下Alt+B键时