6

A bit of background: I'm a C++ programmer mostly, but the only GUI stuff I've ever done was on top of .NET's WinForms platform. I'm completely new to Windows GUI programming, and despite Petzold's excellent book, I'm extremely confused.

Namely, it seems that most every reference on getting started with Win32 is all about drawing lines and curves and things -- a topic about which (at least at present time) I couldn't care less.

I need a checked list box, a splitter, and a textbox -- something that would take less than 10 minutes to do in Winforms land. It has been recommended to me to use the WTL library, which provides an implementation of all three of these controls -- but I keep getting hung up on simple things, such as getting the controls to use the right font, and getting High DPI working correctly. I've spent two days on this, and I can't help but think there has to be a better reference for these kinds of things than I've been able to find. Petzold's book is good, but it hasn't been updated since Windows 95 days, and there's been a LOT changed w.r.t. how applications should be correctly developed since it was published.

I guess what I'm looking for is a modern Petzold book. Where can I find such a resource, if any?

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • 1
    +1 I've wondered this exact same thing more than once, and certainly felt your frustration. MFC seems like the best solution, but it doesn't exactly feel like a modern solution for a brand new app. – Cody Gray - on strike Jan 02 '11 at 06:20
  • 1
    I was thinking exactly what you were thinking when I started programming Win32 GUIs (in C). I couldn't get the font to work, and the controls were flickering when I resized the window, etc. Usually they turn out to be the result of very subtle errors or misunderstood Win32 idioms. – wj32 Jan 02 '11 at 06:37
  • A different angle on a similar complaint at http://stackoverflow.com/questions/1643260/using-the-browser-for-desktop-ui – mr calendar Jan 02 '11 at 13:35
  • 3
    @mr calendar: Not an option unfortunately. – Billy ONeal Jan 02 '11 at 19:21
  • It is not easy. WinForm is comfortable and you can create GUI rapidly. But when you accustom with "spartan" method, you will be impressed of the high speed of execution (program start, etc.) compared to .Net/WinForm. The main advantage of Winform is that there are additional controls which do not exist natively in Win32. – i486 Feb 07 '23 at 14:21

3 Answers3

4

First up, there is virtually no documentation (that I could ever find) explaining how to use WTL. It seems to be a library by experts, for experts.

Your choices then are: Using the MFC app wizard to create an application, or going the Win32 API route. The Win32 API itself is a C API, MFC is a C++ wrapper around the C Win32 API, with the addition of a document-view application model.

However, instead of creating your controls from code: The closest analog the native Windows API has to a "WinForm" is a dialog box. Dialogs are layed out in resource files that are embedded during linking into the EXE or DLL you are developing. The dialog resource takes a font setting that is automatically applied to all controls on the dialog, and dialogs are layed out in terms of dialog units, not pixels, allowing them to automatically scale relative to the font face, and dpi setting on the users system.

If you create a dialog resource, and add it to a simple application, it need look no more complicated than this to get the dialog up onto the screen, and close in response to a click of the OK button.

#include <windows.h>
#include "resource.h"

BOOL CALLBACK MyDialogProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
  switch(uMsg){
  case WM_INITDIALOG:
    return TRUE;
  case WM_COMMAND:
    switch(LOWORD(wParam){
    case IDOK:
      EndDialog(hwnd,wParam);
    }
    return TRUE;
  }
  return FALSE;
}

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hNull,LPCTSTR strCmdLine,int nCmdShow)
{
  return DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,&DialogProc,0l);
}
Chris Becke
  • 34,244
  • 12
  • 79
  • 148
  • 2
    +1. Win32 Dialog boxes are the closest thing to "WinForms" or "Visual" style programming offered by other frameworks and IDEs. Layout the dialog how you want it to look and save it to an RC file within your plain (non-mfc) C++ project in Visual Studio. Compile the RC file with the code Chris provides above. You may also want to #include and call InitCommonControls(); before calling DialogBoxParam above. That way, you can use Trackbar and a few other advanced controls offered by the resource editor. – selbie Jan 02 '11 at 08:08
1

There are definitely better ways. For starters, todays Windows apps are expected to have some sophisticated and cool features. If you plan to implement all these from scratch, you'll be spending a lot of time on your project.

Personally, I use MFC. Other choices are valid, but I would really question not using anything library like that at all. Especially when you are running into the types of problems you are.

Can you place these on a dialog box, or are you doing something unusual in a regular window? Normally, most controls are created by plopping them on a dialog designer. No messing with the font required.

Can you step through what you are trying to do and the problems you are seeing?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 1
    Dialog boxes cannot have either splitter controls or checked list box controls. I really don't want to use MFC if possible due to binary size reasons (the MFC library is ten times as large as my program otherwise is) – Billy ONeal Jan 02 '11 at 06:20
  • Dialog boxes are nothing more than POPUP window with the special window class. Can place any child controls on it. – 9dan Jan 02 '11 at 06:37
  • Well, of course you could add a splitter to a dialog box, although doing so may not makes sense. I'm not at my dev computer and can't easily check how I've done this. Basically, I believe I get a stock GUI font and use setfont on the control. Did you try that? Note that dialogs use dialog units, which adjust for different resolutions. In a plain window, the default will be pixels. This can cause you some extra work. – Jonathan Wood Jan 02 '11 at 06:38
  • @Billy: The splitter control doesn't exist, so you'll have to find one on CodeProject or something. As for the checked list box - just use a list view with checkboxes (LVS_EX_CHECKBOXES). – wj32 Jan 02 '11 at 06:39
  • MFC programmers don't love MFC ;-) It's just efficiency thing. You need C++ wrapper classes on Win32 API, you need readily available implementation of controls. Killer advantage of MFC is that there are lots of reference information and free source codes in the Internet. – 9dan Jan 02 '11 at 06:44
  • @9dan I prefer raw Win32 API. MFC is a wrapper which looks simple but while solving some difficulties in Win32 (controls) it adds other. There was similar C++ library OWL from Borland - same thing. It tries to help but the effect is disputable. And from time to time it is easier to work with Win32 messages instead of artificial classes and object. – i486 Feb 07 '23 at 14:14
1

The Win32 API is aggravatingly hard to use directly, so don't. Most people use some GUI framework. I try to give my personal opinions about more popular choices:

  • WTL. It is bit too thin layer on Win32 API and has not been updated much for a decade. You will face Win32 API itself soon enough if you try to do something above simple stock examples with it. Bonus is that it is extremely quick compared to competition.
  • MFC. Since VS 2008 SP1 MFC is allowing you to make relatively modern-looking GUI. If you can live with its strange coding conventions and heavy macro usage then it is livable. It is supported by VS IDE but far weaker than WinForms are.
  • WxWidgets. Initially felt a bit like MFC that is additionally made portable. It has become lot better after people have started to use it in Python as well.
  • QT. It is most flexible and powerful of them. Unfortunately it has started long time ago so has some legacy strangeness in it. It is slow at places and produces big executables. Best is to use it as well-isolated GUI layer and not to mix heavy usage of STL and boost into that layer.
Öö Tiib
  • 10,809
  • 25
  • 44
  • +1: The Win32 API is exceedingly lacking in the very basics of GUI development. No layout tools, controls that don't know their preferred size, a lack of some common controls (splitters? list-tree view?) - just to name a few - all add up to a shoddy programmer experience. Sadly, MS has added these not to the core API itself, but to layers on top of it, (such as .Net). The only thing I'd add to that list is GTK+. (even though people usually associate it with Linux, Pidgin and GIMP are both excellent Windows GTK apps.) – Thanatos Jan 02 '11 at 06:54
  • 5
    @Thanatos: The Windows API as a whole is not meant to be a GUI toolkit. It's meant to be a relatively low-level application programming interface to the operating system, providing the very basics like file handling, user interface primitives, threading primitives, etc. Of course you will have to use a framework for anything nontrivial. – In silico Jan 02 '11 at 07:34
  • 2
    I was going to use wxWidgets, but it made [HUGE binaries](http://stackoverflow.com/questions/4568364/any-tips-on-reducing-wxwidgets-application-code-size) :( – Billy ONeal Jan 02 '11 at 19:14
  • 1
    Size of binaries is smallest with WTL, then with MFC. QT, WxWidgets and even GTK+ make bigger binaries. – Öö Tiib Jan 03 '11 at 04:21