4 people like it.
Like the snippet!
Draw ellipses on WPF on button click
Can be run in interactive or compiled with FSC. Cobbled together from various other snippets :)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
|
#r @"PresentationCore"
#r @"PresentationFramework"
#r @"WindowsBase"
#r @"System.Xaml"
#r @"UIAutomationTypes"
open System
open System.Windows
open System.Windows.Media
open System.Windows.Shapes
open System.Windows.Controls
let window = Window(Height = 400.0, Width = 400.0)
window.Title <- "Draw test"
let stackPanel = StackPanel()
window.Content <- stackPanel
stackPanel.Orientation <- Orientation.Vertical
let button1 = Button()
button1.Content <- "Click me to draw a blue ellipse"
stackPanel.Children.Add button1
let button2 = Button()
button2.Content <- "Click me to draw a red ellipse"
stackPanel.Children.Add button2
let clearButton = Button()
clearButton.Content <- "Click me to clear the canvas"
stackPanel.Children.Add clearButton
let canvas = Canvas()
canvas.Width <- window.Width
canvas.Height <- window.Height
stackPanel.Children.Add canvas
let buildEllipse height width fill stroke =
let ellipse = Ellipse()
ellipse.Height <- height
ellipse.Width <- width
ellipse.Fill <- fill
ellipse.Stroke <- stroke
ellipse
let ellipse1 = buildEllipse 100.0 200.0 Brushes.Aqua Brushes.Black
Canvas.SetLeft(ellipse1, canvas.Width / 10.0)
Canvas.SetTop(ellipse1, canvas.Height / 10.0)
let ellipse2 = buildEllipse 200.0 100.0 Brushes.Red Brushes.DarkViolet
Canvas.SetLeft(ellipse2, canvas.Width / 4.0)
Canvas.SetTop(ellipse2, canvas.Height / 5.0)
let addEllipseToCanvas (canvas:Canvas) (ellipse:Ellipse) =
match canvas.Children with
| c when c.Contains ellipse ->
canvas.Children.Remove ellipse
canvas.Children.Add(ellipse) |> ignore //needs to be removed and readded or the canvas complains
| _ ->
canvas.Children.Add(ellipse) |> ignore
button1.Click.Add(fun _ -> addEllipseToCanvas canvas ellipse1)
button2.Click.Add(fun _ -> addEllipseToCanvas canvas ellipse2)
clearButton.Click.Add(fun _ -> canvas.Children.Clear())
#if INTERACTIVE
window.Show()
#else
[<EntryPoint; STAThread>]
let main argv =
let app = new Application()
app.Run(window)
#endif
|
namespace System
namespace System.Windows
namespace System.Windows.Media
namespace System.Windows.Shapes
namespace System.Windows.Controls
val window : Window
Full name: Script.window
Multiple items
type Window =
inherit ContentControl
new : unit -> Window
member Activate : unit -> bool
member AllowsTransparency : bool with get, set
member Close : unit -> unit
member DialogResult : Nullable<bool> with get, set
member DragMove : unit -> unit
member Hide : unit -> unit
member Icon : ImageSource with get, set
member IsActive : bool
member Left : float with get, set
...
Full name: System.Windows.Window
--------------------
Window() : unit
property Window.Title: string
val stackPanel : StackPanel
Full name: Script.stackPanel
Multiple items
type StackPanel =
inherit Panel
new : unit -> StackPanel
member CanHorizontallyScroll : bool with get, set
member CanVerticallyScroll : bool with get, set
member ExtentHeight : float
member ExtentWidth : float
member HorizontalOffset : float
member LineDown : unit -> unit
member LineLeft : unit -> unit
member LineRight : unit -> unit
member LineUp : unit -> unit
...
Full name: System.Windows.Controls.StackPanel
--------------------
StackPanel() : unit
property ContentControl.Content: obj
property StackPanel.Orientation: Orientation
type Orientation =
| Horizontal = 0
| Vertical = 1
Full name: System.Windows.Controls.Orientation
field Orientation.Vertical = 1
val button1 : Button
Full name: Script.button1
Multiple items
type Button =
inherit ButtonBase
new : unit -> Button
member IsCancel : bool with get, set
member IsDefault : bool with get, set
member IsDefaulted : bool
static val IsDefaultProperty : DependencyProperty
static val IsCancelProperty : DependencyProperty
static val IsDefaultedProperty : DependencyProperty
Full name: System.Windows.Controls.Button
--------------------
Button() : unit
property Panel.Children: UIElementCollection
UIElementCollection.Add(element: UIElement) : int
val button2 : Button
Full name: Script.button2
val clearButton : Button
Full name: Script.clearButton
val canvas : Canvas
Full name: Script.canvas
Multiple items
type Canvas =
inherit Panel
new : unit -> Canvas
static val LeftProperty : DependencyProperty
static val TopProperty : DependencyProperty
static val RightProperty : DependencyProperty
static val BottomProperty : DependencyProperty
static member GetBottom : element:UIElement -> float
static member GetLeft : element:UIElement -> float
static member GetRight : element:UIElement -> float
static member GetTop : element:UIElement -> float
static member SetBottom : element:UIElement * length:float -> unit
...
Full name: System.Windows.Controls.Canvas
--------------------
Canvas() : unit
property FrameworkElement.Width: float
property FrameworkElement.Height: float
val buildEllipse : height:float -> width:float -> fill:Brush -> stroke:Brush -> Ellipse
Full name: Script.buildEllipse
val height : float
val width : float
val fill : Brush
val stroke : Brush
val ellipse : Ellipse
Multiple items
type Ellipse =
inherit Shape
new : unit -> Ellipse
member GeometryTransform : Transform
member RenderedGeometry : Geometry
Full name: System.Windows.Shapes.Ellipse
--------------------
Ellipse() : unit
property Shape.Fill: Brush
property Shape.Stroke: Brush
val ellipse1 : Ellipse
Full name: Script.ellipse1
type Brushes =
static member AliceBlue : SolidColorBrush
static member AntiqueWhite : SolidColorBrush
static member Aqua : SolidColorBrush
static member Aquamarine : SolidColorBrush
static member Azure : SolidColorBrush
static member Beige : SolidColorBrush
static member Bisque : SolidColorBrush
static member Black : SolidColorBrush
static member BlanchedAlmond : SolidColorBrush
static member Blue : SolidColorBrush
...
Full name: System.Windows.Media.Brushes
property Brushes.Aqua: SolidColorBrush
property Brushes.Black: SolidColorBrush
Canvas.SetLeft(element: UIElement, length: float) : unit
Canvas.SetTop(element: UIElement, length: float) : unit
val ellipse2 : Ellipse
Full name: Script.ellipse2
property Brushes.Red: SolidColorBrush
property Brushes.DarkViolet: SolidColorBrush
val addEllipseToCanvas : canvas:Canvas -> ellipse:Ellipse -> unit
Full name: Script.addEllipseToCanvas
val canvas : Canvas
val c : UIElementCollection
UIElementCollection.Contains(element: UIElement) : bool
UIElementCollection.Remove(element: UIElement) : unit
val ignore : value:'T -> unit
Full name: Microsoft.FSharp.Core.Operators.ignore
event Primitives.ButtonBase.Click: IEvent<RoutedEventHandler,RoutedEventArgs>
member IObservable.Add : callback:('T -> unit) -> unit
UIElementCollection.Clear() : unit
Multiple items
type EntryPointAttribute =
inherit Attribute
new : unit -> EntryPointAttribute
Full name: Microsoft.FSharp.Core.EntryPointAttribute
--------------------
new : unit -> EntryPointAttribute
Multiple items
type STAThreadAttribute =
inherit Attribute
new : unit -> STAThreadAttribute
Full name: System.STAThreadAttribute
--------------------
STAThreadAttribute() : unit
Multiple items
type Application =
inherit DispatcherObject
new : unit -> Application
member FindResource : resourceKey:obj -> obj
member MainWindow : Window with get, set
member Properties : IDictionary
member Resources : ResourceDictionary with get, set
member Run : unit -> int + 1 overload
member Shutdown : unit -> unit + 1 overload
member ShutdownMode : ShutdownMode with get, set
member StartupUri : Uri with get, set
member TryFindResource : resourceKey:obj -> obj
...
Full name: System.Windows.Application
--------------------
Application() : unit
More information