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

📄 bitmapbutton.cs

📁 说明c#组件使用.下了看看就知道了.方便快接.
💻 CS
字号:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace BitmapButton
{
	public class BitmapButton : Button
	{
		enum btnState
		{
			BUTTON_UP=0,
			BUTTON_DOWN=1,
			BUTTON_FOCUSED=2,
			BUTTON_MOUSE_ENTER=3,
			BUTTON_DISABLED=4,
		}

		btnState imgState=btnState.BUTTON_UP;
		bool mouseEnter=false;

		public BitmapButton()
		{
			// enable double buffering.  Must be done by a derived class
			SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);

			// initialize event handlers
			Paint+=new PaintEventHandler(BitmapButton_Paint);
			MouseDown+=new MouseEventHandler(BitmapButton_MouseDown);
			MouseUp+=new MouseEventHandler(BitmapButton_MouseUp);
			GotFocus+=new EventHandler(BitmapButton_GotFocus);
			LostFocus+=new EventHandler(BitmapButton_LostFocus);
			MouseEnter+=new EventHandler(BitmapButton_MouseEnter);
			MouseLeave+=new EventHandler(BitmapButton_MouseLeave);
			KeyDown+=new KeyEventHandler(BitmapButton_KeyDown);
			KeyUp+=new KeyEventHandler(BitmapButton_KeyUp);
			EnabledChanged+=new EventHandler(BitmapButton_EnabledChanged);
		}

		private void BitmapButton_Paint(object sender, PaintEventArgs e)
		{
			Graphics gr=e.Graphics;
			int indexWidth=Size.Width*(int)imgState;

			if (Image.Width > indexWidth)
			{
				gr.DrawImage(Image, 0, 0, new Rectangle(new Point(indexWidth, 0), Size), GraphicsUnit.Pixel);
			}
			else
			{
				gr.DrawImage(Image, 0, 0, new Rectangle(new Point(0, 0), new Size(Size.Width, Size.Height)), GraphicsUnit.Pixel);
			}
		}

		private void BitmapButton_MouseDown(object sender, MouseEventArgs e)
		{
			imgState=btnState.BUTTON_DOWN;
			Invalidate();
		}

		private void BitmapButton_MouseUp(object sender, MouseEventArgs e)
		{
			imgState=btnState.BUTTON_FOCUSED;
			Invalidate();
		}

		private void BitmapButton_GotFocus(object sender, EventArgs e)
		{
			imgState=btnState.BUTTON_FOCUSED;
			Invalidate();
		}

		private void BitmapButton_LostFocus(object sender, EventArgs e)
		{
			if (mouseEnter)
			{
				imgState=btnState.BUTTON_MOUSE_ENTER;
			}
			else
			{
				imgState=btnState.BUTTON_UP;
			}
			Invalidate();
		}

		private void BitmapButton_MouseEnter(object sender, EventArgs e)
		{
			// only show mouse enter if doesn't have focus
			if (imgState==btnState.BUTTON_UP)
			{
				imgState=btnState.BUTTON_MOUSE_ENTER;
			}
			mouseEnter=true;
			Invalidate();
		}

		private void BitmapButton_MouseLeave(object sender, EventArgs e)
		{
			// only restore state if doesn't have focus
			if (imgState != btnState.BUTTON_FOCUSED)
			{
				imgState=btnState.BUTTON_UP;
			}
			mouseEnter=false;
			Invalidate();
		}

		private void BitmapButton_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyData==Keys.Space)
			{
				imgState=btnState.BUTTON_DOWN;
				Invalidate();
			}
		}

		private void BitmapButton_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.KeyData==Keys.Space)
			{
				// still has focus
				imgState=btnState.BUTTON_FOCUSED;
				Invalidate();
			}
		}

		private void BitmapButton_EnabledChanged(object sender, EventArgs e)
		{
			if (Enabled)
			{
				imgState=btnState.BUTTON_UP;
			}
			else
			{
				imgState=btnState.BUTTON_DISABLED;
			}
			Invalidate();
		}
	}
}

⌨️ 快捷键说明

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