Josh
Josh

Reputation: 1049

Mousewheel scroll down event in C# winform programmatically done

I am trying do image slide show with picturebox Control and a trackbar. The trackbar gets minimum and maximum value corresponds to the number of images to show. I use a timer to get interval period for the slide along with trackbar valuechange.

Now, here is the main thing for each image in the picturebox I'm drawing a rectangle box over the image.

I am not able to draw on the first image when the form get load with first image. But I could do if I scroll the mouse wheel.

I need help to trigger the mouse wheel scroll event once after the first image get loaded.

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

Upvotes: 0

Views: 6100

Answers (1)

King King
King King

Reputation: 63377

You can add this code to your form to scroll your form (of course with 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

NOTE: I can see you define a MouseWheel event in your own form. This will hide the base MouseWheel event, I don't think you have any reason to do this, your own MouseWheel can't work as the base one, you have to catch the win32 message and raise it yourself, we should use the base MouseWheel event instead. (Maybe you thought there isn't any MouseWheel event in your form class?)

Upvotes: 0

Related Questions