95 lines
2.7 KiB
C#
95 lines
2.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Deckungsbeitrag.AA_Klassen
|
|
{
|
|
public class TouchScroll
|
|
{
|
|
private ScrollableControl scrollCtr;
|
|
private Point lastMousePosition;
|
|
private bool isDragging;
|
|
private const int DragThreshold = 5; // Pixel, ab denen es "Drag" ist
|
|
|
|
public TouchScroll(ScrollableControl panel)
|
|
{
|
|
this.scrollCtr = panel;
|
|
panel.MouseDown += OnMouseDown;
|
|
panel.MouseMove += OnMouseMove;
|
|
panel.MouseUp += OnMouseUp;
|
|
|
|
// Sub-Controls: Mouse-Events abfangen, aber nicht scrollen lassen
|
|
foreach (Control child in panel.Controls)
|
|
{
|
|
PreventChildScrolling(child);
|
|
}
|
|
|
|
panel.ControlAdded += (s, e) => PreventChildScrolling(e.Control);
|
|
}
|
|
private void PreventChildScrolling(Control child)
|
|
{
|
|
// MouseDown: Nicht zum Scrollen verwenden (Click bleibt)
|
|
child.MouseDown += (s, e) => {
|
|
// Klick-Event wird trotzdem vom Control ausgelöst
|
|
};
|
|
|
|
// MouseMove: Nicht zum Scrollen verwenden
|
|
child.MouseMove += (s, e) => {
|
|
// Bewegung wird vom Kind „verbraucht", nicht zum Panel-Scrollen
|
|
};
|
|
|
|
// Nested Controls auch behandeln
|
|
foreach (Control nested in child.Controls)
|
|
{
|
|
PreventChildScrolling(nested);
|
|
}
|
|
}
|
|
private void OnMouseDown(object sender, MouseEventArgs e)
|
|
{
|
|
isDragging = false;
|
|
lastMousePosition = e.Location;
|
|
}
|
|
|
|
private void OnMouseMove(object sender, MouseEventArgs e)
|
|
{
|
|
// Nur scrollen, wenn LEFT-BUTTON gedrückt UND Bewegung > Threshold
|
|
if (e.Button != MouseButtons.Left) return;
|
|
|
|
int deltaX = Math.Abs(e.X - lastMousePosition.X);
|
|
int deltaY = Math.Abs(e.Y - lastMousePosition.Y);
|
|
|
|
// Erst beim echten Draggen als "Drag" markieren
|
|
if (deltaX > DragThreshold || deltaY > DragThreshold)
|
|
{
|
|
isDragging = true;
|
|
}
|
|
|
|
if (!isDragging) return; // Kurz antippen → kein Scrollen
|
|
|
|
// Korrektur: AutoScrollPosition beim Lesen ist negativ!
|
|
Point currentScroll = scrollCtr.AutoScrollPosition;
|
|
|
|
int actualDeltaX = e.X - lastMousePosition.X;
|
|
int actualDeltaY = e.Y - lastMousePosition.Y;
|
|
|
|
// Positive Werte setzen (bugfix für WinForms)
|
|
scrollCtr.AutoScrollPosition = new Point(
|
|
-currentScroll.X - actualDeltaX, // Negativität umkehren
|
|
-currentScroll.Y - actualDeltaY
|
|
);
|
|
|
|
lastMousePosition = e.Location;
|
|
}
|
|
|
|
private void OnMouseUp(object sender, MouseEventArgs e)
|
|
{
|
|
// Wenn es kein Drag war → Click wird normal vom Control ausgelöst
|
|
isDragging = false;
|
|
}
|
|
}
|
|
}
|