我尝试创建自定义可滚动面板,因为TableLayoutPanels滚动功能不是很可定制的。
我有一个继承自Microsoft.Visualbasics.Powerpacks.RectangleShape的自定义类。此类用于创建滚动条对象。它包含一个MouseDrag事件,该事件应该在滚动条上按下鼠标时触发,并在鼠标重新启动时终止。
此ScrollBar对象在另一个继承自Forms.Panel的自定义类中实例化。
在主窗体方法中,实例化自定义面板,并将MouseDrag事件添加到ScrollBar。当我点击ScrollBar时没有任何反应。我甚至使用内置的Click事件进行测试,但没有任何反应。任何帮助将不胜感激。
滚动条类:
class ScrollBar : RectangleShape
{
public event MouseEventHandler MouseDrag;
private bool mouseHeld = false;
public bool MouseHeld { get => mouseHeld; set => mouseHeld = value; }
public ScrollBar()
{
InitializeObject();
}
public ScrollBar(int x, int y, int width, int height) : base(x, y, width, height)
{
InitializeObject();
}
private void InitializeObject()
{
this.MouseDown += new MouseEventHandler(mouseClickEvent);
}
public void mouseClickEvent(object sender, MouseEventArgs e)
{
MouseHeld = true;
MouseDrag(this, null);
}
}
自定义面板类:
class CustomPanel : Panel
{
private ScrollBar verticalScrollBar;
public ScrollBar VerticalScrollBar { get => verticalScrollBar; set => verticalScrollBar = value; }
public CustomPanel()
{
PanelSetup();
}
public CustomPanel(Size _size)
{
this.Size = _size;
PanelSetup();
}
private void PanelSetup()
{
//Panel setup
this.BackColor = Color.White;
this.Location = new Point(125, 125);
this.BorderStyle = BorderStyle.FixedSingle;
//Behind scrollbar graphic
RectangleShape behindScrollGraphic = new RectangleShape();
behindScrollGraphic.Width = 21;
behindScrollGraphic.Height = this.Height;
behindScrollGraphic.Location = new Point(this.Width - behindScrollGraphic.Width, 0);
behindScrollGraphic.FillStyle = FillStyle.Solid;
behindScrollGraphic.FillColor = SystemColors.Control;
behindScrollGraphic.BorderColor = Color.Transparent;
//adding behind scroll bar to panel
ShapeContainer shapeContainer = new ShapeContainer();
shapeContainer.Shapes.Add(behindScrollGraphic);
this.Controls.Add(shapeContainer);
}
public virtual void AddVerticalScrollBar()
{
ShapeContainer rectangleShapeContainer = new ShapeContainer();
rectangleShapeContainer.Shapes.Add(VerticalScrollBar);
this.Controls.Add(rectangleShapeContainer);
}
public virtual void CreateScrollBar(int _barWidth, int _barHeight)
{
int barWidth = _barWidth;
int barHeight = _barHeight;
VerticalScrollBar = new ScrollBar(this.Width - barWidth - 7, 5, 12, 30);
VerticalScrollBar.FillStyle = FillStyle.Solid;
VerticalScrollBar.FillColor = SystemColors.ControlDark;
VerticalScrollBar.BorderColor = Color.Transparent;
}
}
主要表格类别:
public partial class Form1 : Form
{
private CustomPanel panel;
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
//Form setup
this.Size = new Size(500, 500);
this.BackColor = Color.White;
//Panel setup
panel = new CustomPanel(new Size(250, 250));
panel.CreateScrollBar(10, panel.Height - 2);
panel.AddVerticalScrollBar();
//Scroll Bar
panel.VerticalScrollBar.MouseDrag += new MouseEventHandler(mouseHeldMethod);
//Add panel to form
this.Controls.Add(panel);
}
private void mouseHeldMethod(object sender, MouseEventArgs e)
{
Console.WriteLine("test");
while (panel.VerticalScrollBar.MouseHeld)
{
Console.WriteLine("Held");
}
}
}
答案 0 :(得分:0)
在任何人浪费时间之前解决问题,控件被另一个控件阻挡,即使其他控件显然在其后面,事件调用也没有错。