Link to home
Start Free TrialLog in
Avatar of trwest
trwest

asked on

Dialog resource to MFC code utility

(rephrase) Does anyone know of a utility which can convert text from a .RC file (defining a dialog and its controls) to the equivalent MFC code?  

In my application I want to create a dialog on-the-fly (dynamically?), with a number of controls, instead of placing it in the resource file.  However, to actually design the dialog I would like to use Developer Studio, where I can size/position everything visually.  

Developer Studio creates a .rc file.  I would like something that can read that .rc file, and dump out the corresponding MFC code that would create the same dialog.

eg.  Translate (from the .rc)
     PUSHBUTTON "Cancel",IDCANCEL,205,75,50,14

     to
     button.Create( "Cancel", ... ) ;

     OR to a DLGTEMPLATE structure

I would then stitch this code into my application, and the dialog would be created while the application is running.

Hope that makes sense.
Avatar of naveenkohli
naveenkohli

Hi Buddy,
I have a question? What do you mean by creating at run time? Cause this is bit confusing.

Avatar of trwest

ASKER

Edited text of question
If U whant to use DevStudio to position and size control. Whats the meaning of translating that to code instead of just creating the dialog from resource.

Do you whant to modify the dialog controls at runtime ???

Creating a dialog from a resource definition is actually the same 'dynamic creation' of the dialog, as you would get by tranlating the resource into code.

If you want a DLGTEMPLATE structure, just get it: call LoadResource() on the dialog resource. Then, call LockResource() and you'll have a pointer to the DLGTEMPLATE structure.

.B ekiM

Avatar of trwest

ASKER

No, no no no NO!
I do NOT want to include the dialog in a resource file.
Otherwise, I would just do it!
The only reason I want to use Developer Studio is so I can layout the dialog's controls visually (instead of figuring out x,y's in dialog units).
I want to create the dialog RUN-TIME.  That means:  beforehand, the application (including the resource file) does not know anything about the dialog.  It is not the same as "creating a dialog from a resource definition", because I don't want the resource definition there in the first place.

So, to reiterate, does anyone know of a utility to convert a dialog from a resource file into MFC run-time code.

I'm beginning to think not.

No reason to get testy: I was just answering the question as you asked it. One way to translate a RC file to a DLGTEMPALTE structure is using the resource compiler and the APIs I mentioned in my response.

Anyhow, I don't know of any such utility. But it seems like you should be able to write one in an afternoon or two.

But, now I'm curious: hardcoding calls to create window to build your dialog is going to be slow at runtime. And it's going to result in a lot of code. You'll find that the same dialog in a resource is much smaller and creates faster.  Why would you want to not use resources?

.B ekiM


I agree with .B ekiM .. there seems to be little point in creating a dialog in code (from a dialog template) when it can be created from a dialog resource.  I could imagine there being some use in that if the dialog template were being created modified before the dialog is created for it .. but you even say that just generating the Create calls would do.

This all seems a bit odd.. perhaps if you tell us WHAT you are trying to achieve from a broader perspective, then we can offer a better solution.

Regarding the utility, it shouldn't be hard to write a little program that prompts for a dialog resource ID and a EXE or DLL.  It then loads the resource from that DLL or EXE, loads and locks the dialog resource to get the dialog template, and save the template data out to a file.  Then your other program can read in the generated dialog template and create a dialog from there.

But what is the point???

Perhaps we're missing something here?


Avatar of trwest

ASKER

I don't want a better solution, I just wanted to know if there was such a utility!! :-)

I have since realized, by lack of response to the question, that such a utility either does not exist, or at least is not known of.  In the mean time, I have created a class which will do the job for me.  

I know there are alternative ways (and probably better ways) to accomplish dialog creation, but the fact remains that this is the way I wanted to do it.  I want ONE portable dialog and it seemed overkill to create and distribute a .DLL for it.

By posting a quick question to a broad group of people I was hoping someone would save me the 1/2 day by saying "oh yeah, here's one".  But I've spent more time trying to justify my question than I would've spent just writing the utility! :-)

Thanks, anyways.

ASKER CERTIFIED SOLUTION
Avatar of RONSLOW
RONSLOW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trwest

ASKER

The Source Wizard is C and not C++ but it is the kind of thing I was looking for.  Except I don't need it anymore.

And what on earth am I trying to do?
Okay <deep breath> we already have a static library that we share across several applications.  Instead of spending the time to turn it into a .DLL (into which I could embed resources), I wanted to add my dialog to the static lib.  The only way to do that is to create the dialog in code.  No resource file at all.  And we didn't want to cut and paste code from the .rc file, nor did we want to go through the "multiple resource files in your project" hell.  We were _hoping_ for the easy approach.

I took a quick look around the 'net to see if there was a utility to help me out (if I found one, it _would_ be easy).  But alas, no such beast.  Eventually I found a sample (from Developer Studio, no less) that creates dialogs by setting up a DLGTEMPLATE structure and everything in it.  I turned that into a class, and it's case closed.

But thanks for finding that link for me.  Who knows, it may yet come in handy -- especially if he converts it to MFC! :-)

You can simply use a #include to include from another resource file (you add it to the list of include file for the resource.  It would take very little effort to set this up (much less than what you have spent on this exercise).  You can even use the .rc2 file to do this and have it #include your common dialog resource.