macrosinno-setuppreprocessorpascalscript

Expanding macros to Pascal Code in Inno Setup [Code] section


I was trying to make a macro to avoid duplicating code and comments.

I tried this:

#define GrowOnPage(any Page, any Component) Component.Width := Page.SurfaceWidth; Component.Anchors := [akLeft, akRight, akTop];

However, it can't find the macro. I simply tried calling it like a normal function / procedure:

GrowOnPage(configPage, configTextField);

Maybe I am completly missunderstanding macros here. But the documentation doesn't have any examples.

The reason I am not making a procedure for this, is that the Anchors property isn't available in a common parent class of components such as TCustomEdit.

What would be the correct way to solve this problem?


Solution

  • To evaluate a preprocessor function, you have to use a preprocessor directive. The most usual way to achieve that is using the #emit directive.

    Though note that Inno Setup preprocessor functions are not like C macros. They are really functions. So if you want to use them to produce a part of your script, they have to evaluate to a string. Like this:

    #define GrowOnPage(str Page, str Component) \
        Component + ".Width := " + Page + ".SurfaceWidth; " + \
        Component + ".Anchors := [akLeft, akRight, akTop];"
    

    And use it like this:

    #emit GrowOnPage("configPage", "configTextField")