以控件PictureBox为例
我知道调用Invalidate()后,就会产生一个Paint消息,Form就会重画但是在.net compact framework里面picturebox控件只有Invalidate,没有Paint。
有Invalidate,可以理解为Picturebox在调用invalidate会向系统产生重画消息,但这个重画却不是由Paint完成的(因为.net cf中没有Paint事件)??
我自己写了一个myPicturebox控件(从.net自己的picturebox继承过来的),并添加了Paint事件,但是我在写了myPicturebox1.Invalidate()语句后,系统根本没有调用我的Paint事件...那么如何把系统的Invalidate和我添加的Paint事件联系起来呢?

解决方案 »

  1.   


    [Docking(DockingBehavior.Ask), ComVisible(true), DefaultBindingProperty("Image"), Designer("System.Windows.Forms.Design.PictureBoxDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), DefaultProperty("Image"), SRDescription("DescriptionPictureBox"), ClassInterface(ClassInterfaceType.AutoDispatch)]
    public class PictureBox : Control, ISupportInitialize
    {
        // Fields
        private BorderStyle borderStyle;
        private int contentLength;
        private AsyncOperation currentAsyncLoadOperation;
        private bool currentlyAnimating;
        private Image defaultErrorImage;
        [ThreadStatic]
        private static Image defaultErrorImageForThread;
        private static readonly object defaultErrorImageKey;
        private Image defaultInitialImage;
        [ThreadStatic]
        private static Image defaultInitialImageForThread;
        private static readonly object defaultInitialImageKey;
        private Image errorImage;
        private static readonly object EVENT_SIZEMODECHANGED;
        private bool handleValid;
        private Image image;
        private ImageInstallationType imageInstallationType;
        private string imageLocation;
        private Image initialImage;
        private object internalSyncObject;
        private SendOrPostCallback loadCompletedDelegate;
        private static readonly object loadCompletedKey;
        private static readonly object loadProgressChangedKey;
        private SendOrPostCallback loadProgressDelegate;
        private BitVector32 pictureBoxState;
        private const int PICTUREBOXSTATE_asyncOperationInProgress = 1;
        private const int PICTUREBOXSTATE_cancellationPending = 2;
        private const int PICTUREBOXSTATE_inInitialization = 0x40;
        private const int PICTUREBOXSTATE_needToLoadImageLocation = 0x20;
        private const int PICTUREBOXSTATE_useDefaultErrorImage = 8;
        private const int PICTUREBOXSTATE_useDefaultInitialImage = 4;
        private const int PICTUREBOXSTATE_waitOnLoad = 0x10;
        private const int readBlockSize = 0x1000;
        private byte[] readBuffer;
        private Size savedSize;
        private PictureBoxSizeMode sizeMode;
        private MemoryStream tempDownloadStream;
        private int totalBytesRead;    // Events
        [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
        public event EventHandler CausesValidationChanged;
        [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
        public event EventHandler Enter;
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public event EventHandler FontChanged;
        [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
        public event EventHandler ForeColorChanged;
        [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
        public event EventHandler ImeModeChanged;
        [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
        public event KeyEventHandler KeyDown;
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public event KeyPressEventHandler KeyPress;
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public event KeyEventHandler KeyUp;
        [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
        public event EventHandler Leave;
        [SRCategory("CatAsynchronous"), SRDescription("PictureBoxLoadCompletedDescr")]
        public event AsyncCompletedEventHandler LoadCompleted;
        [SRCategory("CatAsynchronous"), SRDescription("PictureBoxLoadProgressChangedDescr")]
        public event ProgressChangedEventHandler LoadProgressChanged;
        [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
        public event EventHandler RightToLeftChanged;
        [SRDescription("PictureBoxOnSizeModeChangedDescr"), SRCategory("CatPropertyChanged")]
        public event EventHandler SizeModeChanged;
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public event EventHandler TabIndexChanged;
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public event EventHandler TabStopChanged;
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public event EventHandler TextChanged;    // Methods
        static PictureBox();
        public PictureBox();
        private void AdjustSize();
        private void Animate();
        private void Animate(bool animate);
        private void BeginGetResponseDelegate(object arg);
        private Uri CalculateUri(string path);
        [SRDescription("PictureBoxCancelAsyncDescr"), SRCategory("CatAsynchronous")]
        public void CancelAsync();
        protected override void Dispose(bool disposing);
        internal override Size GetPreferredSizeCore(Size proposedSize);
        private void GetResponseCallback(IAsyncResult result);
        private Rectangle ImageRectangleFromSizeMode(PictureBoxSizeMode mode);
        private void InstallNewImage(Image value, ImageInstallationType installationType);
        [SRCategory("CatAsynchronous"), SRDescription("PictureBoxLoad0Descr")]
        public void Load();
        [SRCategory("CatAsynchronous"), SRDescription("PictureBoxLoad1Descr")]
        public void Load(string url);
        [SRDescription("PictureBoxLoadAsync0Descr"), SRCategory("CatAsynchronous")]
        public void LoadAsync();
        [SRDescription("PictureBoxLoadAsync1Descr"), SRCategory("CatAsynchronous")]
        public void LoadAsync(string url);
        private void LoadCompletedDelegate(object arg);
        private void LoadProgressDelegate(object arg);
        protected override void OnEnabledChanged(EventArgs e);
        private void OnFrameChanged(object o, EventArgs e);
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        protected override void OnHandleCreated(EventArgs e);
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        protected override void OnHandleDestroyed(EventArgs e);
        protected virtual void OnLoadCompleted(AsyncCompletedEventArgs e);
        protected virtual void OnLoadProgressChanged(ProgressChangedEventArgs e);
        protected override void OnPaint(PaintEventArgs pe);
      

  2.   

    protected override void OnPaint(PaintEventArgs pe)
    {
        if (this.pictureBoxState[0x20])
        {
            try
            {
                if (this.WaitOnLoad)
                {
                    this.Load();
                }
                else
                {
                    this.LoadAsync();
                }
            }
            catch (Exception exception)
            {
                if (ClientUtils.IsCriticalException(exception))
                {
                    throw;
                }
                this.image = this.ErrorImage;
            }
        }
        if (this.image != null)
        {
            this.Animate();
            ImageAnimator.UpdateFrames();
            Rectangle rect = (this.imageInstallationType == ImageInstallationType.ErrorOrInitial) ? this.ImageRectangleFromSizeMode(PictureBoxSizeMode.CenterImage) : this.ImageRectangle;
            pe.Graphics.DrawImage(this.image, rect);
        }
        base.OnPaint(pe);
    }