基于Windows窗体应用(.NET Framework)开发
partial关键字
用这个关键字修饰类,提示你看到的类只是一部分,剩下的声明可能在其他的文件中。(partial关键字允许一个分成好几个部分在不同的文件中声明实现)
namespace TankWorld { public partial class Form1 : Form { public Form1() { InitializeComponent(); } } }
namespace TankWorld { partial class Form1 { } }
值得注意的是,不用重复写类的权限和继承的父类。
设置窗体的开始位置
namespace TankWorld { public partial class Form1 : Form { public Form1() { InitializeComponent(); //this.StartPosition = FormStartPosition.CenterScreen; //将窗体开始的位置置于屏幕中间 //手动设置窗体的位置 this.StartPosition = FormStartPosition.Manual; this.Location = new Point(300, 200); } } }
用GDI画直线和字体
private void Form1_Paint(object sender, PaintEventArgs e) { //用GDI画直线 Graphics g = this.CreateGraphics(); //创建画布对象,将整个窗体当作画布 Pen p = new Pen(Color.Black); //创建画笔,设置笔的颜色 g.DrawLine(p, new Point(0, 0), new Point(100, 100)); //画线 g.DrawString("芜湖", new Font("隶书", 20), new SolidBrush(Color.Red), new Point(100, 100)); //画一个字体 }
绘制图片(在Resources.resx中导入图片资源)
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = this.CreateGraphics(); //创建画布 Image image = Resources.Boss; //获取boss图片 Bitmap bm = Resources.Star1; //获取Star1图片 bm.MakeTransparent(Color.Black); //将star1图片中黑色的部分变透明 g.DrawImage(bm, 150, 150); //绘制star1图片 g.DrawImage(image, 200, 200); //绘制boss图片 }
还在开发中。。。
相关的项目工程(vs2019)https://github.com/Fake-Trump/Fake-Trump.github.io/tree/main/TankWorld