Ive been looking a some tuts on MFC. And they all use the Appwizard. Can you make an MFC app with out using the appwizard? Are there any tutorials like this?
Printable View
Ive been looking a some tuts on MFC. And they all use the Appwizard. Can you make an MFC app with out using the appwizard? Are there any tutorials like this?
One question...why? The AppWizard creates so much boilerplate code for you that you'd end up duplicating yourself. Plus you need to get all the comments right as well since the IDE uses them to find where to put the ClassWizard-generated entries.
Cuz then your not learning anything. When ever your earning a new language you should not let something create the code for you. 4 or 5 tuts on the appWizard and i havent learned a thing.
Ok im starting to pick up MFC without the AppWizard. I got a question though. Im making a window using the CFrameWnd class, but when i create it, it displays with the client edge sunken in. Like WS_EX_CLIENTEDGE was specified without me knowing. How do i stop this?
If you don't mind my asking, what tutorials are you using?
None. Im pretty much just trying it on my own and figuring it out as i go.
I tried this a few months ago but gave up since I couldn't see where the whole CWinApp::InitInstance (or whatever) fitted in to the just wanting to create a normal window.
So I gave up and wrote my own classes :) I will admit though, that MFC makes Windows programs piss-easy to make - with very little C++ knowledge I made something pretty complicated. But understanding it was a lot harder...maybe that's one of the provisos of MFC (don't bother understanding it, just hope it does what the docs say).
Well, what ive learned i understand really well. I just cant seem to get any farther now. I cant for the life of me figure how to create a button on a window.
After about 30 deep dives into the MFC source (I would be totally lost if it weren't supplied) I get a feeling what this thing is all about. But don't even try to understand the CString class. ;)
MFC can be really good once you get into it, but the question is if it's worth the effort.
Parksie: basically in InitInstance you create a new CFrameWnd derived object and store it in m_pMainWnd. Then you call it's Create method. And you need a document template. I'd rather let the app wizard to it for me (although by now I think I can do it myself, but it's not worth the time).
Chimp: Get a good book. "Programming Windows with MFC" or something like that. A search on amazon.com will give you a good amount of titles.
To create a button, add a CButton member to your view. In InitialUpdate (a virtual function that you need to override) you call the button's Create method (look it up in the reference). You need to specify WS_VISIBLE as style and supply your view as parent.
To create an MFC app without the wizard, do the following:
create classes derived from
1) CWinApp
This one needs the function
BOOL InitInstance();
where it creates a document template and the main window. The document template is not necessary for a simple window.
2) CDocument
This one stores your data. If you just want an empty window, you don't need it.
3) CFrameWnd
This is your main window's frame. It needs to register a window class in it's constructor.
4) CView
This is your window's client area. You need to override OnDraw at least.
No way am i buying a book on MFC (not worth the money). And all that you said i pretty much figured out on my own this morning. Thanks anyway though.
Try this...
Code:#include <afxwin.h> // For the class library
// Window Class definition
class COurWnd : public CFrameWnd
{
protected:
DECLARE_MESSAGE_MAP()
afx_msg void OnPaint();
};
BEGIN_MESSAGE_MAP(COurWnd, CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
void COurWnd::OnPaint()
{
CPaintDC dc(this);
CRect rcClient;
GetClientRect(rcClient);
int nOldBkMode = dc.SetBkMode(TRANSPARENT);
COLORREF clrOldTextColor = dc.SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
dc.DrawText( "Centered text",
rcClient,
DT_SINGLELINE |
DT_CENTER |
DT_VCENTER );
dc.SetTextColor(clrOldTextColor);
dc.SetBkMode(nOldBkMode);
}
// Application class definition
class COurApp : public CWinApp
{
public:
virtual BOOL InitInstance();
private:
COurWnd* GetMainWindow() { return static_cast<COurWnd*>(m_pMainWnd); }
};
// Function to create an instance of the main window
BOOL COurApp::InitInstance(void)
{
// Construct an object of our C++ window class
m_pMainWnd = new COurWnd;
// Call Create() to create the underlying window
GetMainWindow()->Create(NULL, _T("Our Simple Window"));
GetMainWindow()->SetWindowPos( 0, 500, 500, 500, 500, 0);
// Call ShowWindow() to display the window
GetMainWindow()->ShowWindow(m_nCmdShow);
return TRUE;
}
// Application object definition oat global scope
COurApp AnApplication;
Shawn: yeah, this is even simpler, but not extendable. You should at least keep the frame/view paradigm, it simplifies programming in the long run. Document/View is also not bad because you then get better support from the framework.
Chimp: You usually don't make buttons in the main window. This is the duty of dialogs. But you could for instance try to make a small vector draw app. (This is the example that is followed troughout MY MFC book.) The application should be able to draw ellipses, rectangles and lines, filled and/or stroked, in different colors. It should be able to save and load. This is quite a task and may keep you busy for a while.
Important classes for you:
CDC (CPaintDC and CClientDC)
CDialog
CControlBar and derived (especially CToolBar)
Corned Bee, dont worry im not a win32 begginer. Ive been using the API in asm for a while. Im just trying to learn some mfc.
Why would you want to learn MFC when its being phased out anyway as part of the .NET strategy?
(This must be the first time I've given a reason other than "it's crap" :p)
Im learning mfc because it is used alot. And i need to quickly write an editor that meets my high standards. Every free editor ive seen sucks. Except pfe. Its good but its not being updated.
I'm tempted to come out with the Ford SUV argument here ;)
But as far as quick development goes, yeah definitely. wxWindows is another pretty good one that, to be honest, looks almost identical to MFC :D
Only it's platform independent. w00t.
Two questions. When you create the window and you pass it the CRect, how do you make it use CW_USEDEFAULT? And how do you get the application title string? Like when you use...
It will make the title the applications title.Code:AfxMessageBox(_T("Hello."));
Well, in VC++7 although MFC is supported, everything in .NET seems to be gearing up to Windows Forms.
Perhaps I'm getting confused here, but it looks like they want to merge everything into .NET.
Isnt it like java, with a virtual machine?
For the Win32API theres a help file that has alot of info. Is there something like that for MFC? I know about MSDN but i cant download that to my computer.
For Win32 you should be using the Platform SDK (identical to parts of MSDN, but you can download it). That old .hlp file is chronically outdated.Quote:
Originally posted by ChimpFace9000
For the Win32API theres a help file that has alot of info. Is there something like that for MFC? I know about MSDN but i cant download that to my computer.
Isn't there MFC documentation with Visual C++?
I fear the only documentation of MFC is in MSDN. Usually there is an extract of MSDN (the parts that contain MFC documentation, Win32 SDK documentation etc.) distributed with VC++.
parksie: but what IS .NET? Is it a class library? COM library? API?
In a way, its a class library.
What happens is, everything gets compiled down to the CLR (Common Language Runtime) which is analogous to Java bytecodes. Unlike Java, when you run it it is fully compiled for the host, with proper optimisation (like VC++ would do) so that its just as fast as compiling it directly.
VB.NET has had a fairly striking overhaul, and is almost a different language. The way cool thing you can do with the CLR is you can use VB to derive from a C++ (if you use the managed extensions) class :cool:
So not only is it theoretically platform-independent, its language-independent (as long as they support most of OOP) as well :D
Sounds cool, but I'll stay with less fancy things for a while.
The .NET juggernaut is rolling, release date is only weeks away now.
:) I'll stay with the old things nevertheless. Maybe just because nobody buys VC7 for me and I don't want to do it myself. :)
Is there (will there be) a student's version of it?
Got another question. On my window i have an edit control. This is what i use to create it...
The problem is that it doesnt have a border. So i changed it to this...Code:CEditMain = new CEdit;
CEditMain->Create(WS_VISIBLE | WS_CHILD, CRect(10, 10, 100, 100), this, NULL);
But then it doesnt show up for some reason. Any ideas?Code:CEditMain = new CEdit;
CEditMain->CreateEx(WS_EX_CLIENTEDGE, NULL, NULL, WS_CHILD | WS_VISIBLE, CRect(10, 10, 100, 100), this, NULL, NULL);
besides your strange variable naming (you really shouldn't give a variable a name that looks like a class name), use Create and add WS_BORDER to the styles.
That doesnt work. All it does is add a black border around the edit control.
That didnt work either. Heres the project so you can see exactly what the problem is.
Will try and fix it...Code:--------------------Configuration: Editor - Win32 Debug--------------------
Compiling...
main.cpp
wndmain.cpp
d:\Development\editor\Editor\wndmain.cpp(5) : error C2440: 'static_cast' : cannot convert from 'void (CWndMain::*)(LPCREATESTRUCT)' to 'int (CWnd::*)(LPCREATESTRUCT)'
None of the functions with this name in scope match the target type
Error executing cl.exe.
Editor.exe - 1 error(s), 0 warning(s)
I dont get that error when i compile.
I checked the docs and OnCreate should return int - I've changed it and it compiled fine.
It's crashing on startup all the time so this might take me a little longer...
Okay.
The reason that it won't give a real window is because you should have given it a class name (such as "EDIT"). I haven't worked out why it's crashing though:Code:int CWndMain::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CEditMain.CreateEx(WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE, CRect(10, 10, 100, 100), this, 1000, NULL);
return 0;
}
Yup changing it to "edit" worked. But shouldnt it be something like CEdit->szClsName or something?
Also, it doesnt crash on mine. what windows are you using?
I actually changed it to see if it would avoid it crashing. It's not a pointer anymore, its just on the stack as a normal class member.
I'm using Win98, and it crashes on both VC++5 and VC++7. That is, in debug mode you get the little window because the assertion failed...if you hit "Ignore" it runs fine.
Im using VC6 if it matters.
Any idea about...
But shouldnt it be something like CEdit->szClsName or something?
In your code, yes.
In mine I changed it to a normal variable...
Ok i looked. It doesnt have a member called szClsName.
Ok i got another question. Theres a member function called PreCreateWindow that lets you change some attributes for when it gets created. Is there a function like that for when it registers the class? I want make the hbrBackground = NULL.