-2

I'm currently programming Java, I have played around with C++ before, but I have never touched GUI as I feel "cheat-like" when using its drag and drop system. I feel good and enjoy programming my own GUI like I do in Java, it feels much more rewarding.

Is there any way I can program the GUI for C#/C++ like in Java, without the drag and drop system, and if so could someone possibly recommend me a tutorial (A lot of the tutorials are using the drag and drop system)?

So far I have tried researching this and really can't find much explaining how I would do it.

closed as off-topic by GrandmasterB, user40980, gnat, Bart van Ingen Schenau, user53019 Mar 3 '14 at 12:45

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking us to recommend a tool, library or favorite off-site resource are off-topic for Programmers as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – GrandmasterB, Community, gnat, Bart van Ingen Schenau, Community
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    C++ doesn't have a 'drag and drop' system for GUI's. You're confusing the language with some of the IDE's that feature GUI designers. And yes, obviously, you can build a GUI entirely from code. – GrandmasterB Mar 1 '14 at 23:17
  • Oh, that may be why. I use VS2013 as I love the feel of the IDE. Do you know of any good tutorials based around creating UI using C#? – Harry Kitchener Mar 1 '14 at 23:18
  • I've tried YouTube to no avail. I prefer video tutorials, but all of them and using drag and drop. – Harry Kitchener Mar 1 '14 at 23:21
  • 5
    What is with people's beef with graphical GUI editors? GUI is an inherently graphical job, don't be afraid to use a graphical tool. It's not cheating. – whatsisname Mar 1 '14 at 23:58
  • I know, I just don't feel happy with myself when I do use one. I mean, if push comes to shove then I will, but unless it's a necessity, I'm not comfortable with it. – Harry Kitchener Mar 2 '14 at 0:03
1

For C# winforms, I found this little tutorial: http://zetcode.com/gui/csharpwinforms/. It's mono-centric, so it uses the unix philosophy that programmers are actually capable of writing code. It should be enough to teach you the basic architecture - for more complex things you can use the MSDN Winforms documentation.

As for C++ - stay away from WinAPI and from MFC! They are nothing like the orderly component hierarchy that are Winforms, WPF, SWT, Swing or even old AWT! You'll only find there a messy swap of Hungarianly notated, macro configured, manually registered low level components with weird names and poor documentation. Even if you prefer to write the GUI code yourself - if you happen to be forced to use the C++ native windows windowing libraries you should make an exception and stick with the GUI builders. That's the only way to preserve your sanity!

Or - try a third party library...

  • 1
    err Winforms is win32. Just like MFC is a wrapper around them, so too is winforms. Try wxWidgets or Qt for 2 widespread alternatives that are used in a lot of open source projects. – gbjbaanb Mar 2 '14 at 13:54
  • I said "WinAPI", not "Win32" - as in "using the WinAPI directly". – Idan Arye Mar 2 '14 at 14:08
  • Thanks idan. The first link was great, I'm currently using it now, it's VERY similar to Java, I love it. – Harry Kitchener Mar 2 '14 at 14:11
  • 1
    @gbjbaanb: WinForms is not "like MFC", it is much better designed UI framework (of course, using Win32 API under the hood on Windows, but obviously not on Linux/Mono). Of course, for C++ "only" I would consider to use Qt, too, especially when portability is important. – Doc Brown Mar 2 '14 at 14:28
  • @DocBrown same methods, same underlying properties etc.. its made much easier to use I admit, but those classes are just the same. The OP seems to think the MFC methods are different to the ones in Winforms! I'd go with Qt myself nowadays, MFC was a decent enough GUI wrapper but all the other stuff MS bunged in there wasn't the best. – gbjbaanb Mar 2 '14 at 20:23
2

I don't know the GUI libraries for C++, so I'm going to talk about C# only. There, there are two common GUI libraries: the older Windows Forms (Winforms for short) and the newer Windows Presentation Foundation (WPF):

Winforms is primarily based on drag-and-drop graphical GUI editor. This editor is somewhat clumsy: for example, I wasn't able to figure out how to place a button at the bottom center of a window (exactly). So, using Winforms might be okay if you're creating non-resizable windows and if you're okay either with the limited tools that are available or with manually deciding the pixel position of every control.

What this editor actually generates is a .Designer.cs file containing code like:

this.button1 = new System.Windows.Forms.Button();

this.button1.Location = new System.Drawing.Point(124, 258);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "OK";
this.button1.UseVisualStyleBackColor = true;

And you can write code like this into your normal .cs files too, bypassing the graphical editor (though I would probably use object initializer syntax here). But since there will be no IDE preview, it will probably take some time to make it look the way you want. And it still doesn't fix the issues that Winforms have, like window resizing.

WPF, on the other hand, while still supporting drag-and-drop to a limited degree, is all about writing structured code in XAML. By “structured”, I mean that controls are placed into groups called panels, which can themselves be in other panels, creating a structure of the window. And each panel decides the placement of controls in it, so by combining panels, you can achieve pretty much any layout, which is now resizable.

For example, to place two buttons at the bottom center of a window, you could write:

<DockPanel LastChildFill="False">
  <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Center">
    <Button Margin="5" Width="100">OK</Button>
    <Button Margin="5" Width="100">Cancel</Button>
  </StackPanel>
</DockPanel>

(The repetition of Margin and Width can be avoided, but it requires more advanced features of WPF.)

And while you're editing the XAML code, the IDE shows you the layout you're creating, so you get one of the main advantages of graphical editors too.

You can also write the same code in C# instead of XAML, but there's only rarely a reason to do that.

  • The panel idea sounds a lot like Swing and I believe I'll be able to relate to this fairly well. Thanks. – Harry Kitchener Mar 2 '14 at 13:27
  • 1
    Yep. I've been using WPF for nearly 6 years and I've never used the designer (I don't like the code it generates). As you pointed out, the designer can show you the layout - that's as far as I go about using it (very rare). The code the designer create adds too much junk (though I haven't tried the one in VS 2012 and 2013... Maybe they've cleaned up that mess). – MetalMikester Mar 2 '14 at 13:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.