views:

42

answers:

1
+1  Q: 

C# - Lock drawing

Basically I've set up a class to handle sending WM_SETREDRAW messages like so:

public static class DrawingLocker
{
    [DllImport("user32", CharSet = CharSet.Auto)]
    private extern static IntPtr SendMessage(IntPtr hWnd, 
            int msg, int wParam, IntPtr lParam);

    private const int WM_SETREDRAW = 11; //0xB

    public static void LockDrawing(IntPtr Handle)
    {
        SendMessage(Handle, WM_SETREDRAW, 0, IntPtr.Zero);
    }

    public static void UnlockDrawing(IntPtr Handle)
    {
        SendMessage(Handle, WM_SETREDRAW, 1, IntPtr.Zero);
    }
}

I then have a Redraw method in my custom user control:

public void Redraw()
{
    try
    {
        DrawingLocker.LockDrawing(Handle);
        using (Graphics graphics = Graphics.FromHwnd(Handle))
        {
            //Draw Stuff
        }
    }
    finally { DrawingLocker.UnlockDrawing(Handle); }
}

My problem is that nothing I draw where the "Draw Stuff" comment is gets drawn. What am I doing wrong? (Redraw gets called when values that effect drawing change, including resize)

+1  A: 

I'm not really in Windows and stuff, but judging by what MSDN says about that flag, it doesn't do what you think it does. It's used to disable redrawing controls (think a list view) while you change their contents. Disabling it inside the redraw function is probably not going to do anything.

See if you can find something related to "double buffering", because that's one technique used to avoid flicker.

Matti Virkkunen
Oh ok, well i'll just use double buffering which I assume is drawing to say an image, then drawing the image on the control, but i'll google it this time to make sure I don't make another silly mistake.I'll set to accepted once it lets me, and remember to always google :D
Blam