Question no paint-event ?!

aw48

Active member
Joined
Feb 9, 2013
Messages
36
Location
Germany
Programming Experience
10+
hi,
using show/hide of frmBitmap to display a bitmap which is imported by form1 frmBitmap only displays an empty window and I found that frmBitmap's paint event is never thrown. That's why I'm using VisibleChanged event.

frmBitmap.Show(this) is called from form1's paint-event.

C#:
    public partial class frmBitmap : Form
    {
        public frmBitmap()
                {
            InitializeComponent();
            BitmapOrigin = new Point(0,0);
        }

        private void frmBitmap_Paint(object sender, PaintEventArgs e)
        {
            if (Bitmap == null) return;
            e.Graphics.DrawImage(Bitmap, BitmapOrigin);
        }

        private void frmBitmap_VisibleChanged(object sender, EventArgs e)
        {
            switch(this.Visible) {
            case true :
                ParentOrigin = new Point(Owner.Left + (Owner.Width >> 1), Owner.Top + (Owner.Height >> 1));

                Graphics = this.CreateGraphics();
                if (Bitmap.Width != this.Width || Bitmap.Height != this.Height) {
                    SetPositionAndSize();
                }
                Graphics.DrawImage(Bitmap, BitmapOrigin);
                break;

            case false :
                Graphics.Clear(this.BackColor);
                Graphics.Dispose();
                break;
            }
        }

        private void SetPositionAndSize()
        {
            this.Width = Bitmap.Width;
            this.Height = Bitmap.Height;
            this.Left = ParentOrigin.X - (this.Width >> 1);
            this.Top = ParentOrigin.Y - (this.Height >> 1);
        }

        internal Bitmap Bitmap { get; set; }
        internal Point ParentOrigin { get; set; }
        private    Graphics    Graphics;
        private    Point    BitmapOrigin;
    }

can anyone give some explanation on this problem or maybe a different approach?
thanks in advance
 
Last edited by a moderator:
There is a Paint event. The question is did you hook it up? Are you doing anything to force a re-paint?
C#:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SimpleCS
{
    class MyForm : Form
    {
        public MyForm()
        {
            this.Paint += MyForm_Paint;
            var timer = new Timer() { Interval = 1000 };
            timer.Tick += Timer_Tick;
            timer.Enabled = true;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }

        private void MyForm_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawString($"Painting... {DateTime.Now}", SystemFonts.DefaultFont, SystemBrushes.ControlText, 30, 30);
        }
    }

    class Program
    {
        static void Main()
        {
            Application.Run(new MyForm());
        }
    }
}

Produces this output...
579
 
thanks for your answer
originally I had invalidate() in the visiblechanged event's true branch but that didn't help
 
frmBitmap.Show(this) is called from form1's paint-event.
Why from the parent form's paint event? Paint events are supposed to be for painting, not for handling user interactions.

Anyway, this bits of the WM_PAINT documentation may prove insightful.
The system sends this message when there are no other messages in the application's message queue. DispatchMessage determines where to send the message; GetMessage determines which message to dispatch. GetMessage returns the WM_PAINT message when there are no other messages in the application's message queue, and DispatchMessage sends the message to the appropriate window procedure.

A window may receive internal paint messages as a result of calling RedrawWindow with the RDW_INTERNALPAINT flag set. In this case, the window may not have an update region. An application may call the GetUpdateRect function to determine whether the window has an update region. If GetUpdateRectreturns zero, the application need not call the BeginPaint and EndPaint functions.

:

The system sends an internal WM_PAINT message only once. After an internal WM_PAINT message is returned from GetMessage or PeekMessage or is sent to a window by UpdateWindow, the system does not post or send further WM_PAINT messages until the window is invalidated or until RedrawWindow is called again with the RDW_INTERNALPAINT flag set.
 
What do you mean by : imported by form1 frmBitmap ?

Did you tell it to fire if a repaint is needed ? The Paint event occurs when a control is redrawn. Where is the event handler for making the paint event fire ?

And as already pointed out, the paint events are for painting, and not for when to display a form or for using it for user actions. It would be a good idea to wire up your events so that they fire, but it would be a better idea if you would use the appropriate event handlers designed for user interactions and not those designed for painting a control. Can you also please remember to use code tags when posting code on the forums. Reading it as it is is rather heavy on the eyes.
 
before adding frmbitmap the bitmap was displayed in form1's paint event and in a first step I just replaced drawimage with frmbitmap.show(). if displaying the bitmap works like I want it to I will modify form1.
 
Back
Top Bottom