我正在将一个C#项目翻译成F#.虽然逻辑部分很简单,但我对GUI部分感到困惑:
public partial class GomokuGUI : Form {
private void GomokuGUI_Load(object sender, EventArgs e)
{
this.Width = 500;
this.Height = 550;
...
this.Paint += new PaintEventHandler(GomokuGUI_Paint);
Graphics gp = this.CreateGraphics();
DrawChessbord(gp);
}
private void GomokuGUI_Paint(object sender, PaintEventArgs e)
{
Graphics gp = e.Graphics;
DrawChessbord(gp);
}
void DrawChessbord(Graphics gp)
{
float w, h;
SolidBrush br = new SolidBrush(linecolor);
Pen p = new Pen(br, frame);
gp.DrawLine(p, 20, 45, this.Width - 25, 45);
...
}
private void Form1_Click(object sender, EventArgs e) {
Graphics gp = this.CreateGraphics();
DrawChess(gp);
...
}
}
Run Code Online (Sandbox Code Playgroud)
问题:如何在F#中编写上面的C#代码...谢谢
请注意,F#没有任何WinForms设计器,因此如果您在表单中有一些控件,则需要手动创建它们(或者您可以在C#中设计表单,编译它并从F#引用它).你可以从这样的事情开始:
type GomokuGUI() as this =
inherit Form(Width = 300, Height = 550)
let DrawChessbord (gp:Graphics) =
let br = new SolidBrush(linecolor)
let p = new Pen(br, frame)
gp.DrawLine(p, 20, 45, this.Width - 25, 45)
// ...
let paintGui (e:PaintEventArgs) =
let gp = e.Graphics
DrawChessbord(gp)
do
this.Paint.Add(paintGui)
this.Click.Add(fun _ ->
let gp = this.CreateGraphics()
DrawChess(gp)
(* ... *) )
Run Code Online (Sandbox Code Playgroud)
它使用了一些有趣的东西:
Width,可以Height在构造函数中指定一些参数,例如和.as this在类声明中使用,以便您可以引用do代码中的类(在构造期间运行)Add用来注册事件处理程序,你可以给它一个命名函数(例如patinGui)或一个lambda函数,如果你只需要做一些简单的调用(例如处理Click)| 归档时间: |
|
| 查看次数: |
1357 次 |
| 最近记录: |