如何在F#中使用winforms创建模拟时钟?

如何在F#中使用winforms创建模拟时钟?,winforms,f#,Winforms,F#,我必须使用f#中的winforms创建一个模拟时钟。时钟还需要有一个以数字形式显示日期和时间的标签。我已经算出了时间/日期标签,但我在时钟坐标(截至目前,只有1/4的时钟在右上角可见)和配置时钟指针并使其移动方面遇到了问题-它必须每秒更新一次。我从我的学生书中得到了平移和旋转函数 open System.Windows.Forms open System.Drawing open System let win = new Form () // make a windowform win.Cli

我必须使用f#中的winforms创建一个模拟时钟。时钟还需要有一个以数字形式显示日期和时间的标签。我已经算出了时间/日期标签,但我在时钟坐标(截至目前,只有1/4的时钟在右上角可见)和配置时钟指针并使其移动方面遇到了问题-它必须每秒更新一次。我从我的学生书中得到了平移和旋转函数

open System.Windows.Forms
open System.Drawing
open System

let win = new Form () // make a windowform
win.ClientSize <- Size (500, 500)

// make a label to show time
let digitalTimerLabel = new Label ()
win.Controls.Add digitalTimerLabel
digitalTimerLabel.Width <- 200
digitalTimerLabel.Location <- new Point (140,300) 
digitalTimerLabel.Text <- string System.DateTime.Now // get present time and date

// make a timer and link to label
let timer = new Timer()
timer.Interval <- 1000 // create an event every 1000 millisecond
timer.Enabled <- true // activate the timer
timer.Tick.Add (fun e -> digitalTimerLabel.Text <- string System.DateTime.Now)

// ********* Translate the clock *********
let translate (d : Point) (arr : Point []) : Point [] =
    let add (d : Point) (p : Point) : Point =
        Point (d.X + p.X, d.Y + p.Y)
    Array.map (add d) arr

// ********* Rotate the clock hands *********
let rotate (theta : float) (arr : Point []) : Point [] =
        let toInt = int << round
        let rot (t : float) (p : Point) : Point =
            let (x, y) = (float p.X, float p.Y)
            let (a, b) = (x * cos t - y * sin t, x * sin t + y * cos t)
            Point (toInt a, toInt b)
        Array.map (rot theta) arr

/// ********* ClockHands *********
let myPaint (e : PaintEventArgs) : unit =
    // HourHand
    let black = new Pen (Color.Black,Width=2.0f)
    let hourHand =
    //   [bot cord]    [top cord]
        [|Point (0,0);Point (0,-45)|]
    e.Graphics.DrawLines (black, hourHand)

    // MinuteHand
    let red = new Pen (Color.Red,Width=4.0f)
    let minuteHand =
    //   [bot cord]    [top cord]
        [|Point (0,0);Point (0,-20)|]
    e.Graphics.DrawLines (red, minuteHand)

    // SecondHand
    let green = new Pen (Color.Green,Width=1.0f)
    let secondHand =
    //   [bot cord]    [top cord]
        [|Point (0,0);Point (0,-20)|]
    e.Graphics.DrawLines (green, secondHand)

    // Circle
    let circleBlack = new Pen(Color.Black,Width=4.0f)
    let circle =
        e.Graphics.DrawEllipse(circleBlack, -100.0f,-100.0f,200.0f,200.0f)
    circle

    // CenterDot
    let CenterDotBrush = new SolidBrush(Color.Red)
    let center =
        e.Graphics.FillEllipse(CenterDotBrush,-2.5f,-2.5f,5.0f,5.0f)
    center

    let dt = DateTime.Now
    let s = dt.Second
    let m = dt.Minute
    let h = dt.Hour
    let newS = rotate (float s/60.0*2.0*System.Math.PI) secondHand
    let newM = rotate (float m/60.0*2.0*System.Math.PI) minuteHand
    let newH = rotate (float h/12.0*2.0*System.Math.PI) hourHand
    let finalS = translate (Point (200, 200)) secondHand
    let finalM = translate (Point (200, 200)) minuteHand
    let finalH = translate (Point (200, 200)) hourHand
    ()

win.Paint.Add myPaint
Application.Run win // start the event-loop
open System.Windows.Forms
开放系统.绘图
开放系统
let win=new Form()//创建一个windowform

win.ClientSize您需要在表单上添加eventhandler。 像这样:

win.Paint.Add(fun pea -> pea.Graphics.DrawEllipse(Pen(Color.Red), 50, 50, 400, 400))

现在,您只需划出小时、分钟和秒的界限。

有点离题,但为什么不使用WPF呢?我对使用winforms有一个限制:)很遗憾。现在还不清楚你到底在问什么。