2010-02-19 55 views
1

我正在將C#項目翻譯成F#。雖然邏輯部分很容易,我很困惑與GUI部分:F#GUI問題(將C#翻譯成F#)

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); 
    ... 
} 
} 

問題:如何寫在上面F#C#代碼...感謝

+0

如何寫在F#此C#代碼...謝謝。 – 2010-02-19 13:01:05

回答

5

注意,F#沒有任何WinForms設計器,所以如果你在Form中有一些控件,你需要手動創建它們(或者你可以用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) 
     (* ... *)) 

它採用了兩個有趣的問題:

  • 調用基類的構造函數時,您可以指定一些參數,如在構造函數中WidthHeight
  • 你需要在類聲明中使用as this,這樣你可以參考類的do代碼(這是施工過程中運行)
  • 您可以使用Add註冊事件處理程序,你可以給它一個命名函數(如patinGui),或者如果你需要做的只是一些簡單的調用(例如Click處理)lambda函數
1

下面是一個使用形式的另一個例子,但這次與外部3D庫稱爲Mogre

type MogreForm() as this = 
    inherit Form() 

    let mogrePanel = new System.Windows.Forms.Panel() 

    // Between Suspend and Resume Layout is normal form Designer Code 
    do base.SuspendLayout() 

     mogrePanel.Location <- new System.Drawing.Point(0, 0) 
     mogrePanel.Name <- "mogrePanel" 
     mogrePanel.Size <- new System.Drawing.Size(483, 375) 
     mogrePanel.TabIndex <- 0 

     base.AutoScaleDimensions <- new System.Drawing.SizeF(6.0f, 13.0f) 
     base.AutoScaleMode <- System.Windows.Forms.AutoScaleMode.Font 
     base.ClientSize <- new System.Drawing.Size(483, 375) 
     base.Controls.Add(mogrePanel) 
     base.Name <- "MogreForm" 
     base.Text <- "Simple F# Mogre Form"; 

     base.ResumeLayout(false) 

     let mogreWin = new OgreWindow(Point(100, 30), mogrePanel.Handle) 
     this.Disposed.Add(fun _ -> mogreWin.Dispose()) 
     this.Paint.Add(fun _ -> mogreWin.Paint()) 

這裏是完整的代碼,如果你想嘗試運行它。您需要下載Mogre並參考Mogre DLL。此外,由於該代碼拉出來的資源,你必須設置你的項目的工作目錄中的樣本爲「C:\ MogreSDK \ BIN \調試」

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

open Mogre 

type OgreWindow(origin, hWnd) = 
    //----------------------------------------------------- 
    // 1 enter ogre 
    //----------------------------------------------------- 
    let root = new Root() 

    do //----------------------------------------------------- 
     // 2 configure resource paths 
     //----------------------------------------------------- 
     let cf = new ConfigFile() 
     cf.Load("resources.cfg", "\t:=", true) 

     // Go through all sections & settings in the file 
     let seci = cf.GetSectionIterator() 

     // Normally we would use the foreach syntax, which enumerates the values, but in this case we need CurrentKey too; 
     while seci.MoveNext() do 
      for pair in seci.Current do 
       ResourceGroupManager.Singleton.AddResourceLocation(pair.Value, pair.Key, seci.CurrentKey) 

     //----------------------------------------------------- 
     // 3 Configures the application and creates the window 
     //----------------------------------------------------- 
     root.RenderSystem <- root.GetAvailableRenderers() |> Seq.find (fun rs -> rs.Name = "Direct3D9 Rendering Subsystem") 
     root.RenderSystem.SetConfigOption("Full Screen", "No") 
     root.RenderSystem.SetConfigOption("Video Mode", "640 x 480 @ 32-bit colour") 

     root.Initialise(false) |> ignore 
     let misc = new NameValuePairList() 
     misc.["externalWindowHandle"] <- hWnd.ToString() 
     let window = root.CreateRenderWindow("Simple Mogre Form Window", 0u, 0u, false, misc.ReadOnlyInstance) 
     ResourceGroupManager.Singleton.InitialiseAllResourceGroups() 

     //----------------------------------------------------- 
     // 4 Create the SceneManager 
     // 
     //  ST_GENERIC = octree 
     //  ST_EXTERIOR_CLOSE = simple terrain 
     //  ST_EXTERIOR_FAR = nature terrain (depreciated) 
     //  ST_EXTERIOR_REAL_FAR = paging landscape 
     //  ST_INTERIOR = Quake3 BSP 
     //----------------------------------------------------- 
     let sceneMgr = root.CreateSceneManager(SceneType.ST_GENERIC, "SceneMgr") 
     sceneMgr.AmbientLight <- new ColourValue(0.5f, 0.5f, 0.5f) 

     //----------------------------------------------------- 
     // 5 Create the camera 
     //----------------------------------------------------- 
     let camera = sceneMgr.CreateCamera("SimpleCamera") 
     camera.Position <- new Vector3(0.0f, 0.0f, 100.0f) 
     // Look back along -Z 
     camera.LookAt(new Vector3(0.0f, 0.0f, -300.0f)) 
     camera.NearClipDistance <- 5.0f 

     let viewport = window.AddViewport(camera) 
     viewport.BackgroundColour <- new ColourValue(0.0f, 0.0f, 0.0f, 1.0f) 

     let ent = sceneMgr.CreateEntity("ogre", "ogrehead.mesh") 
     let node = sceneMgr.RootSceneNode.CreateChildSceneNode("ogreNode") 
     node.AttachObject(ent) 

    member this.Paint() = 
     root.RenderOneFrame() |> ignore 

    member this.Dispose() = 
     if root <> null then 
      root.Dispose() 

type MogreForm() as this = 
    inherit Form() 

    let mogrePanel = new System.Windows.Forms.Panel() 

    // Between Suspend and Resume Layout is normal form Designer Code 
    do base.SuspendLayout() 

     mogrePanel.Location <- new System.Drawing.Point(0, 0) 
     mogrePanel.Name <- "mogrePanel" 
     mogrePanel.Size <- new System.Drawing.Size(483, 375) 
     mogrePanel.TabIndex <- 0 

     base.AutoScaleDimensions <- new System.Drawing.SizeF(6.0f, 13.0f) 
     base.AutoScaleMode <- System.Windows.Forms.AutoScaleMode.Font 
     base.ClientSize <- new System.Drawing.Size(483, 375) 
     base.Controls.Add(mogrePanel) 
     base.Name <- "MogreForm" 
     base.Text <- "Simple F# Mogre Form"; 

     base.ResumeLayout(false) 

     let mogreWin = new OgreWindow(Point(100, 30), mogrePanel.Handle) 
     this.Disposed.Add(fun _ -> mogreWin.Dispose()) 
     this.Paint.Add(fun _ -> mogreWin.Paint()) 

let main() = 
    Application.EnableVisualStyles() 
    Application.SetCompatibleTextRenderingDefault(false) 
    Application.Run(new MogreForm()) 

[<STAThread>] 
do main()