TabPage更改时保持图形不变 [英] Keeping graphics unaltered when TabPage changes
问题描述
我有一个窗体,该窗体使用停靠在TabControl
的特定TabPage
内的Panel
上的Paint
事件显示一组图形.
I have a form that displays a set of graphics using a Paint
event on a Panel
that is docked inside a particular TabPage
of a TabControl
.
问题如下:
当用户切换到其他TabPage
然后决定返回到最初显示图形的TabPage
时,默认情况下这些图形无效,因此Panel
显得空白.
When the user switches to a different TabPage
and then decides to go back to the TabPage
where the graphics were originally displayed, those graphics are invalidated by default so the Panel
appears blank.
我希望这些图形在不同的TabPage之间切换时保持不变,并完全独立于用户的操作.
I would like those graphics to stay unaltered and totally independent from the user's action when switching between different TabPages.
一个要求:
由于图形复杂,计算机需要花费一些时间来绘制图形,因此我不想每次通过重复调用Paint
事件来重新绘制图形.相反,我只需要避免图形的默认失效即可.
Since the graphics are complex and take some time to be drawn by the computer, I don't want to repaint the graphics each time by calling the Paint
event repeatedly. Instead, I only need to avoid the default invalidation of the graphics.
我已经阅读了其他问题,这可能有助于解决我的问题,但超出了我的理解范围.
I have read this other question which may be helpful to solve my problem but it goes beyond my knowledge.
推荐答案
如果要缓存图形,可以将所有内容绘制到位图中并将其设置为面板的背景图像.
If you want to cache your graphics you can draw everything into a bitmap and set it to the panel's background image.
这是一些使用Control
的示例代码.只需传递您的Panel
:
Here is some example code, using a Control
. Simply pass in your Panel
:
void drawInto(Control ctl)
{
Bitmap bmp = new Bitmap(ctl.ClientSize.Width, ctl.ClientSize.Height);
using ( Graphics G = Graphics.FromImage(bmp))
{
// all your drawing code goes here..!
G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
G.DrawEllipse(Pens.DimGray, ctl.ClientRectangle);
// ..
// ..
}
ctl.BackgroundImage = bmp;
}
只需确保在必要时调用绘图功能 ,因为这现在是您的职责. Panel
的Resize
事件是一个很好的示例您需要调用它的地方!
Just make sure to call the drawing function whenever necessary, as this is now your responsibility. The Resize
event of the Panel
is a good example of where you need to call it!
来自用户的数据更改是明显的其他原因.
And changes in the data coming from the user are the obvious other reason to call it..
这篇关于TabPage更改时保持图形不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!