Home > OS >  C# GUI without Windows Forms/WPF/Any other pre-built thing
C# GUI without Windows Forms/WPF/Any other pre-built thing

Time:10-12

I'm interested in creating my own GUI from scratch, and by scratch, I mean scratch. I get that I would have to just essentially re-code a custom WinForm type application but I have no idea where to start. How would I go about actually making a GUI appear on screen from just C# code as if I was creating a console app per se?

Please note that why I want to do this is not relevant. I'm fully aware that I can just use WinForms, WPF, or any other existing framework.

I am using Visual Studio 2019.

When I say "I don't know where to start", I think I should note that I am proficient in C# so not knowing the language isn't my starting point.

I'm only looking for C#-based answers. I already know that other languages have ways of doing this.

CodePudding user response:

Fundamentally, you will need to call system (OS) level APIs to draw stuff on the screen. Frameworks like WinForms and WPF are just complex wrappers around those fundamental APIs. One of the advantages of these frameworks is that they simplify programming, making common tasks accomplished more easily. Another advantage is that they offer some level of platform independence. The OS-level APIs on Windows are, for example, quite different than the corresponding ones on a Linux system (and, in fact, there is no such thing as a "Linux" API in this sense; rather, you would need to know which specific X-Windows manager was being used and target that). Thus, you'd need to write separate, special code for each system that you wanted to support, and that would be a big pain. But it can certainly be done.

In C#, you make platform-native calls using a facility called P/Invoke. WinForms uses this throughout to make Windows API calls to get things on the screen. You can write a Windows application that displays a platform-native user interface in C# in much the same way as you would in C or C , by calling the Win32 API directly. There are functions like CreateWindow that do exactly as the name suggests. A book that teaches the Windows API, like Charles Petzold's famous Programming Windows, 5th Edition, will teach you the necessary platform API calls. Combine this with some basic information from the C# language documentation about how to use P/Invoke to call native API functions, and you're there. (Note that you must get the 5th edition of Petzold's book. This is the last edition that teaches native Windows API programming in C. The newer editions use the C# wrapper frameworks that you are seeking to avoid.)

If you want to get a window on screen in Windows, you don't have much choice but to call these basic platform-provided functions. You can also use the platform-provided functions to draw the user interface, but you don't have to. You could completely custom-draw the entire user interface, which would give you an additional layer of control. To do this, you would basically use primitive functions provided by either the graphics driver (via the OS API, which gives a universal interface to all graphics drivers on Windows), or use some other framework (such as OpenGL) that does a similar thing.

At this point, you may be objecting that it's frameworks all the way down. Well, yeah. :-) Programmers are an inherently lazy bunch, and for good reason—why do more work than we have to? You have to decide at which level of abstraction you want to work.

When building a GUI application, you won't be able to go all the way down in terms of abstractions. You're still building on top of the OS and/or window manager, so you're bound by it, and need to work with it. You can either do that directly, or you can do that via a framework that provides a relatively thin wrapper, or you can do that via a whole bunch of frameworks that provide increasingly more complex and higher-level wrappers.

  • Related