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?!