Gradient and Textured Form in Visual F#

To create a gradient form in Visual F#, simply use a gradient brush and use that brush to draw a rectangle that will cover the whole form area. To create a LinearGradientBrush, use the following syntax:
let newbrush=new LinearGradientBrush(brushpoint,color1,color2,strokeangle)
For instance:
let newbrush=new LinearGradientBrush(rect,Color.Green,Color.YellowGreen,45.0F)
Follow these steps to create a gradient form using the LinearGradientBrush drawing method:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

// Learn more about F# at http://fsharp.net
//specifies the namespace memory location of the classes that
//will be used in our application
open System
open System.Windows.Forms
open System.Drawing
open System.Drawing.Drawing2D
//creates our form
let myform= new Form(Text="Add Gradient Background")
//create a rectangle
//change its upper left x and y coordinates to 0,0
//sets it width and height to the width and height of the form
let rect=new Rectangle(0,0,myform.Width,myform.Height)
//creates a gradient brush
let newbrush=new LinearGradientBrush(rect,Color.Green,Color.YellowGreen,45.0F)
//paints our form with a solid and filled rectangle
myform.Paint.Add(fun fillrect->fillrect.Graphics.FillRectangle(newbrush,rect)) 
//executes our application
myform.Show()
Application.Run(myform)

This will display the following output:



To create a textured form, use a TextureBrush to draw a rectangle that will cover the whole form area then display that rectangle on your form.

For instance:
// Learn more about F# at http://fsharp.net
//specifies the namespace memory location of the classes that
//will be used in our application
open System
open System.Windows.Forms
open System.Drawing
open System.Drawing.Drawing2D

//creates our form
let myform= new Form(Text="Add Textured Background")
//draws a rectangle with the same width and height as our form
let drawrect=new Rectangle(0,0,myform.Width,myform.Height)
//creates a texturedbrush using a windows default image
//tile the image Vertically and Horizontally
let newbrush=new TextureBrush(new Bitmap("C:\Windows\Zapotec.bmp"),WrapMode=WrapMode.TileFlipXY)
//applies the brush in drawing our rectangle
myform.Paint.Add(fun fillrect->fillrect.Graphics.FillRectangle(newbrush, drawrect)) 
//executes our application
myform.Show()
Application.Run(myform)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.