Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [C#] Metodo OnPaint non viene eseguito

    Ciao a tutti, ho questa classe:

    public class TransparentPictureBox : Control
    {

    private Timer refresher;
    private Image _image = null;

    public TransparentPictureBox()
    {
    this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    this.SetStyle(ControlStyles.UserPaint, true);

    refresher = new Timer();
    refresher.Tick += new EventHandler(this.TimerOnTick);
    refresher.Interval = 50;
    refresher.Start();
    }

    protected override CreateParams CreateParams
    {
    get
    {
    CreateParams cp = base.CreateParams;
    cp.ExStyle |= 0x20;
    return cp;
    }
    }

    protected override void OnMove(EventArgs e)
    {
    base.RecreateHandle();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
    if (_image != null)
    e.Graphics.DrawImage(_image, (Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2));
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
    }

    private void TimerOnTick(object source, EventArgs e)
    {
    base.RecreateHandle();
    refresher.Stop();
    }

    public Image Image
    {
    get
    {
    return _image;
    }
    set
    {
    _image = value;
    base.RecreateHandle();
    }
    }

    private void InitializeComponent()
    {
    this.SuspendLayout();
    //
    // TransparentPictureBox
    //

    this.ResumeLayout(false);

    }


    }

    nella mia Form faccio:

    private void Form1_Load(object sender, EventArgs e)
    {
    TransparentPictureBox a = new TransparentPictureBox();
    a.Image = Image.FromFile("C:\\test.bmp");
    this.Controls.Add(a);
    a.Parent = this;
    }

    qualcuno saprebbe spiegarmi perchè il metodo OnPaint della mia classe non viene mai eseguito?

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,481
    L' OnPaint avviene in seguito ad un Invalidate ...

  3. #3
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,480
    Se n'era parlato già qui, peraltro.

    Il problema è sempre lo stesso.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  4. #4
    Hai ragione, il problema è che anche usando l'Invalidate, l'OnPaint continua a non essere eseguito... Ho notato però che se invece di ereditare da Control, eredito da Panel, l'OnPaint viene eseguito.... come mai....? Non capisco....

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.