How do I catch all WM_PAINT message from all control

Posted on 2007-11-30
Last Modified: 2008-02-01
Hi Experts,

I got stuck when trying implementing a method to catch WM_PAINT message from all control. When I run my apps, the UI seems freeze.
Does anything wrong with my code ?
public partial class frmMain : Form


        CustomFilter c = new CustomFilter();

        #region Form Method

        public frmMain()




        private void frmMain_Load(object sender, EventArgs e)



            c.Redraw += new AllControlRedraw(RedrawLine);


        void RedrawLine()


            IntPtr handle = Win32Helper.GetDesktopWindow();

            IntPtr hDc = Win32Helper.GetWindowDC(handle);

            Graphics g = Graphics.FromHdc(hDc);

            Pen p = new Pen(Color.Red, 2);

            Point[] StartLine = new Point[] { new Point(40, 10) };

            Point[] EndLine = new Point[] { new Point(40, 200) };

            for (int i = 0; i < StartLine.Length; i++)

                g.DrawLine(p, this.PointToScreen(StartLine[i]), this.PointToScreen(EndLine[i]));


            Win32Helper.ReleaseDC(handle, hDc);



    public delegate void AllControlRedraw(object sender, System.EventArgs e);

    class CustomFilter : System.Windows.Forms.IMessageFilter


        private const int WM_PAINT = 0x000F;

        public event AllControlRedraw Redraw;

        #region IMessageFilter Members

        public bool PreFilterMessage(ref System.Windows.Forms.Message m)


            AllControlRedraw t = Redraw;

            if (t != null)


                switch (m.Msg)


                    case WM_PAINT:

                        t(this, new EventArgs());




            return true;





Open in new window

Question by:Aryo1
1 Comment

Accepted Solution

wizrr earned 500 total points
ID: 20386150

Retuen value:
true to filter the message and stop it from being dispatched; false to allow the message to continue to the next filter or control.

You should return false.

Anyway your drawing code will be replaced with control's drawing code. Maybe you should subclass all windows in your app? (i think this is crazy idea)

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Not showing page correctly 3 29
Host pages 5 25
Error on link 14 36
Visual Studio TFS - how do I check in my code? 2 28
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now