Re: pointer to a mfc form
From: Jerry Coffin (jcoffin_at_taeus.us)
Date: 04/30/04
- Next message: Molnar Zoltan: "specialized call ofa template method"
- Previous message: Rudy Ray Moore: "Re: Visual Studio 6 or .NET for C++?"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 30 Apr 2004 11:41:47 -0600
In article <#aIgPtaLEHA.3904@TK2MSFTNGP09.phx.gbl>,
rogerh40NoSpam@mindspring.com says...
[ ... ]
> I am writing a program using mfc because I want a windows interface. I
> am using a generic C++ class, but I want to add data to a listbox in a
> edit box on a mfc formview class from my generic class. I know I need
> to create a pointer to the form, but I can't seem to figure out how to
> do this without getting tons of compiler errors. I know this should be
> very simple so I was wondering if someone who knows how to do this could
> supply me with some sample code that I could look over on how to do
> this? Just something simple, it does not have to be very fancy. If I
> can't figure out how to do it this time, I guess I will have to go back
> to borland builder C++; I have no problem in builder doing this, but I
> wanted to do it in visual c++.
MFC is really intended to work somewhat differently than you seem to
be thinking (really, almost the opposite of how it sounds like you're
thinking).
At least if I understand you correctly, your current "generic class"
is basically a container for some data, and you want to display that
data in a list box in a form (or perhaps in both an edit box and a
list box in the form?)
In any case, to do that you do NOT want to pass a pointer to the view
class(es) to the generic class. Rather, you want to make the generic
class part of the Document class. The View class contains an OnDraw
member function that's responsible for updating the actual display of
the data.
One of the first things the default OnDraw does (in fact, nearly ALL
the default OnDraw does) is retrieve a pointer to the Document object
with which it's associated.
Then, you add code to the OnDraw to display that data in whatever way
is appropriate. In the case of a FormView, you'll normally want to
associate member variables with your control(s), and use them to
update the display.
I've created a small demonstration of this and posted it at:
http://www.coders-central.org/disp.zip
Most of it is a stock AppWizard FormView-based application. The
parts I've added are:
1) put an edit control in the dialog and associated it with a CString
member variable.
2) added a string to the document.
3) added a handler for the content of the edit control having
changed. This handler makes no attempt at being very smart --
anytime the data changes, it just copies the entire contents of the
edit control into the Document's string.
4) added an OnDraw that's equally stupid -- it just copies the whole
string into the current view's control.
Now, if you compile and run this, it'll create a window with an edit
control in its top half. If you go to the Window menu, you can
create another window.
You can now type into one window, and your modifications will
immediately show up in the other -- and the same will work with an
arbitrary number of windows too. To support this, the generic object
holding the data in the view knows precisely NOTHING about any of the
views, and never has to retrieve a pointer to any view.
--
Later,
Jerry.
The universe is a figment of its own imagination.
- Next message: Molnar Zoltan: "specialized call ofa template method"
- Previous message: Rudy Ray Moore: "Re: Visual Studio 6 or .NET for C++?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|