Dynamic click event on PictureBox

后端 未结 2 1999
说谎
说谎 2021-01-28 18:12

I am getting a list of pictures from a directory and storing the filenames in a List. I then loop through each of these and create a PictureBox<

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-28 18:57

    The sender parameter is indeed your PictureBox, downcast to object. Access it this way:

    var pictureBox = sender as PictureBox;
    

    Drawing a border around it could not be so easy as you will have to either override the OnPaint method of the PictureBox, either handle the Paint event.

    You can use this class to draw a black thin border around your image.

    public class CustomBorderPictureBox : PictureBox
    {
        public bool BorderDrawn { get; private set; }
    
        public void ToggleBorder()
        {
            BorderDrawn = !BorderDrawn;
            Invalidate();
        }
    
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            if (BorderDrawn)
                using (var pen = new Pen(Color.Black))
                    pe.Graphics.DrawRectangle(pen, 0, 0, Width - 1, Height - 1);
        }
    }
    

提交回复
热议问题