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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Pass through dll 2 38
ConsoleSql 1 22
Hide Tab Page 3 20
As soon as i click a button on the form,it mnimises. 11 20
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

10 Experts available now in Live!

Get 1:1 Help Now