|
public partial class MainForm
: Form
{
public MainForm()
{
InitializeComponent();
}
private int rows;
private int columns;
private void btnDraw_Click(object
sender, EventArgs e)
{
rows = int.Parse(this.txtRows.Text);
columns = int.Parse(this.txtColumns.Text);
CreateCells(rows, columns);
this.Refresh(); }
protected override void OnPaint(PaintEventArgs
e)
{
if (list == null) return;
if (list.Count == 0) return;
DrawLayout(e.Graphics);
} private System.Collections.ArrayList
list;
private void CreateCells(int rows, int columns)
{
list = new System.Collections.ArrayList();
CellBase cellBase = null;
Image img = Image.FromFile(@"bell.gif");
int counter = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
// cellBase = new TableCell();
cellBase = new TableCell();
cellBase.RowNo = i;
cellBase.ColumnNo = j;
cellBase.TableNo = counter;
if (chkBackColor.Checked)
cellBase = new FillableCellDecorator(Color.Purple,
cellBase);
if (chkText.Checked)
cellBase = new TextCellDecorator(counter.ToString(),
Color.Yellow, cellBase);
if (chkImage.Checked)
cellBase = new IconCellDecorator(img, cellBase);
list.Add(cellBase);
counter++;
}
}
}
// DrawLayout method using offscreen rendering
to avoid flicker.
private void DrawLayout(Graphics g)
{
int cellWidth = 100;
int cellHeight = 50;
Bitmap bmpOff;
Graphics gxOff;
// Create a bitmap
the size of the form.
bmpOff = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
SolidBrush blueBrush = new SolidBrush(Color.Blue);
Pen whitePen = new Pen(Color.White, 3);
// Create a Graphics
object that is not of the screen.
gxOff = Graphics.FromImage(bmpOff);
// gxOff.FillRectangle(new SolidBrush(Color.Red),
0, 0, bmpOff.Width, bmpOff.Height);
foreach (CellBase cell in
list)
{
cell.Draw(gxOff, cellWidth, cellHeight);
}
// Render the off-screen bitmap onto the screen.
g.DrawImage(bmpOff, 0, 0, ClientRectangle, GraphicsUnit.Pixel);
// Dispose created
Graphic object.
gxOff.Dispose();
}
protected override void OnMouseUp(MouseEventArgs
e)
{
if (list == null) return;
if (list.Count == 0) return;
foreach (CellBase cell in
list)
{
if (cell.Intersects(e.X, e.Y))
{
Graphics g = this.CreateGraphics();
DrawLayout(g);
this.lblCellTypeUnClicked.Text
= cell.ToString();
// Wrapped object to have a highlight power.
CellBase clickedCell = new BorderedCellDecorator(Color.Orange,
cell);
clickedCell.Draw(g, 100, 50);
this.lblCellTypeClicked.Text
= clickedCell.ToString();
g.Dispose();
break;
}
}
}
}
|