Skip navigation

You have to override OnPaint.
I tried doing this:

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace testnamespace
{

public class MainWindow : Form
{
  public static int a = 5 ;

  public MainWindow()
  {
    
    this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.Opaque, true ) ;

  }

  public void Render()
  {

    // NOT DOUBLE BUFFERED!  because
    // its not in OnPaint.
    Graphics g = Graphics.FromHwnd( this.Handle ) ;
    g.Clear( Color.White ) ;
    a++;

    g.DrawLine( new Pen( Color.Blue, 5 ), new Point( 5, 5 ), new Point( a, 500 )  );
   
  }

  protected override void OnPaint( PaintEventArgs e )
  {
    
    // THIS IS double buffered
    Graphics g = e.Graphics;

    g.Clear( Color.White );
    a++;

    g.DrawLine( new Pen( Color.Blue, 5 ), new Point( 5, 5 ), new Point( a, 500 ) );
    
    this.Invalidate();
    //base.OnPaint( e );
  }

  static void Main()
  {
    // why not to use while(Created){FullRender();Application.DoEvents();}
    //http://blogs.msdn.com/tmiller/archive/2003/11/07/57524.aspx

    // Also here are records of people burned by it (scroll to bottom)
    //http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx

    // alternative approaches to DoEvents loop
    //http://blogs.msdn.com/tmiller/archive/2003/11/24/57532.aspx

    // Using traditional PeekMessage() loop
    
    // Create-up a window.
    MainWindow mw = new MainWindow();
    mw.Show();
    // (style init stuff is in its constructor)

    Win32.NativeMessage msg = new Win32.NativeMessage() ;

    Dictionary dic = EnumHelper.ReverseEnumLookup();

    while( true ) 
    {
      if( !mw.Created )
      {
        // Need this here for the application to shutdown gracefully.
        break ;
      }

      if( Win32.PeekMessage( out msg, mw.Handle, 0, 0, (uint)Win32.PM.REMOVE ) )
      {
        int msgId = (int)msg.message ;

        Console.WriteLine("MESSAGE!! " + msgId ) ;

        if( dic.ContainsKey( msgId ) )
          Console.WriteLine( "That's " + dic[msgId] ) ;
        else
          Console.WriteLine( "Unknown message!");

        if( msg.message == (uint)Win32.WindowsMessage.WM_QUIT )
        {
          Console.WriteLine("QUITTING...");
          break ;
        }
        else
        {
          Win32.TranslateMessage( ref msg ) ;
          Win32.DispatchMessage ( ref msg ) ;
        }

      }
      
      else
      {

        // Run the game simulation

        // Render current state
        mw.Render();
        
        System.Threading.Thread.Sleep( 2 );
      }
      
    }

    Application.Exit();

  }
}

  
}

But windows won’t double buffer anything you draw unless its in the OnPaint method.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers

%d bloggers like this: