There are times when building things out "by hand" rather than relying on templates/autogeneration are useful to get to know a technology. I don't, personally, believe that is the case here, but I'll try to provide some guidance anyway.
The first thing to know is that you need to run a message loop. I'd strongly recommend using the standard .NET implementation of this (certainly if you want to use other classes within the System.Windows.Forms
namespace) and that's Application.Run
:
Begins running a standard application message loop on the current thread.
But we got a little ahead of ourselves there. We also should be running that from an STA Thread. We get one thread when our application starts running and that calls into Main
. But you can't change the apartment state of a thread after it's started running. So you best add the STAThread attribute to your Main
method so the runtime knows to run it on an STA thread instead1.
Once you do both of those things (passing your myForm
to Application.Run
) you'll have a form shown on screen. You'll want to run other code as well, such as to add controls to your form (myform
will be completely empty) or to take over drawing, and you'll want to do those from methods that the framework calls on the Form2.
For custom drawing, I'd suggest starting at looking at overriding OnPaint
You'll also want to start telling the compiler that your target is winexe
rather than exe
so that you don't have a console window attached. You'll also have to stop using Console
methods.
1By default, the runtime will make your first thread an MTA thread instead.
2Most obviously, because the thread you had is now running the message loop and calling into those methods; the call to Application.Run
doesn't return until the form closes; and you can't (easily) use multiple threads with UI components.