CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2007
    Posts
    274

    [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?
    Last edited by Traps; February 10th, 2009 at 10:46 PM.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  3. #3
    Join Date
    Mar 2007
    Posts
    274

    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.
    Last edited by Traps; February 11th, 2009 at 12:40 AM.

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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&#37; transparent maybe?
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  5. #5
    Join Date
    Mar 2007
    Posts
    274

    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

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    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().

  7. #7
    Join Date
    Mar 2007
    Posts
    274

    Re: Detect vertical scroll in picturebox?

    Quote Originally Posted by BigEd781 View Post
    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 View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured