質問

I have an f# script using Windows Forms and I want it to run without showing console window (fsi.exe). Is it possible and how? Below is a script example. Console window screenshot

#r "mscorlib.dll"
#r "System.dll"

open System.IO
open System.Windows.Forms

let DoUsefulThing() = ()

let form = new Form()
let button = new Button(Text = "Do useful thing")
button.Click.AddHandler(fun _ _ -> DoUsefulThing())

form.Controls.Add(button)
form.ShowDialog()
役に立ちましたか?

解決

as one option you can PInvoke and hide console window

#r "mscorlib.dll"
#r "System.dll"

open System.IO
open System.Windows.Forms

module FSI =
    [<System.Runtime.InteropServices.DllImport("user32.dll")>]
    extern bool ShowWindow(nativeint hWnd, int flags)
    let HideConsole() = 
        let proc = System.Diagnostics.Process.GetCurrentProcess()
        ShowWindow(proc.MainWindowHandle, 0)

FSI.HideConsole()

let DoUsefulThing() = ()


let form = new Form()
let button = new Button(Text = "Do useful thing")
button.Click.AddHandler(fun _ _ -> DoUsefulThing())

form.Controls.Add(button)
form.ShowDialog()

他のヒント

Isn't the real answer for this sort of task to use fsc and compile down to an .exe which won't show the console window.

I don't think fsi is designed for what you are trying to do

I'm guessing you want to run an F# executable rather than a script. In that case use Visual Studio (I'm using 2017) to create an F# Console Application and in Project => Properties set Output Type to Windows Application (sets the compiler target to winexe).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top