可滚动的图片框面板,边框没有重新绘制滚动

问题描述 投票:1回答:1

我正在构建一个带有可滚动缩略图的图像查看器。我有一个面板,面板上的autoscroll设置为true。我用图片框加载面板,一个文件夹中的每个图像;这些是缩略图,一旦面板中有多个图片框,就可以在面板中滚动。

我可以单击一个或多个图片框(缩略图)并在每个单击的图片框周围放置边框。我起初使用BorderStyle = BorderStyle.Fixed3D,但是那个细边框是不合适的。所以,现在我通过在图片框上绘制一个矩形来设置图片框上的边框:

    private void SetBorder(PictureBox pb)
    {
        var color = ColorTranslator.FromHtml("#ff9900");
        var rc = pb.ClientRectangle;
        rc.Inflate(-1, -1);
        ControlPaint.DrawBorder(pb.CreateGraphics(), rc, color, 3, ButtonBorderStyle.Solid, color, 3, ButtonBorderStyle.Solid, color, 3, ButtonBorderStyle.Solid, color, 3, ButtonBorderStyle.Solid);
    }

这看起来比跛脚的Fixed3D bordertyle好多了,但是当我使用滚动条的轨道部分(滚动条外面的区域,向左或向右)滚动图像时我遇到了问题。单击并拖动滚动条本身,或使用箭头工作正常(我从面板滚动事件重新绘制,请参阅下面的进一步说明)。但是,当单击轨道时,当图片框从滚动视图中滚动回来时,边框不会重新绘制。例如,我单击几个缩略图并设置边框:

enter image description here

边框看起来很好,但如果我向右滚动(单击轨道),然后再向左移动,覆盖的内容不会重新绘制。例如:

Borders not repainted

正如我上面提到的,当单击滚动条箭头或拖动滚动条时,我正在重新绘制面板滚动事件中的边框:

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

但是,在单击轨道时,似乎不会引发Panel的Scroll事件。

我忘了提到,SetBorders(复数)是另一种方法(我在问题中没有包含这个方法),它通过面板中的图片框循环,对于每个应该重新绘制的方法,它调用SetBorder(上面包含的方法)并通过给出的图片框...

刚发现使用鼠标滚轮滚动时也存在问题。

有任何想法吗?

c# winforms scroll picturebox
1个回答
1
投票

将所有picboxes paint event设置为相同的子:

pictureBox1.Paint += pictureBox_Paint;
pictureBox2.Paint += pictureBox_Paint;
....
....

或者将picboxes添加到panel之后:

var children = panel1.Controls.OfType<Control>();

foreach( Control child in children ) {
    ( (PictureBox)child ).Paint += pictureBox_Paint;
}

并在事件中:

private void pictureBox_Paint( object sender, PaintEventArgs e ) {
    PictureBox picbox = (PictureBox)sender;
    var color = ColorTranslator.FromHtml( "#ff9900" );
    var rc = picbox.ClientRectangle;
    rc.Inflate( -1, -1 );

    ControlPaint.DrawBorder( e.Graphics, rc, color, 3, ButtonBorderStyle.Solid, color, 3, 
                             ButtonBorderStyle.Solid, color, 3, ButtonBorderStyle.Solid, 
                             color, 3, ButtonBorderStyle.Solid );

}

无需再使用panel1_Scroll事件!

© www.soinside.com 2019 - 2024. All rights reserved.