Link to home
Start Free TrialLog in
Avatar of Aryo1
Aryo1Flag for Indonesia

asked on

How do I catch all WM_PAINT message from all control

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()
        {
            InitializeComponent();
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            Application.AddMessageFilter(c);          
 
            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]));
 
            g.Dispose();
            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());
                        break;
                }
            }
 
            return true;
        }
 
        #endregion
 
        
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of wizrr
wizrr
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial