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.


  1. public partial class ScreensaverForm : Form




    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()






    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);



    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)

  2. pretty much
    welding into your thing ;)