Hallo Community,
ich bin gerade ein wenig am verzweifeln und hoffe das mir hier jemand helfen kann.
Folgendes Problem:
Ich will ein eigenes Steuerelement, abgeleitet von einem TableLayoutPanel, basteln.
Dabei soll per Klick, die Zelle, auf der sich der Mauszeiger befindet, eingefärbt werden.
Soweit funktioniert das auch.
Nun will ich allerding nicht jede Zelle einzeln anklicken müssen sonder auch wenn ich mit der Maus über weitere Zellen fahre, sollen diese eingefärbt werden.
Meine bisherige Lösung (siehe unten) geht schon in die richtige Richtung aber wenn ich die Maus etwas zu schnell bewege werden nicht alle Zellen eingefärbt.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102:
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Diagnostics;
namespace test2 { public partial class zeichenPanel : TableLayoutPanel { Rectangle clicked = new Rectangle(); bool wasClick = false;
public zeichenPanel() { this.MouseDown += drawPanel_MouseDown; this.MouseUp += drawPanel_MouseUp; this.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single; this.Paint += drawPanel_Paint; this.MouseMove += drawPanel_MouseMove; }
private void drawPanel_MouseDown(object sender, MouseEventArgs e) { int row = 0; int verticalOffset = 0; int clickx = e.X; int clicky = e.Y; foreach (int h in this.GetRowHeights()) { int column = 0; int horizontalOffset = 0; foreach (int w in this.GetColumnWidths()) { Rectangle rectangle = new Rectangle(horizontalOffset, verticalOffset, w, h); if (rectangle.Contains(e.Location)) { clicked = rectangle; wasClick = true; this.Invalidate(rectangle); return; } horizontalOffset += w; column++; } verticalOffset += h; row++; } } private void drawPanel_MouseMove(object sender, MouseEventArgs e) { if (wasClick) { int row = 0; int verticalOffset = 0; int clickx = e.X; int clicky = e.Y; foreach (int h in this.GetRowHeights()) { int column = 0; int horizontalOffset = 0; foreach (int w in this.GetColumnWidths()) { Rectangle rectangle = new Rectangle(horizontalOffset, verticalOffset, w, h); if (rectangle.Contains(e.Location)) { clicked = rectangle; wasClick = true; this.Invalidate(rectangle); return; } horizontalOffset += w; column++; } verticalOffset += h; row++; } } }
void drawPanel_MouseUp(object sender, MouseEventArgs e) { wasClick = false; }
private void drawPanel_Paint(object sender, PaintEventArgs e) { if (wasClick) { Graphics g = e.Graphics; Rectangle r = clicked; g.FillRectangle(Brushes.Red, r); } } } } |
Vielleicht hat noch jemand eine besser Idee, anstatt dies mit einem TableLayoutPanel zu realisieren.
Vielen Dank im Voraus