.NET UserControl:Size 属性在 Resize 事件上提供了错误的值

发布于 2024-07-25 04:36:56 字数 809 浏览 6 评论 0原文

请原谅代码转储,这些是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎歌 2024-08-01 04:36:56

首先,您需要覆盖 OnPaint 方法,或者订阅 Paint 事件并在那里绘制所有内容。

其次,您不需要为双缓冲创建离屏位图,因为.net 中已经存在用于此类目的的类缓冲图形

第三,最好创建 UserControl 后代并启用内部 .net 双缓冲,如下所示:

public UserControl2
{
    SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
}

使用这种方法,您将获得双缓冲,您所需要做的就是在 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:

public UserControl2
{
    SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
}

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.

忆沫 2024-08-01 04:36:56

您是否考虑过重写 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.

冰雪梦之恋 2024-08-01 04:36:56

您的控件上是否有诸如分离器之类的东西,或者声明了 MinSize 或 MaxSize ?

Do you have anything like a splitter on your control, or a MinSize or MaxSize declared?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文