Using GDI+ with F#

F# can pretty much work with .NET just as C# and VB.NET do.  Admittedly, F# takes some getting used to, but you can still create  classes, override classes, and utilize classes the same way you do in other .NET languages. F# also appears to be a lot more compact when you write it.  You'll notice a lot of places don't even have semicolons or brackets.

Here is the code for a Windows Form that Tracks the Mouse and Paints a rectangle based on the position of the mouse down click.

Note that I first created a new FSharp Project and then created a new FSharp file in my project.  I then pasted the code below:

open System;
open Drawing;
open Windows.Forms;

// define the class here type <classname> = class .... end
type MainForm = class
  inherit
Form
 
 
val mutable g : Graphics // mutable means its not read-only
 
val mutable position : Point // position of the rectangle

 

// constructor (must initialize all members here)
   new () as form = {g=null;position = new Point(0,0)} then
  
// double buffering
    
form.SetStyle (ControlStyles.UserPaint, true);
    
form.SetStyle (ControlStyles.DoubleBuffer, true);
    
form.SetStyle (ControlStyles.AllPaintingInWmPaint, true);
    
form.Text <- "F# GDI+ Window Form Sample";

     // show the form
    
form.Show()

 

// override of paint event handler
override form.OnPaint e =
  
let g = e.Graphics in
   
g.FillRectangle(Brushes.Aqua, form.position.X+1, form.position.Y+1,
        48, 48);
   
g.DrawRectangle(Pens.Red, form.position.X, form.position.Y, 50,
       50);

 

// override of on mouse down event handler
override form.OnMouseDown e =
 
form.Invalidate(new Rectangle(form.position.X-5, form.position.Y-5,
   60, 60));
 
form.position.X <- e.X;
 
form.position.Y <- e.Y;
 
form.Invalidate(new Rectangle(form.position.X-5, form.position.Y-5,
    60, 60));

end  // end of the class

 

// create the form

let form = new MainForm()

 

// run the application with the form

do Application.Run(form)

 

The code consists of a MainForm class that inherits from a Windows Form and the code to to launch the MainForm.  The MainForm class consists of a constructor (which uses the keyword new),  an override of the OnPaint event handler and an override of the OnMouseDown event handler.  The rectangle is drawn in the OnPaint event handler with a light blue interior and a red border using the graphics object passed into OnPaint.  The rectangle is drawn at a position determined by the mouse location during a mouse down event.  The position is recalculated in the OnMouseDown event every time the user clicks in the form.  The OnMouseDown event also invalidates the locations of the old and new rectangle so that a paint event is forced in the window to redraw those areas.

Running your Windows Form

After pasting the code above, I built and ran my F# application.  When compiling your project, you'll also need to change the project to a WINEXE project in the project properties, otherwise it will bring up the console in the background:

 

The results are what you might have expected, a rectangle that responds to the left mouse click in the form:

Now you are ready to start creating simple 2D games using F#!  Stay tuned...

Ebook Download
View all
Learn
View all