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)
'프로그래밍 > F#' 카테고리의 다른 글
F# GUI Hello 예제 (윈도우 7 용) (0) | 2011.08.24 |
---|---|
F# 용 GUI Hello 예제 (0) | 2011.08.14 |
F# 윈도우 애플리케이션에서 마우스 이벤트 감지하기 (0) | 2010.07.23 |
30000! 빨리 계산하기 with F# (0) | 2010.07.20 |
int 타입과 bigint 타입 간 상호 변환 with F# (0) | 2010.07.18 |