我什么时候应该重绘我的自定义控件 [英] when should i redraw my custom control

查看:65
本文介绍了我什么时候应该重绘我的自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有一个自己制作的用户控件,该控件是图标容器
我用它在其上绘制图标

然后在控件的绘制事件上,我在控件上绘制一个图标

当我将控件放在窗体上并将autoscroll属性设置为true时,我滚动窗体时控件会重绘自身,但当我更改文件夹图标时,它不会像Windows窗口那样快速绘制

当我滚动表单时,我在表单上所有控件的所有控件都会重新绘制

在这里,我希望我的控件在可见且不在矩形矩形外部时绘制它的图标

如果有人知道如何检测我的控件是否不在其父级可见区域之外,请告诉我

Hi, everybody

I have a user control that i made myself , this control is icon container
I use it to draw icon on it

And on paint event of the control i draw an icon on the control

And when i put my control on a form and set the autoscroll property to true then i scroll the form the control redraw itself but not fast like the window of windows when i change folder icon

And this becaouse of all the controls of my own control on the form redraw themselves when i scroll the form

And here i want my control to draw it''s icon when it is visible and not outside the form rectangle

if anybody know how can i detect if my control is not outside it''s parent visible region please tell me

推荐答案

yasser-YSSP写道:
yasser-YSSP wrote:

在这里,我希望我的控件在可见且不在矩形矩形外部时绘制它的图标

如果有人知道如何检测我的控件是否不在其父级可见区域之外,请告诉我

And here i want my control to draw it''s icon when it is visible and not outside the form rectangle

if anybody know how can i detect if my control is not outside it''s parent visible region please tell me



这不是您的问题,因为这是winforms正常运行的方式.我认为这是:



This is not your problem, as this is how winforms normally operate. I think this is:

yasser-YSSP写道:
yasser-YSSP wrote:

在控件的绘制事件中,我在控件上绘制了一个图标

And on paint event of the control i draw an icon on the control



如果创建图形是繁重的工作,而您是在PaintEvent内部进行的,那是错误的.您需要做的是将创建图形的代码从paint事件中移出(例如)到OnLoadEvent中,因此仅被调用一次.然后,PaintEvent应该只绘制您预先准备的图形.



If creating the graphic is heavy work, and you are doing that inside the PaintEvent then that is what is wrong. What you need to do is move the code creating the graphic out of the paint event into (for example) the OnLoadEvent, so it only gets called once. Then the PaintEvent should just paint the graphic you have pre-prepared.


我刚刚尝试过此操作,但不确定您的问题是什么.

我创建了一个只有一个PictureBox的UserControl.它具有一个属性,您可以在其中设置要显示的图片并绘制该图片.

这是我使用的代码:
I just tried this and I''m not sure what your problem is.

I created a UserControl that just has a single PictureBox on it. It has a property where you can set which picture to show and it draws that picture.

Here''s the code that I used:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace IconContainer
{
    public partial class IconContainer : UserControl
    {
        public IconContainer()
        {
            InitializeComponent();
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {
            ChangeImage();
        }

        public enum ImageIndex { Add, Anchor, Application, Application_add, Application_cascade, Application_delete } ;

        private ImageIndex _imageIndex;
        [DefaultValue(ImageIndex.Add)]
        public ImageIndex Image
        {
            get { return _imageIndex; }
            set
            {
                _imageIndex = value;
                ChangeImage();
            }
        }

        private Image _image;
        public void ChangeImage()
        {
            Assembly _assembly;
            Stream _imageStream = null;
            _assembly = Assembly.GetExecutingAssembly();
            
            switch (_imageIndex)
            {
                case ImageIndex.Add:
                    _imageStream = _assembly.GetManifestResourceStream("IconContainer.Resources.add.png");
                    break;
                case ImageIndex.Anchor:
                    _imageStream = _assembly.GetManifestResourceStream("IconContainer.Resources.anchor.png");
                    break;
                case ImageIndex.Application:
                    _imageStream = _assembly.GetManifestResourceStream("IconContainer.Resources.application.png");
                    break;
                case ImageIndex.Application_add:
                    _imageStream = _assembly.GetManifestResourceStream("IconContainer.Resources.application_add.png");
                    break;
                case ImageIndex.Application_cascade:
                    _imageStream = _assembly.GetManifestResourceStream("IconContainer.Resources.application_cascade.png");
                    break;
                case ImageIndex.Application_delete:
                    _imageStream = _assembly.GetManifestResourceStream("IconContainer.Resources.application_delete.png");
                    break;
            }
            if (_imageStream != null )
            {
                _image = new Bitmap(_imageStream);
                this.pictureBox1.Image = _image;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Debug.Print(_imageIndex.ToString());
        }
    }
}



然后,在表单的load事件中,我跑了



Then, in the form''s load event, I ran

int y = 250;
int i;
for (i = 0; (i <= 5); i++)
{
    IconContainer.IconContainer  tempContainer = new IconContainer.IconContainer();
    tempContainer.Image = (IconContainer.IconContainer.ImageIndex)i;
    tempContainer.Location = new Point(25, y);
    this.Controls.Add(tempContainer);
    y += (tempContainer.Size.Height + 15);
}



您会注意到,我使用了Debug.Print调用来查看所绘制的内容.绘制的唯一内容是表单上可见的内容.当我滚动时,它将绘制新项目,但仅绘制可见的项目.

我认为您没有遇到的问题.听起来您正在执行类似于Windows资源管理器窗口的操作,在左窗格中为TreeView,在右侧为所选文件夹的内容.

我可以想象问题是当您单击新文件夹时,您正在加载所有项目的缩略图.这将需要一段时间.相反,您可以只分配一个默认图像,然后使用BackgroundWorker将图标加载到单独的线程上.

但是,您实际上并没有给我们太多帮助,所以我只是在猜测.但是,我很确定您认为是问题所在,实际上并不是问题所在.



You''ll notice that I used a Debug.Print call to see what I was drawing. The only thing that was drawn was what was visible on the form. When I scrolled, it would draw the new items, but only the items that were visible.

I don''t think you''re having the problem that you''re having. It sounds like you''re doing something similar to the Windows Explorer window with a TreeView on the left pane and the contents of the selected folder on the right.

I would imagine that the problem is that you are loading thumbnails for all of the items when you click on a new folder. That will take a while. You could, instead, just assign a default image and then use a BackgroundWorker to load the icons on a separate thread.

But, you haven''t really given us a lot to go on, so I''m just guessing. But, I''m pretty sure that what you think is the problem is not actually the problem.


这篇关于我什么时候应该重绘我的自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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