Windows Develop Bookmark and Share   
 index > Windows Forms General > Calling BeginPaint in WM_PAINT handler affects local variables! (.NET 2.0, C#)
 

Calling BeginPaint in WM_PAINT handler affects local variables! (.NET 2.0, C#)

It' s very, very strange! Try this code (paste it into an empty form):

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        internal const int WM_PAINT = 0xF;
        [StructLayout(LayoutKind.Sequential)]
        internal struct RECT
        {
            internal int Left;
            internal int Top;
            internal int Right;
            internal int Bottom;
        }
        [StructLayout(LayoutKind.Sequential)]
        internal struct PAINTSTRUCT
        {
            internal int hdc;
            internal int fErase;
            internal RECT rcPaint;
            internal int fRestore;
            internal int fIncUpdate;
            internal byte rgbReserved;
        }

        [DllImport("user32.dll")]
        internal static extern int BeginPaint(int hwnd, ref PAINTSTRUCT lpPaint);
        [DllImport("user32.dll")]
        internal static extern int EndPaint(int hwnd, ref PAINTSTRUCT lpPaint);

        public Form1()
        {
            InitializeComponent();
        }
        protected override void WndProc(ref Message m)
        {
            int hWnd = this.Handle.ToInt32();
            if (m.Msg == WM_PAINT)
            {
                Console.WriteLine("WM_PAINT");
                int dummy = 0;
                Console.WriteLine("dummy = {0}", dummy);
                PAINTSTRUCT ps = new PAINTSTRUCT();
                BeginPaint(hWnd, ref ps);
                Console.WriteLine("dummy = {0}", dummy);
                // we can draw smth. here in real application
                EndPaint(hWnd, ref ps);
            }
            else
            {
                base.WndProc(ref m);
            }
        }
    }
}
I get the following output:

 WM_PAINT
 dummy = 0
 dummy = 9618796

How a ____ could 'dummy' variable change its value?! why is it equal to 9618796? what does this strange number mean?
Now if you comment BeginPaint and EndPaint calls, and add the "base.WndProc(ref m);" call to the first IF branch - you'll get the expected output:

 WM_PAINT
 dummy = 0
 dummy = 0

Any ideas?!
Comanche  Tuesday, October 06, 2009 11:06 AM
Problem solved: I had wrong structure definition. It should look like this: [code] [StructLayout(LayoutKind.Sequential)] internal struct PAINTSTRUCT { internal int hdc; internal bool fErase; internal RECT rcPaint; internal bool fRestore; internal bool fIncUpdate; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] internal byte[] rgbReserved; } [/code]
  • Marked As Answer byComanche Tuesday, October 06, 2009 11:29 AM
  •  
Comanche  Tuesday, October 06, 2009 11:29 AM
Problem solved: I had wrong structure definition. It should look like this: [code] [StructLayout(LayoutKind.Sequential)] internal struct PAINTSTRUCT { internal int hdc; internal bool fErase; internal RECT rcPaint; internal bool fRestore; internal bool fIncUpdate; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] internal byte[] rgbReserved; } [/code]
  • Marked As Answer byComanche Tuesday, October 06, 2009 11:29 AM
  •  
Comanche  Tuesday, October 06, 2009 11:29 AM

You can use google to search for other answers

Custom Search

More Threads

• maskedTextBox problem
• SplitContainer, visible handle for resizing
• Hot Key function for whole program
• Office XP PIA with word
• showing blank instead of value in datagrid
• Check if form location is changing
• retain blue background on listview selected item when focus leaves listview
• Getting Units of Measure in use
• UserControl with multiple listboxes
• Right justifying when printing a Grid