Page 1 of 2 12 LastLast
Results 1 to 40 of 46

Thread: Learning MFC

  1. #1
    ChimpFace9000
    Guest

    Post Learning MFC

    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?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    ChimpFace9000
    Guest
    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.

  4. #4
    ChimpFace9000
    Guest
    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?

  5. #5
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    If you don't mind my asking, what tutorials are you using?
    Alcohol & calculus don't mix.
    Never drink & derive.

  6. #6
    ChimpFace9000
    Guest
    None. Im pretty much just trying it on my own and figuring it out as i go.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    ChimpFace9000
    Guest
    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.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    ChimpFace9000
    Guest
    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.

  11. #11
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    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;
    Please rate my post.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    ChimpFace9000
    Guest
    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.

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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" )
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15
    ChimpFace9000
    Guest
    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.

  16. #16
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

    Only it's platform independent. w00t.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  17. #17
    ChimpFace9000
    Guest
    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...

    Code:
    AfxMessageBox(_T("Hello."));
    It will make the title the applications title.

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  19. #19
    ChimpFace9000
    Guest
    Isnt it like java, with a virtual machine?

  20. #20
    ChimpFace9000
    Guest
    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.

  21. #21
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    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.

    Isn't there MFC documentation with Visual C++?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  22. #22
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  23. #23
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

    So not only is it theoretically platform-independent, its language-independent (as long as they support most of OOP) as well
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  24. #24
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Sounds cool, but I'll stay with less fancy things for a while.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  25. #25
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The .NET juggernaut is rolling, release date is only weeks away now.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  26. #26
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  27. #27
    ChimpFace9000
    Guest
    Got another question. On my window i have an edit control. This is what i use to create it...

    Code:
    CEditMain = new CEdit;
    CEditMain->Create(WS_VISIBLE | WS_CHILD, CRect(10, 10, 100, 100), this, NULL);
    The problem is that it doesnt have a border. So i changed it to this...

    Code:
    CEditMain = new CEdit;
    CEditMain->CreateEx(WS_EX_CLIENTEDGE, NULL, NULL, WS_CHILD | WS_VISIBLE, CRect(10, 10, 100, 100), this, NULL, NULL);
    But then it doesnt show up for some reason. Any ideas?

  28. #28
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  29. #29
    ChimpFace9000
    Guest
    That doesnt work. All it does is add a black border around the edit control.

  30. #30
    ChimpFace9000
    Guest
    That didnt work either. Heres the project so you can see exactly what the problem is.
    Attached Files Attached Files

  31. #31
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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)
    Will try and fix it...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  32. #32
    ChimpFace9000
    Guest
    I dont get that error when i compile.

  33. #33
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  34. #34
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  35. #35
    ChimpFace9000
    Guest
    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?

  36. #36
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  37. #37
    ChimpFace9000
    Guest
    Im using VC6 if it matters.

    Any idea about...
    But shouldnt it be something like CEdit->szClsName or something?

  38. #38
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    In your code, yes.

    In mine I changed it to a normal variable...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  39. #39
    ChimpFace9000
    Guest
    Ok i looked. It doesnt have a member called szClsName.

  40. #40
    ChimpFace9000
    Guest
    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width