F# 프로그램으로 버튼의  마우스 이벤트를 감지하여 버튼의 색깔을 변경하는 윈도우폼 애플리케이션을 작성해 보았다.  실행시키고 버튼을 클릭할 때마다 버튼의 색깔이 바뀐다.


// Filename: ClickForm5.fs
//
// Compile: fsc --codepage:949 ClickForm5.fs
// Execute: ClickForm5

// Import useful .NET namespaces
open System
open System.Drawing
open System.Windows.Forms

// Create the form and a label objects
let frm = new Form(Text = "버튼 이벤트 핸들러 테스트", Height = 200)
let btn = new Button(Text = "여기를 클릭!", Width=240, Height=60,
                    TextAlign = ContentAlignment.MiddleCenter)
let displaybox = new Label(Width=260, Height=35,
                    TextAlign = ContentAlignment.MiddleLeft)

// Set ‘Font’ property of the label and add label to the form
btn.Font <- new Font("궁서체", 24.0f)
btn.Location <- new System.Drawing.Point(25, 80)
displaybox.BackColor <- Color.FromArgb(255, 255, 255)
displaybox.Font <- new Font("돋움체", 13.0f)
displaybox.Location <- new Point(15, 20)
frm.Controls.Add(btn)
frm.Controls.Add(displaybox)
frm.Show()

// Create random number generator
let rnd = new Random()  

// Function that changes the text of the label and back color of the form
let handleClick (e:EventArgs) =
    let r = rnd.Next(256)
    let g = rnd.Next(256)
    let b = rnd.Next(256)
    // Set the text and the new back color
    btn.Text <- if (btn.Text = "틱!") then "택!" elif (btn.Text = "택!") then "톡!" else "틱!"
    btn.BackColor <- Color.FromArgb(r, g, b)
    displaybox.Text <- String.Format("(R, G, B) = ({0}, {1}, {2})", r, g, b)

btn.Click.Add(handleClick)

do
  Application.Run(frm)



 

                           <실행하여 마우스를 몇 번 클릭한 화면>



크리에이티브 커먼즈 라이선스
Creative Commons License

 

Posted by Scripter
,