1

I’m posting this question and i hope to find out the responds. My question is: How can i make the code below functional in F# :

First, add these declarations to your Window class:

private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x80000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

Then put this code in the Window's Loaded event:

var hwnd = new WindowInteropHelper(this).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
Ali Yacine
  • 43
  • 3

1 Answers1

0

Translation code from tom.maruska to F# will look as follows:

type HideCloseButtonOnWindow() =
    inherit Behavior<Window>() 

    let GWL_STYLE = -16;
    let WS_SYSMENU = 0x80000;

    [<DllImport("user32.dll", SetLastError = true)>]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [<DllImport("user32.dll")>]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    override x.OnAttached() =
        base.OnAttached()
        x.AssociatedObject.Loaded.AddHandler(fun s e -> x.OnLoaded(s,e))

    override x.OnDetaching() =
        x.AssociatedObject.Loaded.RemoveHandler(fun s e -> x.OnLoaded(s,e))
        base.OnDetaching()


    member x.OnLoaded(sender:obj, e:RoutedEventArgs) =
        let hwnd = (new WindowInteropHelper(x.AssociatedObject)).Handle
        SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) &&& ~~~WS_SYSMENU)
        |> ignore

You should open:

open System.Windows.Interactivity
open System.Runtime.InteropServices
open System.Windows.Interop
Community
  • 1
  • 1
FoggyFinder
  • 2,230
  • 2
  • 20
  • 34