首页 > 编程 > C# > 正文

在程序中动态地修改按钮的图片和尺寸

2023-05-10 18:49:25
字体:
来源:转载
供稿:网友

下面的C#程序代码演示了在程序中创建图片按钮,并指定其大小及其位置的方法。

注:本程序转自国外的一个网站(http://www.java2s.com)。

using System;
using System.Drawing;
using System.Windows.Forms;
   
class BitmapButtons: Form
{
     int    cxBtn, cyBtn, dxBtn;
     Button btnLarger, btnSmaller;
   
     public static void Main()
     {
          Application.Run(new BitmapButtons());
     }
     public BitmapButtons()
     {
          ResizeRedraw = true;
   
          dxBtn = Font.Height;
          btnLarger = new Button();
          btnLarger.Parent = this;
          btnLarger.Image  = new Bitmap(GetType()"LargerButton.bmp";
   
          cxBtn = btnLarger.Image.Width  + 8;
          cyBtn = btnLarger.Image.Height + 8;
   
          btnLarger.Size   = new Size(cxBtn, cyBtn);
          btnLarger.Click += new EventHandler(ButtonLargerOnClick);
   
          btnSmaller = new Button();
          btnSmaller.Parent = this;
          btnSmaller.Image  = new Bitmap(GetType()"SmallerButton.bmp");
          btnSmaller.Size   = new Size(cxBtn, cyBtn);
          btnSmaller.Click += new EventHandler(ButtonSmallerOnClick);
   
          OnResize(EventArgs.Empty);
     }
     protected override void OnResize(EventArgs ea)
     {
          base.OnResize(ea);
   
          btnLarger.Location = new Point(ClientSize.Width / - cxBtn - dxBtn / 2,
                                  (ClientSize.Height - cyBtn2);
          btnSmaller.Location = new Point(ClientSize.Width / + dxBtn / 2,
                                  (ClientSize.Height - cyBtn2);
     }
     void ButtonLargerOnClick(object obj, EventArgs ea)
     {
          Left   = 50;
          Top    = 50;
          Width  = 50;
          Height = 50;
     }
     void ButtonSmallerOnClick(object obj, EventArgs ea)
     {
          Left   = 200;
          Top    = 200;
          Width  = 20;
          Height = 20;
     }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表