Solved

How do I catch all WM_PAINT message from all control

Posted on 2007-11-30
1
4,268 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 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