Lion King
Lion King

Reputation: 33813

Write the resource-definition in the .h/.cpp file is allowed?

As we know the resource-definition is written in a resource.rc file, but my question Is it possible to write the resource-definition in .h/.cpp file instead .rc file

For example, I want to do something similar to the following:

// .cpp or .h file
#define IDD_MainWindow 1 

IDD_MainWindow DIALOGEX 100, 100, 350, 150
STYLE WS_VISIBLE
BEGIN
CONTROL "", ED_ABOUT, "Static", WS_CHILD | WS_VISIBLE | SS_CENTER, 4, 4, 340, 115
CONTROL "&OK", BTN_OK, "Button", WS_VISIBLE | WS_CHILD, 155, 120, 50, 15
END
// --------
CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MainWindow), NULL, DlgProc);

Is it possible to do that or there is another way to write the resource-definition internally without .rc file?

Upvotes: 0

Views: 88

Answers (1)

Anders
Anders

Reputation: 101756

Yes it is possible. The Microsoft resource compiler has a basic C pre-processor and it defines a macro you can check for.

#define MYICONID 42
#ifdef RC_INVOKED
MYICONID  ICON "myicon.ico"
#else
#include <Windows.h>
int main(...)
{
  LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(MYICONID), ...);
  ...
}
#endif

You can then feed this file into rc.exe and cl.exe. This will of course still generate a .res file that you have to include when linking.

Upvotes: 4

Related Questions