C#自繪多狀態按鈕

本文介绍如何使用C#实现一个自定义的多状态按钮,包括设置按钮背景、文字及绘制不同状态下的关闭按钮图标。通过指定路径加载图片资源,并设定透明颜色及位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C#自繪多狀態按鈕

  protected override void OnPaintBackground(PaintEventArgs pea)
  {
   Graphics grfx = pea.Graphics;
   grfx.PageUnit = GraphicsUnit.Pixel;
   
   Graphics offScreenGraphics;
   Bitmap offscreenBitmap;
   
   offscreenBitmap = new Bitmap(BackgroundBitmap.Width, BackgroundBitmap.Height);
   offScreenGraphics = Graphics.FromImage(offscreenBitmap);
   
   if (BackgroundBitmap != null)
   {
    offScreenGraphics.DrawImage(BackgroundBitmap, 0, 0, BackgroundBitmap.Width, BackgroundBitmap.Height);
   }
   
   DrawCloseButton(offScreenGraphics);
   DrawText(offScreenGraphics);

   grfx.DrawImage(offscreenBitmap, 0, 0);
  }
  /// <summary>
  /// Sets the 3-State Close Button bitmap, its transparency color and its coordinates
  /// </summary>
  /// <param name="strFilename">Path of the 3-state Close button Bitmap on the disk (width must a multiple of 3)</param>
  /// <param name="transparencyColor">Color of the Bitmap which won't be visible</param>
  /// <param name="position">Location of the close button on the popup</param>
  /// <returns>Nothing</returns>
  public void SetCloseBitmap(string strFilename, Color transparencyColor, Point position)
  {
   CloseBitmap = new Bitmap(strFilename);
   CloseBitmap.MakeTransparent(transparencyColor);
   CloseBitmapSize = new Size(CloseBitmap.Width/3, CloseBitmap.Height);
   CloseBitmapLocation = position;
  }

  /// <summary>
  /// Sets the 3-State Close Button bitmap, its transparency color and its coordinates
  /// </summary>
  /// <param name="image">Image/Bitmap object which represents the 3-state Close button Bitmap (width must be a multiple of 3)</param>
  /// <param name="transparencyColor">Color of the Bitmap which won't be visible</param>
  /// /// <param name="position">Location of the close button on the popup</param>
  /// <returns>Nothing</returns>
  public void SetCloseBitmap(Image image, Color transparencyColor, Point position)
  {
   CloseBitmap = new Bitmap(image);
   CloseBitmap.MakeTransparent(transparencyColor);
   CloseBitmapSize = new Size(CloseBitmap.Width/3, CloseBitmap.Height);
   CloseBitmapLocation = position;
  }

  protected void DrawCloseButton(Graphics grfx)
  {
   if (CloseBitmap != null)
   { 
    Rectangle rectDest = new Rectangle(CloseBitmapLocation, CloseBitmapSize);
    Rectangle rectSrc;

    if (bIsMouseOverClose)
    {
     if (bIsMouseDown)
      rectSrc = new Rectangle(new Point(CloseBitmapSize.Width*2, 0), CloseBitmapSize);
     else
      rectSrc = new Rectangle(new Point(CloseBitmapSize.Width, 0), CloseBitmapSize);
    }  
    else
     rectSrc = new Rectangle(new Point(0, 0), CloseBitmapSize);
     

    grfx.DrawImage(CloseBitmap, rectDest, rectSrc, GraphicsUnit.Pixel);
   }
  }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值