今天我一直在搜索论坛和Stack Overflow试图找出在我的表单上的面板周围拖动图像(图片框)时消除烦人闪烁的正确方法。我正在尝试为我的朋友复制纸牌游戏,我可以在家里玩。我从Picturebox类派生,以便我可以添加一些额外的属性到我的对象。以下是该类的代码:
class Card : PictureBox
{
public string CardId { get; set; }
public string CardName { get; set; }
public string CardColor { get; set; }
public string CardType { get; set; }
public string CardRarity { get; set; }
public bool Tapped { get; set; }
public bool Revealed { get; set; }
}
我有一个面板设置,以便当玩家双击他们手中的牌时,它会将其添加到面板中。然后我设置了一个拖放,这样他们就可以在面板周围移动卡片了。这是代码。
int posX;
int posY;
private void DragCard(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
dragging = true;
posX = e.X;
posY = e.Y;
}
}
private void isDragging(object sender, MouseEventArgs e)
{
if (dragging == true)
{
((Card)sender).Left += e.X - posX;
((Card)sender).Top += e.Y - posY;
((Card)sender).BringToFront();
}
}
private void DoneDrag(object sender, MouseEventArgs e)
{
dragging = false;
}
问题在于我在面板中有一个背景图像,因此当卡片移动时,它会在移动之前显示原始位置的位置。我搜索和搜索了很多不同的方法来摆脱这个,但似乎没有什么对我有用。如果有人能用蹩脚的语言解释它,我会非常感激,因为我对编码很新。非常感谢您的帮助。为了防止我遗漏任何问题,我已经添加了下面的其余代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Card> PlayerDeck = new List<Card>();
Card curCard;
bool dragging = false;
int posX;
int posY;
private void DragCard(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
dragging = true;
posX = e.X;
posY = e.Y;
}
}
private void isDragging(object sender, MouseEventArgs e)
{
if (dragging == true)
{
((Card)sender).Left += e.X - posX;
((Card)sender).Top += e.Y - posY;
((Card)sender).BringToFront();
}
}
private void DoneDrag(object sender, MouseEventArgs e)
{
dragging = false;
}
private void Form1_Load(object sender, EventArgs e)
{
btnLoadDeck.Enabled = true;
btnShuffle.Enabled = true;
numericHealth.Value = 20;
numericHealth.Enabled = false;
cardDeck.Enabled = true;
cardGrave.Enabled = false;
listStatus.Enabled = true;
}
private void btnLoadDeck_Click(object sender, EventArgs e)
{
if (PlayerDeck.Count > 0)
{
MessageBox.Show("Deck Already Loaded...");
return;
}
int x = 0;
foreach (Card c in Deck.LoadDeck())
{
Card c1 = new Card();
c1.CardId = c.CardId;
c1.CardName = c.CardName;
c1.CardColor = c.CardColor;
c1.CardType = c.CardType;
c1.CardRarity = c.CardRarity;
c1.Size = new Size(100, 120);
c1.Location = new Point(0, 0);
c1.SizeMode = PictureBoxSizeMode.StretchImage;
c1.BorderStyle = BorderStyle.Fixed3D;
c1.BackColor = Color.Black;
c1.ContextMenuStrip = conMenuHand;
c1.Image = Image.FromFile(@"C:\Users\vildez\Desktop\Virtual Magic\Cards\c72b2bb7-e6da-49bb-9bd5-a538db0761ee\Cards\" + c.CardId + ".jpg");
c1.Name = "Card" + x.ToString();
c1.MouseEnter += ViewCard;
c1.DoubleClick += c1_DoubleClick;
PlayerDeck.Add(c1);
x++;
}
}
void c1_DoubleClick(object sender, EventArgs e)
{
PlayCard(sender,null);
}
void ViewCard(object sender, EventArgs e)
{
cardViewer.Image = Image.FromFile(@"C:\Users\vildez\Desktop\Virtual Magic\Cards\c72b2bb7-e6da-49bb-9bd5-a538db0761ee\Cards\" + ((Card)sender).CardId + ".jpg");
}
private void cardDeck_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
panelHand.Controls.Add(PlayerDeck[0]);
PlayerDeck.RemoveAt(0);
}
}
public void PlayCard(object sender, EventArgs e)
{
panelHand.Controls.Remove(((Card)sender));
Card c1 = new Card();
c1.CardId = ((Card)sender).CardId;
c1.CardName = ((Card)sender).CardName;
c1.CardColor = ((Card)sender).CardColor;
c1.CardType = ((Card)sender).CardType;
c1.CardRarity = ((Card)sender).CardRarity;
c1.Size = new Size(50, 70);
c1.SizeMode = PictureBoxSizeMode.StretchImage;
c1.BorderStyle = BorderStyle.Fixed3D;
c1.BackColor = Color.Black;
c1.Location = new Point(10, 10);
c1.Image = Image.FromFile(@"C:\Users\vildez\Desktop\Virtual Magic\Cards\c72b2bb7-e6da-49bb-9bd5-a538db0761ee\Cards\" + c1.CardId + ".jpg");
c1.MouseDown += new MouseEventHandler(DragCard);
c1.MouseMove += new MouseEventHandler(isDragging);
c1.MouseUp += new MouseEventHandler(DoneDrag);
c1.MouseEnter += new EventHandler(ViewCard);
int x = 0;
for (int i = 0; i < panelPlayer.Width; i++)
{
if (i + 50 > panelHand.Width)
{
i = 0;
x += 70 + 1;
}
if (panelPlayer.GetChildAtPoint(new Point(i, x)) == null)
{
c1.Location = new Point(i, x);
panelPlayer.Controls.Add(c1);
break;
}
}
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
curCard = ((ContextMenuStrip)sender).SourceControl as Card;
}
private void tsMenuItemPlayCard_Click(object sender, EventArgs e)
{
PlayCard(curCard, null);
}
private void panelPlayer_Paint(object sender, PaintEventArgs e)
{
}
}
}