WPF中通过代码设置控件的坐标

用WPF做贪吃蛇小游戏时,发现了一个问题:

贪吃蛇的移动,我是通过不断刷新Rectangle来实现(贪吃蛇的身体由一组Rectangle组成),因此需要不断调整Rectangle的坐标,但是WPF中没有Location的相关设置

在网上查到可以用Thickness实现

Rectangle Rec = new Rectangle();
Rec.Margin = new Thickness(1, 2, 3, 4);

但总是调不准

其实可以通过Canvas实现

Canvas介绍

用代码实现Canvas:

// Create the application‘s main window
mainWindow = new Window ();
mainWindow.Title = "Canvas Sample";

// Create the Canvas
myParentCanvas = new Canvas();
myParentCanvas.Width = 400;
myParentCanvas.Height = 400;

// Define child Canvas elements
myCanvas1 = new Canvas();
myCanvas1.Background = Brushes.Red;
myCanvas1.Height = 100;
myCanvas1.Width = 100;
Canvas.SetTop(myCanvas1, 0);
Canvas.SetLeft(myCanvas1, 0);

myCanvas2 = new Canvas();
myCanvas2.Background = Brushes.Green;
myCanvas2.Height = 100;
myCanvas2.Width = 100;
Canvas.SetTop(myCanvas2, 100);
Canvas.SetLeft(myCanvas2, 100);

myCanvas3 = new Canvas();
myCanvas3.Background = Brushes.Blue;
myCanvas3.Height = 100;
myCanvas3.Width = 100;
Canvas.SetTop(myCanvas3, 50);
Canvas.SetLeft(myCanvas3, 50);

// Add child elements to the Canvas‘ Children collection
myParentCanvas.Children.Add(myCanvas1);
myParentCanvas.Children.Add(myCanvas2);
myParentCanvas.Children.Add(myCanvas3);

// Add the parent Canvas as the Content of the Window Object
mainWindow.Content = myParentCanvas;
mainWindow.Show ();

也可以通过添加Canvas控件来实现,添加之后

Rectangle Rec = new Rectangle();
Canvas.SetLeft(Rec, posX);
Canvas.SetTop(Rec, posY);
你添加的canvas名字.Children.Add(Rec);
时间: 2024-08-01 22:14:00

WPF中通过代码设置控件的坐标的相关文章

android在代码中四种设置控件背景颜色的方法(包括RGB)

转载请注明出处: http://blog.csdn.net/fth826595345/article/details/9208771  TextView tText=(TextView) findViewById(R.id.textv_name); //第1种: tText.setTextColor(android.graphics.Color.RED);//系统自带的颜色类 // 第2种: tText.setTextColor(0xffff00ff);//0xffff00ff是int类型的数据

转载 [WPF][C#]在WPF中内嵌WindowsForm控件-使用WindowsFormsControlLibrary

[WPF][C#]在WPF中内嵌WindowsForm控件-使用WindowsFormsControlLibrary 在[WPF][C#]在WPF中内嵌WindowsForm控件一文中为各位介绍了直接在WPF中使用XAML来嵌入Windows Form控件的作法,不过不是每个人都喜欢写XAML,而且有时候会需要把已经存在的Windows Form应用程序嵌入到WPF中,所以这次就来跟大家介绍怎么使用参考dll档的方式,把dll中的Windows Form加到WPF中. 都说了要使用Windows

WPF 中如何使用第三方控件 ,可以使用WindowsFormsHost 类

允许在 WPF 页面上承载 Windows Forms控件的元素. 命名空间:   System.Windows.Forms.Integration 程序集:   WindowsFormsIntegration(在 WindowsFormsIntegration.dll 中) 用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2

android 中通过代码创建控件

package bvb.de.openadbwireless.circle; import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.view.View; impor

在WPF中内嵌WindowsForm控件-使用WindowsFormsControlLibrary

原来有一个WINFORM项目的功能模块希望集成到新的WPF项目中,怎样集成才最简单? 思路:将原来的WINFORM项目类型改为WindowsFormsControlLibrary类型就OK了. 步骤: 1.所以我们就直接建立一个WindowsFormsControlLibrary项目吧!接着我在该项目中新增Windows Form,为Form1.也就是将原来的项目类型改造为WindowsFormsControlLibrary项目. 2新建Wpf项目 (1).添加两个引用:WindowsForms

在WPF中引用WinForm的控件

以ArcEngine为例: mapControl = new AxMapControl(); MapHost.Child = mapControl; //MapHost为WindowsFormHost控件 ((System.ComponentModel.ISupportInitialize)(this.mapControl)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.mapControl)).EndInit();

android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法

http://blog.csdn.net/fth826595345/article/details/9208771 主题 TextView 转载请注明出处: http://blog.csdn.net/fth826595345/article/details/9208771 直接上代码吧,注释解说: TextView tText=(TextView) findViewById(R.id.textv_name); //第1种: tText.setTextColor(android.graphics.

VC++ 设置控件显示文本的前景色、背景色以及字体

在每个控件开始绘制之前,都会向其父窗口发送WM_CTLCOLOR通告消息,在该消息的处理函数中,可以设置控件显示文本的前景色.背景色以及字体.该消息处理函数还要求返回一个画刷的句柄,用于在控件具体的绘制之前擦除其客户区. HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO:  Retur

获取屏幕高宽,在代码中设置控件大小的方法

获取屏幕高宽的方法: 1 import android.view.Display; 2 import android.view.WindowManager; 3 WindowManager windowManager = getWindowManager(); 4 Display display = windowManager.getDefaultDisplay(); 5 if(display.getWidth()==480 && display.getHeight()== 272 ||