0

how do i draw a moving rectangle in windows form in pure c#? i can't find anything anywhere surprisingly, it's always a tutorial by using visual studio. I'm looking for ways to do it in pure c# without any ide. Here is what i tried so far.

using System;
using System.Windows.Forms;
using System.Drawing;

namespace form
{
     class Program
     {
         static void Main(string[] args)
         {
             Form myform = new Form();

             while (myform.Created)
             {

             }

             Console.ReadLine();
         }
     }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
James Penner
  • 71
  • 1
  • 6
  • 2
    Visual Studio is an IDE. It doesn’t make the code “impure C#” if you write code in a specific editor – Sami Kuhmonen Dec 24 '19 at 10:37
  • @Sami Kuhmonen Visual studio let's you pick a project type and one of them is windows form application, if you select that it's no longer pure c# because it already adds extra functions to make things easier. Most tutorials I found are using visual studio and it's not what i want. – James Penner Dec 24 '19 at 10:40
  • 1
    @JamesPenner If you follow that logic `using System.Drawing;` is not "pure" either. – SᴇM Dec 24 '19 at 10:42
  • @Sami Kuhmonen https://www.youtube.com/watch?v=PRpYDTegGyo like in this video, c# doesnt even have a InitializeComponent() function and in visual studio you dont even have to instantiate the form class to make a window. it's definitely not pure c#. I probably worded things badly, what I meant is not using any extra code/functions/libraries from an IDE. – James Penner Dec 24 '19 at 10:43
  • @Sami Kuhmonen I probably worded things badly, what I meant is not to use any IDE. just a notepad, a .cs file and a c# compiler – James Penner Dec 24 '19 at 10:47
  • @JamesPenner there you go https://stackoverflow.com/questions/553143/compiling-executing-a-c-sharp-source-file-in-command-prompt – gsharp Dec 24 '19 at 11:18
  • @gsharp that's not what i meant, I'm looking for the code to draw and move the rectangle, not how to compile it. – James Penner Dec 24 '19 at 11:19
  • @JamesPenner did you google it? it's full of examples. https://stackoverflow.com/questions/15981840/how-to-draw-and-move-shapes-using-mouse-in-c-sharp – gsharp Dec 24 '19 at 11:23
  • @gsharp it's not what I want, the link you shown is using an ide, im looking for the code without any ide – James Penner Dec 24 '19 at 11:44
  • @JamesPenner yeah but not that much the code that is generated in InizializeComponent() is just minimal, you can adapt that code in no time to your needs. I really don't see the problem. – gsharp Dec 24 '19 at 12:27

1 Answers1

1

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.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448