📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WinAppPictureBox
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnShowPic1;
private System.Windows.Forms.Button btnShowPic2;
private System.Windows.Forms.Button btnShowPic3;
private System.Windows.Forms.PictureBox picBrowse;
private System.Windows.Forms.Label labPicInfo;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.picBrowse = new System.Windows.Forms.PictureBox();
this.btnShowPic1 = new System.Windows.Forms.Button();
this.btnShowPic2 = new System.Windows.Forms.Button();
this.btnShowPic3 = new System.Windows.Forms.Button();
this.labPicInfo = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.picBrowse)).BeginInit();
this.SuspendLayout();
//
// picBrowse
//
this.picBrowse.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.picBrowse.Image = ((System.Drawing.Image)(resources.GetObject("picBrowse.Image")));
this.picBrowse.Location = new System.Drawing.Point(13, 10);
this.picBrowse.Name = "picBrowse";
this.picBrowse.Size = new System.Drawing.Size(256, 206);
this.picBrowse.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.picBrowse.TabIndex = 0;
this.picBrowse.TabStop = false;
//
// btnShowPic1
//
this.btnShowPic1.Location = new System.Drawing.Point(21, 267);
this.btnShowPic1.Name = "btnShowPic1";
this.btnShowPic1.Size = new System.Drawing.Size(100, 30);
this.btnShowPic1.TabIndex = 1;
this.btnShowPic1.Text = "显示图片1";
this.btnShowPic1.Visible = false;
this.btnShowPic1.Click += new System.EventHandler(this.btnShowPic1_Click);
//
// btnShowPic2
//
this.btnShowPic2.Location = new System.Drawing.Point(128, 267);
this.btnShowPic2.Name = "btnShowPic2";
this.btnShowPic2.Size = new System.Drawing.Size(100, 30);
this.btnShowPic2.TabIndex = 2;
this.btnShowPic2.Text = "显示图片2";
this.btnShowPic2.Click += new System.EventHandler(this.btnShowPic2_Click);
//
// btnShowPic3
//
this.btnShowPic3.Location = new System.Drawing.Point(245, 278);
this.btnShowPic3.Name = "btnShowPic3";
this.btnShowPic3.Size = new System.Drawing.Size(100, 29);
this.btnShowPic3.TabIndex = 3;
this.btnShowPic3.Text = "显示图片3";
this.btnShowPic3.Visible = false;
this.btnShowPic3.Click += new System.EventHandler(this.btnShowPic3_Click);
//
// labPicInfo
//
this.labPicInfo.AutoSize = true;
this.labPicInfo.Location = new System.Drawing.Point(117, 226);
this.labPicInfo.Name = "labPicInfo";
this.labPicInfo.Size = new System.Drawing.Size(0, 15);
this.labPicInfo.TabIndex = 4;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(378, 320);
this.Controls.Add(this.labPicInfo);
this.Controls.Add(this.btnShowPic3);
this.Controls.Add(this.btnShowPic2);
this.Controls.Add(this.btnShowPic1);
this.Controls.Add(this.picBrowse);
this.Name = "Form1";
this.Text = "图片框示例";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.picBrowse)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
//设置窗体的宽度为450像素,高度为450像素
this.Width=450;
this.Height=450;
//设置图片框相对于窗体的宽度为420像素,高度为330像素
picBrowse.Width=this.Width-30;
picBrowse.Height=this.Height-120;
//设置三个按钮距窗体顶端的距离为相同值
btnShowPic1.Top=btnShowPic2.Top=btnShowPic3.Top=//380;
this.Height-btnShowPic1.Height-50;
//设置三个按钮距窗体左边的距离为相同值,与Top属性相结合使按钮重叠
btnShowPic1.Left=btnShowPic2.Left=btnShowPic3.Left=//188;
(this.Width-btnShowPic1.Width)/2;
//设置标签框距窗体顶端的距离
labPicInfo.Top=this.Height-labPicInfo.Height-85;
labPicInfo.Text="当前显示为第一张图片";
//设置标签框距窗体左边的距离
labPicInfo.Left=(this.Width-labPicInfo.Width)/2;
//new关键字用于创建一个实例 Bitmap("f:\\gif\\001.gif");
//pictureBox1.Image=new Bitmap("f:\\gif\\001.gif");
//pictureBox1.Image = Image.FromFile("f:\\gif\\006.gif");
}
private void btnShowPic1_Click(object sender, System.EventArgs e)
{
btnShowPic2.Visible=true;
btnShowPic1.Visible=false;
picBrowse.Image=new Bitmap("001.gif");
labPicInfo.Text="当前显示为第一张图片";
}
private void btnShowPic2_Click(object sender, System.EventArgs e)
{
btnShowPic3.Show( );
btnShowPic2.Hide( );
picBrowse.Image=Image.FromFile("004.gif");
labPicInfo.Text="当前显示为第二张图片";
}
private void btnShowPic3_Click(object sender, System.EventArgs e)
{
btnShowPic1.Visible=true;
btnShowPic3.Visible=false;
picBrowse.Image=new Bitmap("006.gif");
labPicInfo.Text="当前显示为第三张图片";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -