Mouse scrolling in a panel with dynamically added image controls?

I dynamically added 20 pictures to the panel and would like to see the scroll of the panel when I use the mouse wheel. To implement this, I tried to set autoscroll to true in the control panel. Here is the code. For I'm As Integer = 1 to 20:

        Dim b As New PictureBox()
        b.Image = Nothing
        b.BorderStyle = BorderStyle.FixedSingle
        b.Text = i.ToString()
        b.Size = New Size(60, 40)
        b.Location = New Point(0, (i * b.Height) - b.Height)
        b.Parent = Panel1
        Panel1.Controls.Add(b)
    Next

I did the same with the button control and it works fine. For I As Integer = 1 To 100:

        Dim b As New Button()

        b.Text = i.ToString()
        b.Size = New Size(60, 40)
        b.Location = New Point(0, (i * b.Height) - b.Height)
        b.Parent = Panel1
        Panel1.Controls.Add(b)
    Next

Does it work for a button control, but not for a picturebox or label control? How can I implement the scroll effect using the mouse wheel?

+5
source share
2

, . , , , PictureBox, , . select() , , .

, , Control.MouseEnter:

void panel1_MouseEnter(object sender, EventArgs e)
{
    panel1.select();
}
+6

"cwick" , Windows WM_MOUSWHEEL . , , . , , - , Windows Parent . Button , Parent - , .

, ( SetStyle (ControlStyles.Selectable)), . , , (, Office), . WF , . , , . IMessageFilter . :

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1 {
  public partial class Form1 : Form, IMessageFilter {
    public Form1() {
      InitializeComponent();
      Application.AddMessageFilter(this);
    }

    public bool PreFilterMessage(ref Message m) {
      if (m.Msg == 0x20a) {
        // WM_MOUSEWHEEL, find the control at screen position m.LParam
        Point pos = new Point(m.LParam.ToInt32());
        IntPtr hWnd = WindowFromPoint(pos);
        if (hWnd != IntPtr.Zero && hWnd != m.HWnd && Control.FromHandle(hWnd) != null) {
          SendMessage(hWnd, m.Msg, m.WParam, m.LParam);
          return true;
        }
      }
      return false;
    }

    // P/Invoke declarations
    [DllImport("user32.dll")]
    private static extern IntPtr WindowFromPoint(Point pt);
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
  }
}

, . , , .

+3

All Articles