C# Winform窗体中添加鼠标滚轮事件鼠标滚轮的方向判断

C# Winform窗体中添加鼠标滚轮事件鼠标滚轮的方向判断

如图所示,在控件中没有直接的鼠标滚轮事件,需要我们进行手动添加。
在这里插入图片描述
下面以自定义控件MapComponent为例进行说明:
添加鼠标滚轮事件:
this.MouseWheel += new System.Windows.Forms.MouseEventHandler(LMap_MouseWheel);
其中LMap_MouseWheel为鼠标滚轮上下滚动事件的自定义函数,当鼠标滚轮上下移动时,该函数会响应鼠标滚轮事件。

		//鼠标滚轮事件自定义函数(控件中没有直接的鼠标滚轮事件,需手动添加)
		public void LMap_MouseWheel(object sender, MouseEventArgs e)
		{
			//当e.Delta > 0时鼠标滚轮是向上滚动,e.Delta < 0时鼠标滚轮向下滚动
			if (e.Delta > 0)//滚轮向上
			{
				ZoomIn(); //放大
				//MessageBox.Show("鼠标向上滑动");
			}
			else
			{
				ZoomOut();//缩小
				//MessageBox.Show("鼠标向下滑动");
			}
		}
		//沿中心缩放:变大
		public void ZoomIn()
		{
			if (/*this.Status == LMapStatus.ZoomIn && */this.ZoomValue >= 0.02)
			{
				this.ZoomValue *= 0.5f;//缩放比例
				this.ZoomValue_w *= 0.5f;//地图宽
				this.ZoomValue_h *= 0.5f;//地图高
				this.Display();
			}
		}

		//沿中心缩放:缩小
		public void ZoomOut()
		{
			if (/*this.Status == LMapStatus.ZoomOut && */this.ZoomValue <= 0.5)
			{
				this.ZoomValue *= 2.0f;
				this.ZoomValue_w *= 2f;
				this.ZoomValue_h *= 2f;
				this.Display();
			}
		}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值