Advertisement
| Hall of Fame |
|
[x]
Posted via EE Mobile
|
||
Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again. |
||
| Question |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
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: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace gridCustomControl
{
public delegate void MouseClickEventHandler(object sender, MouseEventArgs e);
public delegate void MouseMoveEventHandler(object sender, MouseEventArgs e);
public partial class GridCustomControl : UserControl
{
#region Private Members
private ColorGrid _grid;
private int _cols = 3;
private int _rows = 3;
private int _width = 10; // Cell width
private int _height = 10; // Cell height
private int _zoom = 1;
private Color _cellColor = Color.White;
private Color _borderColor = Color.Black;
private const int WM_HSCROLL = 0x114;
private const int WM_VSCROLL = 0x115;
#endregion
#region Events
// The events of this class of delegates assigned to them
// outside this class in the namespace.
public event MouseClickEventHandler MouseClickedEvent;
public event MouseMoveEventHandler MouseMovedEvent;
#endregion
public GridCustomControl()
{
// We do not want to call InitializeComponent() from the
// Designer of this custom control because we want the user
// to be able to set the size of the control at design time.
InitializeComponent();
// The following SetStyles are needed for double buffering:
this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint, true);
this._grid = new ColorGrid(this._cols, this._cols,
this._cellColor, this._borderColor);
this.SetGridWindow();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
int cellHeight = this.CellHeight * this.Zoom;
int cellWidth = this.CellWidth * this.Zoom;
int nbrColumns = this.Columns;
int nbrRows = this.Rows;
Size scrollOffset;
// Draw cells
for (int row = 0; row < nbrRows; row++)
{
for (int col = 0; col < nbrColumns; col++)
{
// Get negative x & y coordinates of scroll
scrollOffset = new Size(this.AutoScrollPosition);
// Calculate new cell location
// Detailed Explanation of example of offsets:
/* Assume the first cell top-left corner is located at x:0 y:0,
* or (0,0). If there aren't any scrolling offsets, and the cell
* at (0,0) intersects with the ClipRectangle, then it will need
* to be painted. If there was scrolling of x:-100 (width) and
* y:-100 (height) then the cell at (-100, -100) will not be drawn
* because it is not within the 'viewport' and therefore there
* isn't a chance it intersects with the ClipRectangle. But if the
* original cell location was (100, 100) and the scrolling offset
* size was (-100,-100) that would draw the cell at (0,0) of the
* control. So the rule is that if the final location of the cell
* to be drawn is not negative, then it is within the viewport and
* MAY be redrawn if it intersects with the ClipRectangle.
*/
Point cellLocation = new Point(col * cellWidth + col + scrollOffset.Width,
row * cellHeight + row + scrollOffset.Height);
// Define cell parameter
Rectangle cellRect = new Rectangle(cellLocation.X,
cellLocation.Y,
cellWidth, cellHeight);
// Only draw cell if it needs to be painted
if (cellRect.IntersectsWith(e.ClipRectangle))
{
g.FillRectangle(new SolidBrush(
_grid.getFillColor(row, col)), cellRect);
g.DrawRectangle(new Pen(
_grid.getBorderColor(row, col), 1), cellRect);
}
}
}
}
private void SetGridWindow()
{
// 1 is added otherwize the most very most right line and
// the very most bottom line will not show.
this.AutoScrollMinSize = new Size(this.CellWidth *
this.Columns * this.Zoom + this.Columns,
this.CellHeight * this.Rows * this.Zoom + this.Rows);
this.Invalidate(); // Redraw the whole control
}
public Point RCCoordinate(int x, int y)
{
double fltX;
double fltY;
Point p;
Size scrollOffset;
fltX = Convert.ToDouble(x);
fltY = Convert.ToDouble(y);
scrollOffset = new Size(this.AutoScrollOffset);
p = new Point(Convert.ToInt32(Math.Ceiling(Convert.ToDouble
(x / this._width / this._zoom))) + scrollOffset.Width,
Convert.ToInt32(Math.Ceiling(Convert.ToDouble
(y / this._height / this._zoom))) + scrollOffset.Height);
return p;
}
#region Properties
/***
* Properties
* */
[Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
Description("The number of columns in the grid.")]
public int Columns
{
get { return _cols; }
set
{
_cols = value;
this._grid = new ColorGrid(this._cols, this._cols,
this._cellColor, this._borderColor);
this.SetGridWindow();
}
}
[Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
Description("The number of rows in the grid.")]
public int Rows
{
get { return _rows; }
set
{
_rows = value;
this._grid = new ColorGrid(this._cols, this._cols,
this._cellColor, this._borderColor);
this.SetGridWindow();
}
}
[Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
Description("The width, in pixels, of one cell.")]
public int CellWidth
{
get { return _width; }
set
{
if (value > 0)
{
_width = value;
this.SetGridWindow();
}
}
}
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
Description("The height, in pixels, of one cell.")]
public int CellHeight
{
get { return _height; }
set
{
if (value > 0)
{
_height = value;
this.SetGridWindow();
}
}
}
public int Zoom
{
get { return _zoom; }
set
{
if (value >= 1)
{
_zoom = value;
this.SetGridWindow();
}
}
}
[Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
Description("Height & width of this control")]
public new Size Size
{
get { return base.Size; }
set
{
if((value.Width > 0) && (value.Height > 0))
base.Size = value;
}
}
[Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
Description("The color of the background inside of the cell.")]
public Color FillColor
{
get { return _cellColor; }
set
{
_cellColor = value;
this.Invalidate();
}
}
[Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
Description("The color of the border around the cell.")]
public Color BorderColor
{
get { return _borderColor; }
set
{
_borderColor = value;
this.Invalidate();
}
}
#endregion
#region Mouse Events
/***
* Mouse Events
* */
protected void GridCustomControl_MouseMove(object sender, MouseEventArgs e)
{
if (MouseMovedEvent != null)
{
MouseMovedEvent(sender, e);
Console.WriteLine("GridCustomControl_MouseMove");
}
}
protected void GridCustomControl_MouseClick(object sender, MouseEventArgs e)
{
if (MouseClickedEvent != null)
{
Point p;
p = RCCoordinate(e.X, e.Y);
_grid.setColor(p.X, p.Y,this._cellColor,this._borderColor);
MouseClickedEvent(sender, e);
Console.WriteLine("GridCustomControl_MouseClick");
this.Invalidate();
}
}
protected override void WndProc(ref Message m)
{
if ((m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL)
&& (((int)m.WParam & 0xFFFF) == 5))
{
// Change SB_THUMBTRACK to SB_THUMBPOSITION
m.WParam = (IntPtr)(((int)m.WParam & ~0xFFFF)
| 4);
}
base.WndProc(ref m);
}
#endregion
}
}
|