Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How do I catch all WM_PAINT message from all control

Posted on 2007-11-30
1
4,262 Views
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()
        {
            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

0
Comment
Question by:Aryo1
1 Comment
 
LVL 3

Accepted Solution

by:
wizrr earned 500 total points
ID: 20386150
Ofcourse

http://msdn2.microsoft.com/en-us/library/system.windows.forms.imessagefilter.prefiltermessage.aspx

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)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

840 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