【问题标题】:Mousewheel scroll down event in C# winform programmatically doneC#winform中的鼠标滚轮向下滚动事件以编程方式完成
【发布时间】:2013-09-16 16:16:40
【问题描述】:

我正在尝试使用图片框控件和轨迹栏进行图像幻灯片放映。轨迹栏获得最小值,最大值对应于要显示的图像数量。我使用计时器来获取幻灯片的间隔时间以及轨迹栏值的变化。

现在,这是图片框中每个图像的主要内容,我在图像上绘制了一个矩形框。

当表单加载第一张图片时,我无法在第一张图片上绘图。但是如果我滚动鼠标滚轮我可以做到。

我需要帮助才能在第一张图片加载后触发鼠标滚轮滚动事件。

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);
        }
    }
  }
}

【问题讨论】:

  • 又一个 CreateGraphics() 问题。最小化和恢复你的应用程序的窗口,看看你为什么不应该使用它。改为为 pictureBox1 实现 Paint 事件。

标签: c# winforms event-handling eventtrigger


【解决方案1】:

您可以将此代码添加到您的表单以滚动您的表单(当然是MouseWheel):

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

注意:我可以看到您以自己的形式定义了 MouseWheel 事件。这将隐藏基础MouseWheel 事件,我认为您没有任何理由这样做,您自己的MouseWheel 不能作为基础事件,您必须抓住@ 987654326@ 并自己提升它,我们应该使用基本的MouseWheel 事件来代替。 (也许您认为您的表单类中没有任何MouseWheel 事件?)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-22
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    相关资源
    最近更新 更多