我嘗試製作Connect Four遊戲,並遇到問題。我想要洗衣機秋天的動畫。這是我的代碼:在用戶控制中繪製圖像
public class BoardA : UserControl
{
private static int xSize = 7, ySize = 6;
private int slotWidth, slotHeight;
private SlotState[,] states;
private int currentColumn;
private bool paintBoard;
public BoardA()
{
states = new SlotState[xSize, ySize];
slotWidth = 70;
slotHeight = 70;
currentColumn = -1;
MouseMove += new MouseEventHandler(BoardA_MouseMove);
MouseLeave += new EventHandler(BoardA_MouseLeave);
Click += new EventHandler(BoardA_Click);
paintBoard = true;
}
void BoardA_Click(object sender, EventArgs e)
{
if (ColumnClick != null)
ColumnClick(this, new ColumnClickedEventArgs(currentColumn));
}
void BoardA_MouseLeave(object sender, EventArgs e)
{
this.CreateGraphics().FillRectangle(new SolidBrush(Color.Transparent), currentColumn * slotWidth, 0, slotWidth, slotHeight);
currentColumn = -1;
}
void BoardA_MouseMove(object sender, MouseEventArgs e)
{
int newColumn = e.X/slotWidth;
if (newColumn != currentColumn)
{
Graphics g = this.CreateGraphics();
g.FillRectangle(new SolidBrush(Color.Transparent), currentColumn * slotWidth, 0, slotWidth, slotHeight);
currentColumn = newColumn;
g.DrawImage(Resources.Down_arrow, currentColumn * slotWidth, 0, slotWidth, slotHeight);
}
}
#region functions paint
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (paintBoard)
{
for (int i = 0; i < xSize; i++)
{
for (int j = 0; j < ySize; j++)
{
DrawSlot(states[i, j], i, j);
}
}
}
e.Graphics.DrawImage(Resources.Down_arrow, currentColumn * slotWidth, 0, slotWidth, slotHeight);
}
public void UpdateGraphic()
{
RecreateHandle();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
#endregion
public SlotState this[int columnIndex, int rowIndex]
{
get
{
return states[columnIndex, rowIndex];
}
set
{
states[columnIndex, rowIndex] = value;
DrawSlot(value, columnIndex, rowIndex);
}
}
public Size BoardSize
{
get { return new Size(slotWidth, slotHeight); }
}
public event ColumnClickedEventHandler ColumnClick;
public bool IsFull
{
get
{
foreach (var item in states)
{
if (item == SlotState.Empty)
return false;
}
return true;
}
}
public void Clear()
{
for (int i = 0; i < xSize; i++)
{
for (int j = 0; j < ySize; j++)
{
this[i, j] = SlotState.Empty;
}
}
}
public bool EnterToColumn(int Column, SlotState state)
{
if (Column < 0 || Column >= xSize)
throw new ArgumentOutOfRangeException("Column");
if (state == SlotState.Empty)
return false;
Graphics g = this.CreateGraphics();
for (int i = ySize - 1; i >= 0; i--)
{
if (this[Column, i] == SlotState.Empty)
{
for (int j = 0; j < ySize - 1; j++)
{
for (int k = 0; k < slotHeight; k++)
{
g.DrawImage(Resources.RealRed_Slot, Column * slotWidth, (j + 1) * slotHeight + k, slotWidth, slotHeight); // Not working !!!!!
g.DrawImage(Resources.RealEmpty_Slot, Column * slotWidth, (j + 1) * slotHeight, slotWidth, slotHeight); // Not working !!!!!
g.DrawImage(Resources.RealEmpty_Slot, Column * slotWidth, (j + 2) * slotHeight, slotWidth, slotHeight); // Not working !!!!!
System.Threading.Thread.Sleep(5);
}
}
this[Column, i] = state;
return true;
}
}
return false;
}
public bool PopTopOfColumn(int Column)
{
if (Column < 0 || Column >= xSize)
throw new ArgumentOutOfRangeException("Column");
for (int i = ySize - 1; i >= 0; i--)
{
if (this[Column, i] != SlotState.Empty)
{
this[Column, i] = SlotState.Empty;
return true;
}
}
return false;
}
private void DrawSlot(SlotState state, int columnIndex, int rowIndex)
{
Graphics g = this.CreateGraphics();
switch (state)
{
case SlotState.Empty:
g.DrawImage(Resources.RealEmpty_Slot, columnIndex * slotWidth, (rowIndex + 1) * slotHeight, slotWidth, slotHeight);
break;
case SlotState.Red:
g.DrawImage(Resources.Red_Slot, columnIndex * slotWidth, (rowIndex + 1) * slotHeight, slotWidth, slotHeight);
break;
case SlotState.Orange:
g.DrawImage(Resources.Orange_Slot, columnIndex * slotWidth, (rowIndex + 1) * slotHeight, slotWidth, slotHeight);
break;
default:
break;
}
}
}
And ... does not working。我必須做什麼? 如果我在繪畫中這樣做並刷新它閃爍。
請不要用「c#」等來標題。這就是標籤的用途。 – 2012-02-13 20:16:22
如果你想防止filckering,你可以嘗試從PictureBox派生你的類而不是UserControl。 – 2012-02-13 20:49:24