⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Visual C#2005程序设计教程
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinApp3_2图片框示例
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {   
            button1.Text = "大"; button2.Text = "中"; button3.Text = "小";   // 初始化按钮的Text属性
            pictureBox1.Image = Image.FromFile("照片.jpg");   // 加载图片
            this.MinimumSize = new Size(300, 300);//.Width = 300; this.MinimumSize.Height = 300;
            this.Width = 500; this.Height = 500;   // 初始化窗口为中等大小
            // 设置图片框大小及位置
            pictureBox1.Width = this.Width - 50;   
            pictureBox1.Height = this.Height - 150;
            pictureBox1.Top = 10;
            pictureBox1.Left = (this.Width - pictureBox1.Width) / 2;
            // 设置按钮位置
            int x = pictureBox1.Height + pictureBox1.Top;
            button1.Top = button2.Top = button3.Top
                = x + (this.Height - x - button1.Height) / 2 - 25;
            button2.Left = (this.Width - button2.Width) / 2;
            button1.Left = (button2.Left - button1.Width) / 2;
            x=button2.Left+button2.Width;
            button3.Left = x + (this.Width - x - button3.Width) / 2;
            // 设置窗口标题(Text属性)
            this.Text = "图片大小:"+pictureBox1.Width + " x " + pictureBox1.Height + "像素";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Width = 700; this.Height = 700;
            pictureBox1.Width = this.Width - 50;
            pictureBox1.Height = this.Height - 150;
            pictureBox1.Top = 10;
            pictureBox1.Left = (this.Width - pictureBox1.Width) / 2;
            int x = pictureBox1.Height + pictureBox1.Top;
            button1.Top = button2.Top = button3.Top
                = x + (this.Height - x - button1.Height) / 2 - 25;
            button2.Left = (this.Width - button2.Width) / 2;
            button1.Left = (button2.Left - button1.Width) / 2;
            x = button2.Left + button2.Width;
            button3.Left = x + (this.Width - x - button3.Width) / 2;
            this.Text = "图片大小:" + pictureBox1.Width + " x " + pictureBox1.Height + "像素";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Width = 500; this.Height = 500;
            pictureBox1.Width = this.Width - 50;
            pictureBox1.Height = this.Height - 150;
            pictureBox1.Top = 10;
            pictureBox1.Left = (this.Width - pictureBox1.Width) / 2;
            int x = pictureBox1.Height + pictureBox1.Top;
            button1.Top = button2.Top = button3.Top
                = x + (this.Height - x - button1.Height) / 2 - 25;
            button2.Left = (this.Width - button2.Width) / 2;
            button1.Left = (button2.Left - button1.Width) / 2;
            x = button2.Left + button2.Width;
            button3.Left = x + (this.Width - x - button3.Width) / 2;
            this.Text = "图片大小:" + pictureBox1.Width + " x " + pictureBox1.Height + "像素";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Width = 300; this.Height = 300;
            pictureBox1.Width = this.Width - 50;
            pictureBox1.Height = this.Height - 150;
            pictureBox1.Top = 10;
            pictureBox1.Left = (this.Width - pictureBox1.Width) / 2;
            int x = pictureBox1.Height + pictureBox1.Top;
            button1.Top = button2.Top = button3.Top
                = x + (this.Height - x - button1.Height) / 2 - 25;
            button2.Left = (this.Width - button2.Width) / 2;
            button1.Left = (button2.Left - button1.Width) / 2;
            x = button2.Left + button2.Width;
            button3.Left = x + (this.Width - x - button3.Width) / 2;
            this.Text = "图片大小:" + pictureBox1.Width + " x " + pictureBox1.Height + "像素";
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            pictureBox1.Width = this.Width - 50;
            pictureBox1.Height = this.Height - 150;
            pictureBox1.Top = 10;
            pictureBox1.Left = (this.Width - pictureBox1.Width) / 2;
            int x = pictureBox1.Height + pictureBox1.Top;
            button1.Top = button2.Top = button3.Top
                = x + (this.Height - x - button1.Height) / 2 - 25;
            button2.Left = (this.Width - button2.Width) / 2;
            button1.Left = (button2.Left - button1.Width) / 2;
            x = button2.Left + button2.Width;
            button3.Left = x + (this.Width - x - button3.Width) / 2;
            this.Text = "图片大小:" + pictureBox1.Width + " x " + pictureBox1.Height + "像素";
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -