Paint Application in Visual F#

The following example demonstrates how to make a simple Paint-like application in Visual F#:

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. open System.Drawing.Drawing2D  
  10. let drawingform = new Form(Text="Draw Objects",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  11. //creates our control  
  12. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  13. let erasebutton=new Button(Text="Erase", Location=new System.Drawing.Point(120, 200))  
  14. let colorbutton=new Button(Text="Brush Color", Location=new System.Drawing.Point(40, 200))  
  15. drawingform.Controls.Add(exitbutton)  
  16. drawingform.Controls.Add(erasebutton)  
  17. drawingform.Controls.Add(colorbutton)  
  18. //creates a color dialog box  
  19. let colordlg=new ColorDialog()  
  20. //creates a colorblend object  
  21. let mutable color=new ColorBlend()  
  22.   
  23. let gr=drawingform.CreateGraphics()  
  24. gr.SmoothingMode<-SmoothingMode.HighQuality  
  25. //when the form is loaded, change its color to white  
  26. drawingform.Load.Add(fun background->  
  27. //set the default brush color to indigo  
  28. color.Colors<-[|Color.Indigo|]  
  29.                         drawingform.BackColor<-Color.White)  
  30.   
  31. drawingform.MouseMove.Add(fun trail->  
  32. //when the mouse button is moved and the left button is clicked  
  33.   
  34. if (trail.Button=System.Windows.Forms.MouseButtons.Left)then  
  35. //draw the object assign the color seleted from the color dialog as a brush color  
  36. gr.FillRectangle(new SolidBrush(color.Colors.[0]),new Rectangle(trail.X,trail.Y,5,5)))   
  37. //when the erase button is clicked  
  38. //erase the object drawn in the form  
  39. erasebutton.Click.Add(fun erase->gr.Clear(Color.White))   
  40. //when the exit button is clicked  
  41. //quit the form                                                                                                                
  42. exitbutton.Click.Add(fun quit->drawingform.Close())  
  43. //when the brush color button is selected                                                            
  44. colorbutton.Click.Add(fun colors->  
  45. //display the Color Dialog box  
  46. if colordlg.ShowDialog()=DialogResult.OK then  
  47. //store the value selected by the user in our colorblend object  
  48. color.Colors<-[|colordlg.Color|])  
  49. //executes our application  
  50. Application.Run(drawingform)  
Click the run icon once you are done entering these codes in the code editor window. You should see the following outputs: