Click here to Skip to main content
15,921,382 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionStoring files in a dll, or somewhere like that... Pin
Anthony Mushrow23-Mar-07 14:05
professionalAnthony Mushrow23-Mar-07 14:05 
AnswerRe: Storing files in a dll, or somewhere like that... Pin
DLChambers23-Mar-07 15:04
DLChambers23-Mar-07 15:04 
Questionserial.Open(_T("COM1")); in CSerial class Pin
m-hwang23-Mar-07 14:01
m-hwang23-Mar-07 14:01 
AnswerRe: serial.Open(_T("COM1")); in CSerial class Pin
Cedric Moonen23-Mar-07 22:41
Cedric Moonen23-Mar-07 22:41 
GeneralRe: serial.Open(_T("COM1")); in CSerial class Pin
m-hwang24-Mar-07 5:50
m-hwang24-Mar-07 5:50 
AnswerRe: serial.Open(_T("COM1")); in CSerial class Pin
Gary R. Wheeler24-Mar-07 2:47
Gary R. Wheeler24-Mar-07 2:47 
QuestionEject one USB device on multiple USB devices Pin
fuxiangwu23-Mar-07 12:37
fuxiangwu23-Mar-07 12:37 
QuestionNewbie learning MFC, confused about Document/View Pin
maxmaven23-Mar-07 10:26
maxmaven23-Mar-07 10:26 
Hi,

I'm learning MFC, with prior experience in java, so I have a little bit of understanding about the general concept of a document and a view (I hope).

I'm trying to figure out some missing knowledge I don't have about the concept of Document/View in MFC when using a splitter window.

I have two questions. The first question, I will explain something I did and ask your advice. I think I did it the "right" way, but I'm not 100%. By the right way, I am meaning, the way that makes the most sense and uses MFC as properly as possible. I know there are lots of ways to do the same thing, I am just hoping I did it the most appropriate way.

My second question points out that I am missing something major. I know I did it the wrong way. Could you point me towards the proper way?

All this was done in Visual Studio 2005.

Question 1:

I have a MainFrame and the App Wizard added a window splitter for me. I made the splitter static with one row, two columns. The left pane has a CTreeView subclass. The right pane displays one of two CFormViews, call them info_pane_1 and info_pane_2 (imagine a project settings kind of situation). There are two items in the left CTreeView pane, clicking on the first item causes info_pane_1 to appear in the right pane. Similarly, clicking on the second item causes info_pane_2 to appear in the right pane.

I didn't implement any of the fancy splitter stuff myself, I used "CUsefulSplitterWnd" on this website. The only code I needed to add was a message handler, OnTvnSelchanged(...) to my CTreeView subclass. In this function, I figure out which item was clicked on in the tree view, then I get a pointer to the parent frame (MainFrame). I call a new member I added to MainFrm.cpp called ChangeDialog(int which). The variable "which" is either 1 for info_pane_1 or 2 for info_pane_2. The function ChangeDialog(...) in MainFrm.cpp is just:

(paraphrasing)

switch(which)

case 1:
m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(info_pane_1),...)


or

case 2:
m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(info_pane_2)


Everything works exactly as I had hoped; Clicking in the tree (in the left pane) dynamically changes what appears in the right pane.

Did I do this the right way?

Question 2:

Here is where I'm confused about how to properly use the Doc/View architecture. What I need to do is simple: The user clicks on the various checkboxes and radio-boxes in info_pane_1, and I have an "Apply" button. When the user clicks "Apply", the state of the controls in info_pane_1 need to be saved. I want to save all this information into the CDocument so that I can use the serialize functions to save all the settings for all the controls. The first thing I did, which I think is correct, is to add control member variables to my classes info_pane_1 and _2. When the user clicks "Apply", I just call UpdateData. So it looks like this:

info_pane_1::OnBnClickedApplybutton()
{
   UpdateData(TRUE);
}


This works correctly.

However, now I notice an issue. Whenever I call replaceview in the UsefulSplitterWnd class, it gets rid of the old view that was in the right pane of the splitter (including the class!) (the class is deleted?) and makes a brand new one to put into the right pane. I know this for sure because I can just put a breakpoint in the constructor for whatever class is put into the right pane. So, I've lost all the control settings the user has chosen. But I think this is ok and kind of the right behavior; I should be storing this data into the CDocument not really the pane. So what I do is the following:

First I add a "BOOL checkboxstate" to the CDocument subclass that the App Wizard made for me. Then I change OnBnClickedApply:

void info_pane_1::OnBnClickedApplybutton()
{
	// TODO: Add your control notification handler code here
	
	// Retrieve the state of all the controls in the dialog and
	// transfer this information to the member variables we added. 
	UpdateData(TRUE);

	// Now tell document
	CCodeBuilderDoc *p_Doc = (CCodeBuilderDoc *) GetDocument();
	ASSERT_VALID(p_Doc);
	p_Doc->state_InsertionSortCheck = state_InsertionSortCheck;
}


Maybe this is not the most correct way to do it? It seems like I've missed something.

In any case, now that I've saved the information into the CDocument, I now need to get it when the new view is inserted into the right-hand splitter pane by UsefulSplitterWnd. So what I did was:

void info_pane_1::OnDraw(CDC *pDC)
{
	CCodeBuilderDoc *p_Doc = (CCodeBuilderDoc *) GetDocument();
	ASSERT_VALID(p_Doc);
	state_InsertionSortCheck = p_Doc->state_InsertionSortCheck;

	UpdateData(FALSE);
}


This seems to me to be obviously wrong.

I am pretty sure the following things are either broken or confused as a result:
1. I bet CDocument::UpdateAllViews doesn't work.
2. I don't understand why my project/solution has a CMyAppView descended from CView in MyAppView.cpp/.h. I haven't done anything to this class. Is it being used?

I think my question 2 is, how should I have done this? I've clearly done it the wrong way.

Thanks gurus for your comments and help. I am new to MFC, only about 5 days into learning it/working with it. I think I've learned a lot, but I think I don't have a good understanding of what to do with this Document/View architecture.

Thank you so much for any help. I hope to type up a little tutorial on this, as I've seen a number of questions on split windows and Doc/View. I'll add your credits to the tutorial. I hope that perhaps it will be accepted as a tutorial on codeproject. Smile | :)
AnswerRe: Newbie learning MFC, confused about Document/View Pin
Gary R. Wheeler24-Mar-07 3:37
Gary R. Wheeler24-Mar-07 3:37 
GeneralRe: Newbie learning MFC, confused about Document/View Pin
maxmaven24-Mar-07 9:37
maxmaven24-Mar-07 9:37 
Questionalways load windows default color scheme in a dialog??? Pin
tiflo23-Mar-07 9:28
tiflo23-Mar-07 9:28 
QuestionContext Menu and displaying menu on screen Pin
flippydeflippydebop23-Mar-07 8:17
flippydeflippydebop23-Mar-07 8:17 
AnswerRe: Context Menu and displaying menu on screen Pin
flippydeflippydebop23-Mar-07 8:39
flippydeflippydebop23-Mar-07 8:39 
Questionany fn to know the memoru allocated by SafeArrayCreate() Pin
GANsJob23-Mar-07 7:24
GANsJob23-Mar-07 7:24 
AnswerRe: any fn to know the memoru allocated by SafeArrayCreate() Pin
prasad_som23-Mar-07 20:44
prasad_som23-Mar-07 20:44 
Questioncan't retrieve the string from string table using loadstring() Pin
GANsJob23-Mar-07 7:16
GANsJob23-Mar-07 7:16 
AnswerRe: can't retrieve the string from string table using loadstring() Pin
perle123-Mar-07 14:22
perle123-Mar-07 14:22 
Questionconfusing problem Pin
swatgodjr23-Mar-07 6:58
swatgodjr23-Mar-07 6:58 
AnswerRe: confusing problem Pin
Christian Graus23-Mar-07 7:10
protectorChristian Graus23-Mar-07 7:10 
GeneralRe: confusing problem Pin
swatgodjr23-Mar-07 7:42
swatgodjr23-Mar-07 7:42 
AnswerRe: confusing problem Pin
Phil J Pearson23-Mar-07 7:38
Phil J Pearson23-Mar-07 7:38 
AnswerRe: confusing problem Pin
Sameerkumar Namdeo23-Mar-07 22:22
Sameerkumar Namdeo23-Mar-07 22:22 
GeneralRe: confusing problem Pin
swatgodjr24-Mar-07 2:52
swatgodjr24-Mar-07 2:52 
GeneralRe: confusing problem Pin
Sameerkumar Namdeo25-Mar-07 16:48
Sameerkumar Namdeo25-Mar-07 16:48 
QuestionInitializtion Code for Dialog Boxes Pin
gunner_uk200023-Mar-07 4:48
gunner_uk200023-Mar-07 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.