C#winform中的鼠标滚轮向下滚动事件以编程方式完成
我正在尝试用picturebox控件和轨迹栏进行图像幻灯片放映。轨迹栏获取与要显示的图像数相对应的最小值和最大值。我使用计时器获取幻灯片的间隔时间以及轨迹栏值更改 现在,这里是picturebox中每个图像的主要内容,我在图像上画了一个矩形框 当表单加载第一个图像时,我无法绘制第一个图像。但是如果我滚动鼠标滚轮,我可以做到 我需要帮助触发鼠标滚轮滚动事件一次后,第一个图像得到加载C#winform中的鼠标滚轮向下滚动事件以编程方式完成,c#,winforms,event-handling,eventtrigger,C#,Winforms,Event Handling,Eventtrigger,我正在尝试用picturebox控件和轨迹栏进行图像幻灯片放映。轨迹栏获取与要显示的图像数相对应的最小值和最大值。我使用计时器获取幻灯片的间隔时间以及轨迹栏值更改 现在,这里是picturebox中每个图像的主要内容,我在图像上画了一个矩形框 当表单加载第一个图像时,我无法绘制第一个图像。但是如果我滚动鼠标滚轮,我可以做到 我需要帮助触发鼠标滚轮滚动事件一次后,第一个图像得到加载 using System; using System.Collections.Generic; using Syst
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public event MouseEventHandler MouseWheel;
//MouseEventArgs k = new MouseEventArgs(MouseButtons.Middle,0,0,-1);
string[] pics = { "C:\\Downloads\\folder_picture_green.png", "C:\\Downloads\\Aetherpal.ico", "C:\\Downloads\\folder_picture_green.png" };
public Form1()
{
InitializeComponent();
this.trackBar1.Minimum = 0;
this.trackBar1.Maximum = (pics.Count() - 1);
//this.trackBar1.Maximum = 0;
imageupdate(0);
timer1.Start();
timer1.Interval = 3000;
this.MouseWheel += test;
this.MouseWheel(null, null);
}
private void test(object sender, System.Windows.Forms.MouseEventArgs e)
{
MessageBox.Show("Scrolled");
}
private void check(object sender, System.EventArgs e)
{
//if (Initializing == false) { return; }
if(this.trackBar1.Value < this.trackBar1.Maximum )
this.trackBar1.Value += 1;
else
timer1.Stop();
}
private void Valuechange(object sender, System.EventArgs e)
{
imageupdate(this.trackBar1.Value);
if(this.trackBar1.Value < this.trackBar1.Maximum)
timer1.Start();
}
private void imageupdate(int k)
{
this.pictureBox1.Refresh();
this.pictureBox1.Image = new Bitmap(pics[k]);
Pen blackPen = new Pen(Color.Blue, 5);
this.pictureBox1.Refresh();
using (Graphics g = this.pictureBox1.CreateGraphics())
{
g.DrawRectangle(blackPen, 10, 10, 100, 50);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间测试
{
公共部分类Form1:Form
{
公共活动鼠标;电动滑鼠;
//MouseEventArgs k=新的MouseEventArgs(鼠标按钮。中间,0,0,-1);
字符串[]pics={“C:\\Downloads\\folder\u picture\u green.png”、“C:\\Downloads\\Aetherpal.ico”、“C:\\Downloads\\folder\u picture\u green.png”};
公共表格1()
{
初始化组件();
this.trackBar1.Minimum=0;
this.trackBar1.max=(pics.Count()-1);
//this.trackBar1.max=0;
图像更新(0);
timer1.Start();
计时器1.间隔=3000;
此.鼠标滚轮+=测试;
这个鼠标滚轮(null,null);
}
私有无效测试(对象发送器,System.Windows.Forms.MouseEventArgs e)
{
MessageBox.Show(“滚动”);
}
私有无效检查(对象发送方,System.EventArgs e)
{
//如果(初始化==false){return;}
if(this.trackBar1.Value
您可以将此代码添加到表单中以滚动表单(当然可以使用鼠标滚轮
):
private void Wheel(整数刻度,布尔下降){
//WM_鼠标滚轮=0x20a
Message msg=Message.Create(Handle,0x20a,new IntPtr((down?-1:1)还有另一个CreateGraphics()问题。最小化并还原应用程序的窗口。请查看不应使用它的原因。改为为对pictureBox1执行绘制事件。
private void Wheel(int ticks, bool down){
//WM_MOUSEWHEEL = 0x20a
Message msg = Message.Create(Handle, 0x20a, new IntPtr((down ? -1 : 1)<<16), new IntPtr(MousePosition.X + MousePosition.Y << 16));
for(int i = 0; i < ticks; i++)
WndProc(ref msg);
}
//Use it
Wheel(120,true);//Wheel down
Wheel(120,false);//Wheel up