面板处于活动状态时绘制边框 [英] Draw Border of Panel When it is being Active Scroll

查看:90
本文介绍了面板处于活动状态时绘制边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试制作面板边框时遇到问题,首先我将属性面板设置为: AutoScroll = true;
然后我放了边框面板事件中的绘制代码:

I just got problem when i'm trying to make a panel border, firstly i have set my properties panel to be: "AutoScroll = true;" then i put the border drawing codes in Panel event:

    ControlPaint.DrawBorder(e.Graphics, 
        ClientRectangle,  
        Color.Black, 5, 
        ButtonBorderStyle.Solid,
        Color.Black, 5, ButtonBorderStyle.Solid, 
        Color.Black, 5, ButtonBorderStyle.Solid,
        Color.Black, 5, ButtonBorderStyle.Solid);

实际上我仍然遇到第二个问题,在这里我将解释所有问题。.希望您不要介意。
好​​,当面板滚动处于活动状态时,面板边框会崩溃。看一下图片:

actually i still got a second problem and i will explain it all here.. I hope you don't mind. well, the panel border will get some crash when the panel scroll is being active. take a look at the picture:

甚至我放

`e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);`

它看起来并不像边框,因为它只是在面板内部绘制了一个矩形当滚动处于活动状态时。那不是我期望的,但是我需要一个 Border

it doesn't looks like a border panel because it just draw a rectangle inside panel when the scroll is being active. that's not what i expected, but i need a Border

我敢打赌,问题是源代码,不是因为它不是可能,看看 System.Windows.Forms.Panel ,尽管它很简单,但我认为它是完美的面板。
,请帮助我解决此问题。这使我感到困惑

i bet, the problem is the source code, that is not because it is not possible, take a look at System.Windows.Forms.Panel i think it is perfect panel although it simple. please help me to solve this problem. this has made me confused

推荐答案

这无法正常工作,您正在与名为显示窗口内容的Windows系统选项作斗争同时拖动。它在所有最新的Windows版本中都已打开,并且您不能合理地将其关闭。该选项的作用是在操作滚动条时以优化的方式滚动窗口内容。它会按滚动量复制窗口像素,并要求对滚动显示的窗口部分进行重新绘制。

This is not going to work well, you are fighting a Windows system option named "Show window contents while dragging". It is turned on in all recent Windows versions and you cannot reasonably turn it off. What the option does is scroll the window content in an optimized way when you operate the scrollbar. It copies the window pixels by the scroll amount and asks for a repaint for the part of the window that got revealed by the scroll.

麻烦的是,这也移动了绘制的图像边界。因此,您也会看到底部的黑线也向上移动。但是它不会被擦除,因为Windows仅要求对滚动显示的窗口部分进行重新绘制。所以它涂片。最上面的行消失了,滚动了下来。要解决此问题,您需要重新绘制 entire 窗口。通过为面板控件实现Scroll事件,很容易做到:

Trouble is, that also moved your painted border. So you'll see the black line in the bottom getting moved up as well. But it doesn't get erased because Windows asked only for a repaint of the part of the window that got revealed by the scroll. So it "smears". The top line just disappears, getting scroll off. To get this fixed, you need to repaint the entire window. Easy to do by implementing the Scroll event for the panel control:

    private void panel1_Scroll(object sender, ScrollEventArgs e) {
        panel1.Invalidate();
    }

这可以解决此问题,但您可能仍会在速度较慢的计算机上看到伪像。该黑线仍在向上移动,以被Paint事件处理程序快速再次涂上油漆。问题是快速,如果不是很快,那么您仍然会看到行的移动。该工件很有意思,很有趣,您会看到生产线在进行pogo,上下跳跃。人眼对这样的运动非常敏感,能够很好地检测出大草原草中的狮子是一种进化优势。

That fixes the problem, but you may still notice an artifact on slower machines. That black line is still getting moved up, to be quickly overpainted again by your Paint event handler. The "quickly" is the issue, if it is not that quickly then you'll still see that line move. The artifact is, erm, interesting, you'll see the line doing the pogo, jumping up and down. The human eye is very sensitive to motion like that, it was an evolutionary advantage to be able to be good at detecting the lion in the tall savanna grass.

滚动窗口中固定的对象不能很好地工作。您可以使用面板控件,并为WM_NCCALCSIZE实现消息处理程序,以使面板具有非工作区,但这非常痛苦。

Trying to keep objects stationary in scrolling window just doesn't work that well. You can monkey with the panel control and implement a message handler for WM_NCCALCSIZE to give the panel a non-client area but that's all rather painful.

简单的解决方案是让Form在面板周围绘制一个矩形:

The simple solution is to just have the Form draw a rectangle around the panel:

    protected override void OnPaint(PaintEventArgs e) {
        var rc = panel1.Bounds;
        rc.Inflate(1, 1);
        e.Graphics.DrawRectangle(Pens.Black, rc);
        base.OnPaint(e);
    }

或更简单的方法是设置面板的BorderStyle。

Or easier yet, set the panel's BorderStyle.

这篇关于面板处于活动状态时绘制边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆