I am using Visual Studio 2008, and i am working with a single document application, it is CView Based, and i want to know how i can in addition to the view, that i will be drawing on, how i can add controls, like CButton and CListBox. do i have to make a toolbar, or can it be added to the view? a simple example would be nice

Recommended Answers

All 10 Replies

You can add controls, but it might be easier to use CFormView which is derived from CView but acts like CDialog.

To add a button to your CView, just create an instance of CButton in the CView's class, then when initializing CView class call the button's Create() method. Of course before calling Create() you will have to create a CRect (or just RECT) which you pass to Create(). I don't have an example to show you but its not all that difficult to do. It may take a few tries to get the CRect coordinates just right because you can't do it visually like you do with CDialog or CFormView.

thanka, the same for a listbox? oh, that brings another question, can list box contents be changed during runtime?

yes, and yes. If you want to know how to change the listbox contents, just read the methods that are available and you will find it (I don't recall off the top of my head.)

ok, and got a link to how to change it at runtime?

search www.microsoft.com for CListBox. You can find everything you need there, and for all the windows classes. You should use that for all your MFC questions, if you can't find it there then post question here. There is a lot of things though that MSDN doesn't tell you.

You should also be able to just press <F1> while in the compiler's IDE and it will take you right to the search box. I'm being a little bit evasive because you need to learn how to look it up yourself.

ok, i made an attempt to add a button to the CView. but it isn't working, no errors, just not showing up. i think i am passing something incorrectly, or i'm trying to do this in the wrong place.
button1 is declared as public in the class declaration, as is button1Rect (neither are pointers) so i tried this:

BOOL CGDI1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	button1Rect.SetRect(250,50,400,100);
	button1.Create(_T("HI"),WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,button1Rect,this,1);

	return CView::PreCreateWindow(cs);
}

is this the wrong method to try this in, or should i not be passing "this"? sorry for my ignorance on the subject, i'm new to MFC

but it isn't working, no errors, just not showing up.

You are not checking the return value of Create(...), so you are unaware of the error.

Override the view's OnInitialUpdate() method and create the button there. At that point the view is readily constructed, hence 'this' can be passed to Create(...) as a valid parent of the button.

ok, i will try that. what will the boolean tell, if it worked or not, if so how would that show the error?

EDIT: i overrided the method you recommended and it worked, thanks

what will the boolean tell, if it worked or not,

If it is TRUE, then it worked, else not (i.e. it is FALSE).

MFC provides the VERIFY() macro, which you might find useful, i.e.

VERIFY(button1.Create(_T("HI"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,button1Rect,this,1));

If the 'verified' funtion fails (returns NULL or FALSE), you'll be seeing a Debug Assertion Failure dialog box with an option to debug the program, so you can quickly figure out where things are going wrong. In Release builds, VERIFY() does nothing.

About generic Windows error codes, here is a brief introduction.

thanks for the help, kinda forgot about this thread for a while, solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.