Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 鼠标滚轮在面板中滚动,并动态添加picturebox控件?_Vb.net_Winforms_Scroll_Panel_Picturebox - Fatal编程技术网

Vb.net 鼠标滚轮在面板中滚动,并动态添加picturebox控件?

Vb.net 鼠标滚轮在面板中滚动,并动态添加picturebox控件?,vb.net,winforms,scroll,panel,picturebox,Vb.net,Winforms,Scroll,Panel,Picturebox,我已经向一个面板动态添加了20个图片框,希望在使用鼠标滚轮时看到面板滚动。为了实现这一点,我尝试在panel控件上将autoscroll设置为true。这是代码。 对于i作为整数=1到20: Dim b As New PictureBox() b.Image = Nothing b.BorderStyle = BorderStyle.FixedSingle b.Text = i.ToString() b.Size =

我已经向一个面板动态添加了20个图片框,希望在使用鼠标滚轮时看到面板滚动。为了实现这一点,我尝试在panel控件上将autoscroll设置为true。这是代码。 对于i作为整数=1到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作为整数=1到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
它适用于“按钮”控件,但不适用于“picturebox”或“label”控件?
如何使用“鼠标滚轮”实现滚动效果?

当面板或其中的控件具有焦点时,面板将使用鼠标滚轮滚动。您遇到的问题是,当您单击PictureBox或panel时,它都不会收到焦点。如果在面板上调用
select()
,您将看到鼠标滚轮再次开始工作

一种可能的解决方案是,每当鼠标光标进入面板时,通过处理Control.MouseEnter事件来选择面板:

void panel1_MouseEnter(object sender, EventArgs e)
{
    panel1.select();
}
“cwick”非常正确,Windows将WM_MOUSWHEEL通知发布到具有焦点的窗口。当你把一个按钮放在面板上时,它会工作,因为它可以获得焦点。接下来发生的事情是Windows一直在寻找父控件来获取消息。按钮将不关心它,它的父母是面板,它会高兴地滚动和消费的消息

除了使用子控件的焦点控制能力(您必须重写它们并调用StType(控件样式。可选择)),您可以考虑更改该消息的处理方式。许多商业应用(浏览器、办公应用)似乎没有这个问题,因为它们只有几个窗口。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);
  }
}
请注意,此代码对于应用程序中的任何窗口都是活动的。确保您尝试了它,并验证它不会让用户太困惑