.NET UserControl:Size 属性在 Resize 事件上提供了错误的值
请原谅代码转储,这些是 UserControl 中的函数,
private void PNGQuantPreviewControl_Resize(object sender, EventArgs e)
{
createOffScreenBm();
draw();
}
private void createOffScreenBm()
{
offScreenBm = new Bitmap(this.Size.Width, this.Size.Height);
offScreenGfx = Graphics.FromImage(offScreenBm);
}
private void draw()
{
// draw background
offScreenGfx.FillRectangle(transTexture, 0, 0, offScreenBm.Width, offScreenBm.Height);
// draw image preview
offScreenGfx.DrawImage(pngQuantPreview, getTopLeftPosition());
// apply to picture box
this.CreateGraphics().DrawImage(offScreenBm, 0, 0);
}
因此,当控件更改大小时,它会重新创建屏幕外位图以反映新的大小并重新绘制图像。
但是,如果我快速调整控件的大小,位图不会填充它,右侧和/或底部会留下间隙。
我对 C# 相当陌生,所以可能明显我做错了,或者我在错误的时间读取了大小值。 有任何想法吗?
Excuse the code dump, these are functions within a UserControl
private void PNGQuantPreviewControl_Resize(object sender, EventArgs e)
{
createOffScreenBm();
draw();
}
private void createOffScreenBm()
{
offScreenBm = new Bitmap(this.Size.Width, this.Size.Height);
offScreenGfx = Graphics.FromImage(offScreenBm);
}
private void draw()
{
// draw background
offScreenGfx.FillRectangle(transTexture, 0, 0, offScreenBm.Width, offScreenBm.Height);
// draw image preview
offScreenGfx.DrawImage(pngQuantPreview, getTopLeftPosition());
// apply to picture box
this.CreateGraphics().DrawImage(offScreenBm, 0, 0);
}
So, when the control changes size, it recreates the offscreen bitmap to reflect the new size and redraws the image.
However, if I quickly resize the control the bitmap doesn't fill it, there's a gap left at the right and/or bottom.
I'm fairly new to C#, so there's probably something obvious I'm doing wrong, or I'm reading the size values at the wrong time. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您需要覆盖 OnPaint 方法,或者订阅 Paint 事件并在那里绘制所有内容。
其次,您不需要为双缓冲创建离屏位图,因为.net 中已经存在用于此类目的的类缓冲图形。
第三,最好创建 UserControl 后代并启用内部 .net 双缓冲,如下所示:
使用这种方法,您将获得双缓冲,您所需要做的就是在 OnPaint 方法中绘制图形。 您可以阅读有关此控件样式的更多信息在 MSDN 中。
First of all you need to overwrite OnPaint method, or subscribe to Paint event and draw everything there.
Second you do not need to create offscreen bitmap for double buffering, because in .net already exist class for such purposes BufferedGraphics.
And third, it is much better to create UserControl descedant and enable internal .net double buffering, something like this:
Using this approach you will get double-buffering, and all you need is to draw your graphics in OnPaint method. You can read more about this control styles in Msdn.
您是否考虑过重写 OnPaint 方法并将代码放入该方法中? 这将导致每当需要重新绘制控件时都会执行您的绘图代码,无论原因如何。
调整大小事件不一定要等到您完成调整父容器的大小。 当引发调整大小事件时,它需要等到代码退出才能捕获新的调整大小事件,因此当窗口/控件快速调整大小时,它无法很好地跟上,您得到的是最后一次它能够捕获事件,不一定是控件的最终状态......如果这有意义的话。
Have you considered overriding the OnPaint method and placing the code within that method? This would result in your drawing code being executed any time the control needs to be redrawn, regardless of the reason.
A resize event does not necessarily wait until you are finished resizing the parent container. When the resize event is raised it needs to wait until the code exits before it can capture a new resize event so when the window/control is resized quickly, it can't keep up all that well and what you get is the last time it was able to capture the event, not necessarily the final state of the control ... if that makes any sense.
您的控件上是否有诸如分离器之类的东西,或者声明了 MinSize 或 MaxSize ?
Do you have anything like a splitter on your control, or a MinSize or MaxSize declared?