How to save form to a DFM file at runtime in Delphi

It's very simple. Just drop TFormSaver component onto your form, set DataFormat property and call the SaveToDFM method.

cmpFormSaver.SaveToDFM(YourFileName);

Also you can save form to stream with SaveToStream method.

cmpFormSaver.SaveToStream(YourStreamObject);
See also
Form Saver
Components for Delphi / C++ Builder
Howtos for Programmers