C#—窗体的基本操作(实验8.1、8.2)

/*
 * 设置一个窗体,该窗体自动位于屏幕中央;大小不可调;最小化、最大化按钮不可用;窗体标题为“烟台大学”。
 * 在该窗体上,放置一个按钮、一个标签。单击按钮时,在标签上显示当前系统时间。
 */
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键时


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值