需求:
有一个不变的背景(可以想象成魂斗罗的背景画面)
每帧都会有元素在背景中移动(例如魂斗罗中的州长)
要求比较复杂的界面有流程感,不闪。支持滚轮放大缩小就更好了
有一个不变的背景(可以想象成魂斗罗的背景画面)
每帧都会有元素在背景中移动(例如魂斗罗中的州长)
要求比较复杂的界面有流程感,不闪。支持滚轮放大缩小就更好了
解决方案
50
用Winfrom绘制的话估计会比较困难,界面闪动是硬伤,可以考虑用WebBrowser中套HTML页,绘图用JS实现,同时也可以实现JS和C#端方法的相互调用,实现数据传输,具体百度WebBrowser与WinFrom交互。
50
这种情况下,WinForm不太适合。
public partial class Form1 : Form { public Form1() { InitializeComponent(); this.DoubleBuffered = true; this.Text = "用上下左右键移动"; string uri = "https://kbdevstorage1.blob.core.windows.net/asset-blobs/19191_en_1"; byte[] data = new System.Net.WebClient().DownloadData(uri); this.bgBmp = new Bitmap(new System.IO.MemoryStream(data)); } protected override void OnKeyDown(KeyEventArgs e) { if (e.KeyData == Keys.Up) { sprite = Cursors.PanNorth; offsetY--; } if (e.KeyData == Keys.Down) { sprite = Cursors.PanSouth; offsetY++; } if (e.KeyData == Keys.Left) { sprite = Cursors.PanWest; offsetX--; } if (e.KeyData == Keys.Right) { sprite = Cursors.PanEast; offsetX++; } this.Invalidate(); } protected override void OnMouseWheel(MouseEventArgs e) { zoom += e.Delta; this.Invalidate(); } protected override void OnPaint(PaintEventArgs e) { var diff = System.Drawing.Size.Subtract(this.bgBmp.Size, this.ClientRectangle.Size); var srcRect = new Rectangle(diff.Width / 2 + offsetX * 4, diff.Height / 2 + offsetY * 4, ClientRectangle.Width, ClientRectangle.Height); srcRect.Inflate(zoom, zoom); e.Graphics.DrawImage(bgBmp, ClientRectangle, srcRect, GraphicsUnit.Pixel); Rectangle spriteRect = new Rectangle(ClientRectangle.Width / 2, ClientRectangle.Height / 2, 0, 0); spriteRect.Inflate(sprite.Size.Width / 2, sprite.Size.Height / 2); sprite.Draw(e.Graphics, spriteRect); } Cursor sprite = Cursors.NoMove2D; Bitmap bgBmp; int offsetX, offsetY, zoom; }