Monday, July 12, 2010

Matrix Screensaver

With the knowledge from the previous post of how to create screensavers with C# I wrote a screensaver which looks kind of the well known screen from the movie matrix.
In the screensaver all visible letters are instances of a class, which implements a function to draw the letters.
In specific time intervals rows with letters are created and all letters are moved down continiously.
The drawing does not take place directly on the form but in a buffer. Only when all letters are drawn in this, the complete content of the buffer is transfered to the form (this is called rendering).
I would like to just post the complete sourcecode here, hope some of you enjoy trying this.
You can download the commented (in German) sourcecode here,  the executable screensaver file is available here.


Edit: A German video about this post is now also available on Youtube.

2 comments:

  1. public partial class ScreensaverForm : Form

    {

     

    [System.Runtime.InteropServices.DllImport("gdi32.dll")]

    private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,

    IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);

     

    private PrivateFontCollection fonts = new PrivateFontCollection();

     

    public static Font myFont;

     

     

     

    public ScreensaverForm()

    {

     



     

    InitializeComponent();

     

    byte[] fontData = Properties.Resources.matrix_code_nfi;

    IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);

    System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);

    uint dummy = 0;

    fonts.AddMemoryFont(fontPtr, Properties.Resources.matrix_code_nfi.Length);

    AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.matrix_code_nfi.Length, IntPtr.Zero, ref dummy);

    System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);

     

    myFont = new Font(fonts.Families[0], 16.0F);





    }

    }



    This would allow the embedding of a custom Font (such as an actual matrix-a-like one)

    ReplyDelete
  2. pretty much
    http://stackoverflow.com/questions/556147/how-to-quickly-and-easily-embed-fonts-in-winforms-app-in-c-sharp
    welding into your thing ;)

    ReplyDelete