Showing posts with label UltraGrid. Show all posts
Showing posts with label UltraGrid. Show all posts

Jun 5, 2009

Draw Rectangle Over UltraGrid

This is some basic stuff about how to draw lines in C#:
Drawing lines in C#

This tries to help with the flickering problem. Didn't get it to work over an UltraGrid:
Don't Flicker! Double Buffer!

This works on a Windows Form but couldn't get it to work over a control containing an UltraGrid, and it uses Win32 API to draw:
C# Rubber Rectangle

This works fine in a Windows Form but gives artifacts over the UltraGrid:
How to draw a rubber band rectangle or a focus rectangle in Visual C#


I also had my own implementation which flickered:



private Point _mouseStart, _mouseCurrent;
private bool _bPaintSelectionRect = false;

private void uGrid_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_mouseStart = e.Location;
...
}
}

private void uGrid_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// TODO: Select cells in the rectangle
_bPaintSelectionRect = false;
}
}

private void uGrid_MouseMove(object sender, MouseEventArgs e)
{
// If user is holding the left button and dragging, draw a rectangle
if (e.Button == MouseButtons.Left && _mouseStart != null)
{
// Save current mouse position as it moves
_mouseCurrent = e.Location;
// Allow the paint event to draw the rectangle
_bPaintSelectionRect = true;
}

}

private void uGrid_Paint(object sender, PaintEventArgs e)
{
if (_bPaintSelectionRect)
{
Graphics g = e.Graphics;
Pen solidPen = new Pen(new SolidBrush(Color.Gray));
//solidPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
solidPen.DashPattern = new float[] { 1, 1 };

// Normalize the rectangle
int x = _mouseCurrent.X > _mouseStart.X ? _mouseStart.X : _mouseCurrent.X;
int y = _mouseCurrent.Y > _mouseStart.Y ? _mouseStart.Y : _mouseCurrent.Y;
int width = Math.Abs(_mouseCurrent.X - _mouseStart.X);
int height = Math.Abs(_mouseCurrent.Y - _mouseStart.Y);

g.DrawRectangle(solidPen, x, y, width, height);

// Force the redraw (causes flicker) - don't call Invalidate()
// and you won't see the rectangle
uGrid.Invalidate();
}
}