I found this answer for Python here Question/Answer. I wanted to know how I could do this in F# and what specifically do I need to import and how do I get the device context. I realize it's better to use a window but I don't want too for the project I'm working on. I'm guessing there is a way to get the width/height of the screen too.
Here is a minimal example that uses the GetDC
and SetPixel
functions to do exactly the same thing as the Python example in the referenced question:
open System
open System.Runtime.InteropServices
[<DllImport("user32.dll",EntryPoint="GetDC")>]
extern IntPtr GetDC(IntPtr ptr)
[<DllImport("gdi32.dll")>]
extern uint32 SetPixel(IntPtr hdc, int X, int Y, uint32 crColor);
let dc = GetDC(IntPtr.Zero)
for i in 0 .. 255 do
let r, g, b = 255, i, 255
let clr = (r <<< 16) ||| (g <<< 8) ||| b
SetPixel(dc, i, 0, uint32 clr) |> ignore
That said, I cannot imagine a scenario where this would be a good thing to do. If you are creating a sensible windows application, you will most certainly want to draw anything in the window belonging to your app.