0

I'm learning the WinAPI for now... 3 days ? and I'm already facing a problem. Indeed, in order to create a DialogBox, we need to write its template in a resource file ( resource.rc for example ).

Here is my resource.rc:

#include "IDs.h"

#include <windows.h>

DIALOG_HELP DIALOG
    CW_USEDEFAULT, CW_USEDEFAULT, 200, 120
        STYLE WS_OVERLAPPEDWINDOW
        CAPTION "Title"
BEGIN
    DEFPUSHBUTTON "Ok", IDOK, 96, 90, 42, 12
    ICON icone1, -1, 60, 55, 32, 32
    LTEXT "Test DialogBox", -1, 100, 58, 100, 10
END

The thing is that #include seems not to be working as it should.

I got resource.rc(6): error RC2108: expected numerical dialog constant when compiling, as if windows.h was not included and that the compiler didn't know what CW_USEDEFAULT is. When I define CW_USEDEFAULT on my own with a simple #define CW_USEDEFAULT 1, it works, no problem, but it would be getting round the problem.

How could I get this to work ? Thanks

Tom Clabault
  • 481
  • 4
  • 18
  • *rc* can not understand *windows.h* need include *winres.h* – RbMm Dec 16 '17 at 14:29
  • #include does not change anything – Tom Clabault Dec 16 '17 at 14:31
  • simply because it undefined here (in sub-includes). but for for resource compiler need anyway include *winres.h* instead *windows.h* – RbMm Dec 16 '17 at 14:32
  • I don't understand. What should I write at the top of my resource.rc ? – Tom Clabault Dec 16 '17 at 14:47
  • for example *#include *. however if you want use `CW_USEDEFAULT` define it itself – RbMm Dec 16 '17 at 14:49
  • *"in order to create a DialogBox, we need to write its template in a resource file"* - This is not strictly required. A resource-definition file is just a shorthand for creating a dialog manually. You *can* create a dialog using C code only. – IInspectable Dec 16 '17 at 15:57
  • 1
    Coordinates in dialog template are specified in _dialog units_ (not pixels) so `CW_USEDEFAULT` doesn't make sense in this case. Additionally they are limited to 16 bits (`short` type) so 0x80000000 is truncated to 0, 0x80000016 to 0x16. – Daniel Sęk Dec 17 '17 at 12:04

2 Answers2

2

error RC2108: expected numerical dialog constant

This means you must use a reasonable number (probably 0). CW_USEDEFAULT (which is (int)0x80000000L) is only meaningful for CreateWindow() / CreateWindowEx() WinAPI functions. As the resource compiler neither supports type casts, nor allows negative dialog coordinates, it results in a syntax error.

Also the style WS_OVERLAPPEDWINDOW should not be used with dialogs. Consider using WS_POPUP-based style instead.

Matt
  • 13,674
  • 1
  • 18
  • 27
  • The resource compiler is capable of expanding preprocessor macros just fine, so you *can* use symbolic constants in resource definition files. This answer doesn't explain the compiler error. – IInspectable Dec 16 '17 at 15:56
  • @IInspectable Fixed. – Matt Dec 16 '17 at 17:27
  • That's still not really an explanation. The true issue here is, that while the resource compiler can expand preprocessor macros, it isn't a C compiler, and doesn't understand the cast expression. Using `WS_POPUP`, for example, is fine. – IInspectable Dec 16 '17 at 17:39
  • @IInspectable Ouch. I seemed that's because (int)0x80000000 is negative (and using negative number would also result in an error here). – Matt Dec 16 '17 at 18:07
1

When you include windows.h from resource compiler it is specially handled by conditional compilation, because resource compiler can't parse more advanced features of C/C++ language.

windows.h fragment:

#if defined(RC_INVOKED) && !defined(NOWINRES)
    #include <winresrc.h>
#else
    #if defined(RC_INVOKED)
        /* Turn off a bunch of stuff to ensure that RC files compile OK. */
        #define NOATOM
        #define NOGDI
        ...
    #endif
    ...
#endif

CW_USEDEFAULT is defined as:

#define CW_USEDEFAULT       ((int)0x80000000)

but it is skipped. When you put this definition directly in resource script, it can't be compiled because resource compiler doesn't handle casts.

You can define

#define CW_USEDEFAULT       0x80000000

but coordinate fields in dialog template are limited to 16 bits (short type), so 0x80000000 doesn't fit and it would be truncated to 0 in resource data (without any error from resource compiler). You should use real coordinates (expessed in dialog units) or reposition your dialog when handling WM_INITDIALOG message.

Daniel Sęk
  • 2,504
  • 1
  • 8
  • 17