[RESOLVED] Detect vertical scroll in picturebox?
Do I have to pinvoke to do this?
I tried
Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
myPictureBox x = new myPictureBox();
x.Dock = DockStyle.Fill;
this.Controls.Add(x);
}
}
public class myPictureBox : PictureBox
{
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x115)
{
if (m.WParam.ToInt32() < 0)
{
}
else
{
}
}
}
}
But I think thats only for controls with a vertical scroll bar. Picturebox does not seem to fire the OnMouseWheel event....... Any ideas on how to do this in managed code?
Re: Detect vertical scroll in picturebox?
You could create your own class inherited from VScrollbar and place it on the form. Hook into the form's MouseWheel event and control the scroll bar like this ( a little like this,m this is quick and dirty):
Code:
public Form1()
{
InitializeComponent();
this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
}
void Form1_MouseWheel(object sender, MouseEventArgs e)
{
int toMove = ( e.Delta < 0 ) ? 1 : -1;
int current = this.myScrollBar1.Value;
if( (current + toMove) < 0 ||
( (current + toMove) > this.myScrollBar1.Maximum) )
{
toMove = 0;
}
this.myScrollBar1.Value += toMove;
}
You would then hook into the ValueChanged event of the scroll bar in order to scroll the image. The example is really basic, but it would work.
Re: Detect vertical scroll in picturebox?
No, that sounds more like a hack than a solution.
This is close, but not quite right, as I need to know when the picturebox has focus.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
PictureBox pb = new PictureBox();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pb.Dock = DockStyle.Fill;
pb.BackColor = Color.White;
this.Controls.Add(pb);
}
protected override void OnMouseWheel(System.Windows.Forms.MouseEventArgs e)
{
//If (pb.Focused = True) Then
if (e.Delta < 0)
{
}
else
{
}
//End If
}
}
}
tested the pb, and confirmed that a picturebox can not be selected.
Edit to add: I figured it out I'll just monitor when the mouse enters and leaves the picturebox.
Thank you for reviewing my post. I'm sure your solution would work in another situation.
Re: Detect vertical scroll in picturebox?
Not sure if you can overlay the Picture box with another control that accepts the message you want, but make the other control invisible. Then simply react to the foreground controls messages, but act on the picture box. I know I did it in an MFC Doc/View app once, but I'm not so sure that invisible controls receive messages on a windows form... But you could set it to 99% transparent maybe?
Re: Detect vertical scroll in picturebox?
Bah. The answer is simple and here it is in vb (yes I was writing it in vb for another forum. C# is my one true love though :))
Code:
Public Class Form1
Private pb As New PictureBox
Private checkWheel As Boolean = False
Private rand As New Random()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
pb.BackColor = Color.Beige
AddHandler pb.MouseEnter, AddressOf pb_MouseEnter
AddHandler pb.MouseLeave, AddressOf pb_MouseLeave
Me.Controls.Add(pb)
End Sub
Protected Overrides Sub OnMouseWheel(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseWheel(e)
If checkWheel Then
If e.Delta < 0 Then
' zoom out
pb.BackColor = Color.FromArgb(rand.Next Or &HFF000000)
Else
' zoom in
pb.BackColor = Color.FromArgb(rand.Next Or &HFF000000)
End If
End If
End Sub
Private Sub pb_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)
checkWheel = True
End Sub
Private Sub pb_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs)
checkWheel = False
End Sub
End Class
Re: Detect vertical scroll in picturebox?
So, how is that any different than what i suggested? I'm glad you have a solution, but all I see is a flag set OnLeave() or OnExit().
Re: Detect vertical scroll in picturebox?
Quote:
Originally Posted by
BigEd781
So, how is that any different than what i suggested? I'm glad you have a solution, but all I see is a flag set OnLeave() or OnExit().
Quote:
Originally Posted by
BigEd781
You could create your own class inherited from VScrollbar and place it on the form. Hook into the form's MouseWheel event and control the scroll bar like this
I saw the line "create your own class, and inherit VScrollbar", then glanced at your code. I thought you were like perhaps setting focus to the vscrollbar, and using the mousewheel event of that..... haha nevermind, I was hopped on a little prescribed vicoden (and still am to a degree) so, please excuse... but my solution is much more straightforward.
I should have set the thread title to "Detect mouse wheel delta in a picturebox."
Anyways, as usual, you are able to provide an answer. I thank you again.