简体   繁体   English

为.net Compact Framework构建自定义绘制控件

[英]Build a custom drawn control for .net compact framework

I just started on the .net compact framework. 我刚开始使用.net紧凑框架。 I want to draw a Sudoku field on the screen. 我想在屏幕上绘制一个数独字段。 So I put down a PictureBox and defined a method for the Paint event: 因此,我放下一个PictureBox并为Paint事件定义了一个方法:

private void pictureBoxPlayfield_Paint(object sender, PaintEventArgs e)
{
    // use e.Graphics to draw the grid, numbers and cursor
}

This works, but you can see as the grid is drawn. 这可行,但是您可以在绘制网格时看到。 So my question is, what is the right/better way to create such a custom control? 所以我的问题是,创建这种自定义控件的正确/更好方法是什么? Is there maybe a way to enable double buffering? 有没有可能启用双重缓冲的方法?

There is no built-in support for double buffering in the Compact Framework. Compact Framework中没有对双缓冲的内置支持。 You can add it yourself, PictureBox already supports the Image property. 您可以自己添加它,PictureBox已经支持Image属性。 Create a Bitmap in the constructor and assign it to Image. 在构造函数中创建一个位图,并将其分配给Image。 You don't need the Paint event anymore, the one provided by PictureBox already draws it to the screen. 您不再需要Paint事件,PictureBox提供的事件已将其绘制到屏幕上。

Whenever the image needs to change, create a Graphics object with Graphics.FromImage(), passing the PB's Image and draw your stuff. 每当需要更改图像时,请使用Graphics.FromImage()创建一个Graphics对象,传递PB的Image并绘制您的东西。 Call the PB's Invalidate() method to tell it that it needs to redraw the image. 调用PB的Invalidate()方法以告知它需要重绘图像。 If you still see flicker, override the PB's OnPaintBackground() method and do nothing. 如果仍然看到闪烁,请重写PB的OnPaintBackground()方法,而不执行任何操作。

The only other consideration is handling resizing, you'd need a larger or smaller Bitmap. 唯一的其他考虑因素是处理调整大小,您需要更大或更小的位图。 Not so sure that would be necessary for a game. 不确定是否需要游戏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM