发牌区代码-已经完成

本代码主要实现了纸牌的出牌动画。鼠标选中的牌前置,定义的牌可以任意指定大小,程序里面是5张,给每张牌定义了鼠标点击事件。

 

using System; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Media.Imaging; namespace SilverlightApplication1 { public partial class 玩家出牌区 : UserControl { public 玩家出牌区() { // Required to initialize variables InitializeComponent(); LoadPage(); } void LoadPage() { int count=5; //Image[] im=new Image[count]; int cc=-550; for(int i=1;i<=count;i++) { Rectangle r=new Rectangle(); r.Name="r"+i.ToString(); r.Fill = new ImageBrush() { ImageSource = new BitmapImage(new Uri("img/sha.jpg", UriKind.Relative))}; r.Width=96; r.Height=134; r.Margin=new Thickness(cc,80,0,0); LayoutRoot.Children.Add(r); cc+=200; } int A=0; foreach (var rec in LayoutRoot.Children) { if (rec is Rectangle) { var onerec = rec as Rectangle; //注册鼠标点击事件 onerec.MouseLeftButtonDown += new MouseButtonEventHandler(rec_MouseLeftButtonDown); } } //MessageBox.Show(A.ToString()); } void CardReset() { int ccc=-750; foreach (var rec in LayoutRoot.Children) { if (rec is Rectangle) { var onerec = rec as Rectangle; onerec.Margin=new Thickness(ccc,80,0,0);//图片复位 } ccc+=200; } } private void rec_MouseLeftButtonDown(object sender,MouseButtonEventArgs e) { //e.OriginalSource //(e.Source as FrameworkElement).Name; //MessageBox.Show((onerec.Margin.Left.ToString())); CardReset(); foreach (var rec in LayoutRoot.Children) { if (rec is Rectangle) { var onerec = rec as Rectangle; if(onerec.Name==(e.OriginalSource as FrameworkElement).Name) { double ileft = onerec.Margin.Left; double iTop = onerec.Margin.Top; MessageBox.Show(onerec.Name); onerec.Margin=new Thickness(ileft,40,0,0); //int left=onerec. //onerec.SetValue(onerec.Margin..LeftProperty,ileft+0); //设置矩形的起始X坐标 //onerec.SetValue(Canvas.TopProperty, ileft+10); } } } //r1.Margin=new Thickness(400,299,136,8);//图片前移 } } }   

 

//这次用到Rectangle做牌的承载体,需要引用using System.Windows.Media.Imaging;

麻烦的地方就是,如何访问程序中定义的Rectangle对象,烦死了。

 

搞定了,参见代码

你可能感兴趣的:(发牌区代码-已经完成)