dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

View Poll Results: What is the first application you all code using MFC ?

Voters
13. You may not vote on this poll
  • Notepad

    1 7.69%
  • IRC Application

    0 0%
  • Web Browser

    0 0%
  • Dialog Based Application

    7 53.85%
  • Others

    5 38.46%
Page 1 of 3 123 LastLast
Results 1 to 15 of 34

Thread: Simple Notepad with MDI and Printing features

  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Smile Simple Notepad with MDI and Printing features

    Hello to all MFC programmer, i pretty new to MFC programming.

    I want to code a simple notepad application but facing some problem.

    Do you all have a good reference in this case ?

    Below is what i have code so far :

    Main.cpp :

    /*

    Lesson 1 - CFrameWnd and CWinApp

    Inheritance Hierachy

    CObject - Base Class
    CCmdTarget - Handling Windows Messages
    - Message Maps(Macro)

    CWnd - data member is m_hWnd
    - member function
    - setTime(ID, WaitSecond before WM_TIMER send
    , NULL(CWnd object handle WM_Time message) )

    X > 1 WM_TIMER in program message queue
    WM_TIMER map to void OnTimer(UINT nID)
    nID is ID number of the timer

    - killTimer(UNIT nID)
    Must remove timer function explicitly
    Destroy Window doesn't remove timer
    - SetMenu()
    - DrawMenuBar()

    CFrameWnd - Provide GUI capabilities
    CWinApp - Provide Application Function
    - InitInstance() method - Application entry point
    - m_pWnd = Pointer to CFrameWnd object

    CMenu - LoadMenu()
    - CreatePopMenu()
    - AppendMenu()
    - AlterMenu()
    - CheckMenuItem()
    - RemoveMenu()
    - DestroyMenu()

    Lesson 2 - Message Map

    1 Class 1 Message Map
    DECLARA_MESSAGE_MAP access if derived from
    CCmdTarget
    BEGIN_MESSAGE_MAP && END_MESSAGE_MAP takes
    two parameters which is class name who implement
    message map and its base class name


    Inside BEGIN_MESSAGE_MAP, a particular handler
    is correspond to a member function inside
    the class implement message map



    Lesson 4 - CMenu

    Two methods to create Menu in MFC
    1. resource option
    2. dynamic menu option


    Lesson 5 - CDialog

    CDialog derived from CWnd
    OnInitDialog - Initialize Dialog
    EndDialog - Close Dialog Box
    Controls - Edit Controls, Static Controls,
    List Boxes, Combo Boxes, Progress Bard
    , List Views and Tree Views

    Lesson 6 - Handler Function

    Function being called via message map

    Two Handler Function

    1. Respond to WM_COMMAND from Menu
    WM_COMMAND take two parameters(ID, Handler Function Name)
    2. Regular Window OS Message
    - Message Handler function from Cwnd

    There are few types of regular Window OS Messages
    - Input Messages(*)
    - General Messages(*)
    - System Messages
    - Non-client area Messages
    - Selected Messages

    Lesson 7 - Child Window

    Dialog Box - Child Window/Pop Up Window
    Controls/view window used in the document-view architecture
    - Fixed Child window

    CView - Provide fixed view child window

    Child or Popup window process messages independently
    from their parent window - send message to
    each others

    Created with WS_CHILD or WS_POPUP MakeChild argument
    WM_User - User defined message map


    */

    #include "Window.h"
    #include "Application.h"



    // ------------------------------------------------

    // Global Variable of the application
    myApp aApp;


    // ----------------Application---------------------
    BOOL myApp::InitInstance()
    {

    frameWnd = new CMFC();
    m_pMainWnd = frameWnd;

    m_pMainWnd->ShowWindow(1);

    // Flash in Bar
    m_pMainWnd->FlashWindow(1);

    return 1;
    }
    // -------------------------------------------------



    // Begin mapping message handler

    BEGIN_MESSAGE_MAP(CMFC, CFrameWnd)

    /*
    A Left Click event handler map
    correspond message to correspond function
    */
    ON_WM_LBUTTONDOWN()

    /*
    A New File Click event handler map
    correspond message to correspond function
    */

    ON_COMMAND(ID_FILE_NEW40027, OnFileNew)

    /*
    A Exit event handler map
    correspond message to correspond function
    */
    ON_COMMAND(ID_FILE_EXIT, ExitApplication)

    /*
    A keyboard shortcut event handler map
    correspond message to correspond functio
    */
    ON_COMMAND(ID_ACCELERATOR40041, ExitApplication)
    ON_COMMAND(ID_ACCELERATOR40071, OnFileNewAccelerator)

    /*
    A WM_Create(Create Toll Bar)
    event handler map
    correspond message to correspond functi
    */
    ON_WM_CREATE()

    /*
    A ToolBar File Open event handler
    map correspond message to
    correspond function
    */
    ON_COMMAND(ID_BUTTON40068, OnToolBarFileOpen)

    END_MESSAGE_MAP()
    // ------------------------------------------------





    // -------------------Window------------------------
    CMFC::CMFC()
    {

    /*
    First parameter is
    MFC defined Window registration class.
    Windows OS creates the window
    from a “registered class,” which
    means it will give
    the window the style,
    background color, cursor,
    and icon that are
    registered with the operating system.

    NULL = default frame

    AfxRegisterWndClass() is to register
    our own class that define the
    */

    Create(NULL, "Window Application"
    , WS_OVERLAPPEDWINDOW
    , rectDefault, 0, MAKEINTRESOURCE(IDR_MENU1) );

    /*
    MAKEINTRESOURCE is a macro that
    convert an integer value to a
    resource type compatible with
    the resource-management functions

    This macro is used when a function
    accept resource parameter
    */


    // Load Accelerator Table into memory
    LoadAccelTable(MAKEINTRESOURCE(IDR_ACCELERATOR1));

    }

    // -------------Left Mouse Clicked------------------
    void CMFC::OnLButtonDown(UINT nFlags, CPoint point)
    {
    CFrameWnd::OnLButtonDown(nFlags, point);

    MessageBox("Left Button Click within the frame Window",
    "Information", MB_ICONINFORMATION);
    }
    // --------------File New Clicked-------------------
    void CMFC::OnFileNew()
    {
    MessageBox("File->New Clicked"
    , "Information", MB_ICONINFORMATION);
    }
    // --------------File Exit Clicked------------------
    void CMFC::ExitApplication()
    {
    /*
    1 - OK Cancel
    2 - Abort, Retry and Ignore
    3 - Yes, No, Cancel
    5 - Retry and Cancel
    6 - Cancel, Try Again and Continue
    */
    MessageBox("Do you want to save the changes to Document1",
    "Information"
    , MB_ICONINFORMATION | MB_YESNOCANCEL);


    DestroyWindow();
    PostQuitMessage(0);
    }
    // ----------Function Create Tool Bar--------------
    int CMFC::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if ( CFrameWnd::OnCreate(lpCreateStruct) == -1)
    {
    return -1;
    }
    else
    {
    if ( ! myBar.CreateEx(this, TBSTYLE_FLAT,
    WS_CHILD | WS_VISIBLE |
    CBRS_TOP | CBRS_GRIPPER |
    CBRS_TOOLTIPS | CBRS_FLYBY |
    CBRS_SIZE_DYNAMIC) )
    {
    MessageBox("Failed to create Tool Bar"
    , "Information", MB_ICONINFORMATION);
    return -1;
    }
    else
    {
    myBar.LoadToolBar(IDR_TOOLBAR1);
    myBar.EnableDocking(CBRS_ALIGN_ANY);
    EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&myBar);

    }
    }
    }
    // -----------Tool Bar File Open Clicked------------
    void CMFC::OnToolBarFileOpen()
    {
    MessageBox("ToolBar Button Clicked"
    , "Information", MB_ICONINFORMATION);
    }
    // -----------

    void CMFC::OnFileNewAccelerator()
    {
    MessageBox("File New Accelerator",
    "Information", MB_ICONINFORMATION);
    }

    Application.h :

    #ifndef _Application_
    #define _Application_

    #include "Window.h"

    class myApp : public CWinApp
    {

    private:
    CMFC *frameWnd;

    private:
    BOOL InitInstance();

    };


    #endif


    Window.h :

    #ifndef _MFC_
    #define _MFC_


    #pragma once


    #include "afxwin.h"
    #include "afxext.h"
    #include "resource.h"


    using namespace std;

    class CMFC : public CFrameWnd
    {

    private:
    CMenu m_WndMainMenu;
    CToolBar myBar;
    public:

    CMFC();

    // Function handle Left Mouse Clicked
    void OnLButtonDown(UINT, CPoint);

    // Function handle File New Menu Bar
    void OnFileNew();

    void OnFileNewAccelerator();

    // Function handle File Exit Menu Bar
    void ExitApplication();

    // Function Create Tool Bar
    int OnCreate(LPCREATESTRUCT);

    // Function handle File open Tool Bar
    void OnToolBarFileOpen();

    // Macro handle message
    DECLARE_MESSAGE_MAP()
    };
    // ------------------------------------------------


    #endif



    I looking for pointer.

    Thanks in advance.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,514

    Re: Simple Notepad with MDI and Printing features

    I have no time to read/understand your code (it is almost unreadable because you ignore using Code tags).

    And the answer on your question is simple:
    use AppWizard to create a SDI application with the CEditView derived class - it will be what you are asking for: the simple "notepad" application
    Victor Nijegorodov

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,100

    Re: Simple Notepad with MDI and Printing features

    The first app that I coded in MFC and would recommend to others is to do the Scribble Tutorial on Msdn.

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Simple Notepad with MDI and Printing features

    If i don't want to use AppWizard to code this program, is there any other method ?

    Please provide some guideliene.

    Thanks in advance.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,514

    Re: Simple Notepad with MDI and Printing features

    I'd recommend you to start with using AppWizard and learning how MFC Framework works.
    And, of course, learn scribble example, how Arjay already recommended you!
    Victor Nijegorodov

  6. #6
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Simple Notepad with MDI and Printing features

    As you said, this is a simple notepad application, i rather prefer manually create the application than with AppWizard.

    What is CEditVIew class and what should i include in my previous code and i didn't find any code tags.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,100

    Re: Simple Notepad with MDI and Printing features

    Code tags are surrounding your code with [ CODE] [/ CODE] (minus the spaces). They allow your code to be displayed in a formatted manner.

    You can find out information on all the MFC classes by searching in Msdn. There are plenty of samples and tutorials that if you spend the time, you can get up to speed in no time.

    Out of curiosity, what do you hope to gain by hand writing MFC without using a Wizard?
    Last edited by Arjay; May 23rd, 2008 at 11:44 PM.

  8. #8
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Simple Notepad with MDI and Printing features

    The reason is i need to learn the document view architecture by hand first and i will use AppWizard for subsequent application that i create.

    Can you provide me some guideline ?

    Thanks for your help.

  9. #9
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Simple Notepad with MDI and Printing features

    The reason is i need to learn the document view architecture by hand first and i will use AppWizard for subsequent application that i create.
    I think it's easier to let the appwizard create the files first, then you totally analyze what happened, and decompose it. Then you are able to create them yourself (that's what I did).

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,100

    Re: Simple Notepad with MDI and Printing features

    Quote Originally Posted by Peter_APIIT
    The reason is i need to learn the document view architecture by hand first and i will use AppWizard for subsequent application that i create.

    Can you provide me some guideline ?

    Thanks for your help.
    I have provided guidelines. My recommendation and other's recommendation has been to do some simple tutorials to learn the basics.

    Whether you choose to hand code is up to you after completing a few tutorials, but at least at that point you'll be able to make an informed decision on what you give up.

  11. #11
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Simple Notepad with MDI and Printing features

    I have create the a simple notepad using AppWizard but i not really understand what is happening there.


    Code:
    // MainFrm.h : interface of the CMainFrame class
    //
    
    
    #pragma once
    
    class CMainFrame : public CFrameWnd
    {
    	
    protected: // create from serialization only
    	CMainFrame();
    	DECLARE_DYNCREATE(CMainFrame)
    
    // Attributes
    public:
    
    // Operations
    public:
    
    // Overrides
    public:
    	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    
    // Implementation
    public:
    	virtual ~CMainFrame();
    #ifdef _DEBUG
    	virtual void AssertValid() const;
    	virtual void Dump(CDumpContext& dc) const;
    #endif
    
    protected:  // control bar embedded members
    	CToolBar    m_wndToolBar;
    
    // Generated message map functions
    protected:
    	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    	DECLARE_MESSAGE_MAP()
    
    // My Function 
    
    public:
    	void ExitApplication();
    };
    
    
    // MainFrm.cpp : implementation of the CMainFrame class
    //
    
    #include "stdafx.h"
    #include "Notepad.h"
    
    #include "MainFrm.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    
    // CMainFrame
    
    IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
    
    BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    	
    	ON_WM_CREATE()
    	ON_COMMAND(ID_APP_EXIT, ExitApplication)
    
    END_MESSAGE_MAP()
    
    
    // CMainFrame construction/destruction
    
    CMainFrame::CMainFrame()
    {
    	// TODO: add member initialization code here
    }
    
    CMainFrame::~CMainFrame()
    {
    }
    
    
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
    		return -1;
    	
    	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
    	{
    		TRACE0("Failed to create toolbar\n");
    		return -1;      // fail to create
    	}
    	
    
    	// TODO: Delete these three lines if you don't want the toolbar to be dockable
    	/*
    		Dockabale means toolbar is able
    		to split from Menu Bar
    	*/
    	
    	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
    	EnableDocking(CBRS_ALIGN_ANY);
    	DockControlBar(&m_wndToolBar);
    
    	return 0;
    }
    
    BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    {
    	if( !CFrameWnd::PreCreateWindow(cs) )
    		return FALSE;
    	// TODO: Modify the Window class or styles here by modifying
    	//  the CREATESTRUCT cs
    
    	cs.style = WS_OVERLAPPED | WS_CAPTION | FWS_ADDTOTITLE
    		 | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_MAXIMIZE | WS_SYSMENU;
    
    	return TRUE;
    }
    
    
    // CMainFrame diagnostics
    
    #ifdef _DEBUG
    void CMainFrame::AssertValid() const
    {
    	CFrameWnd::AssertValid();
    }
    
    void CMainFrame::Dump(CDumpContext& dc) const
    {
    	CFrameWnd::Dump(dc);
    }
    
    #endif //_DEBUG
    
    
    void CMainFrame::ExitApplication()
    {
    	
    	MessageBox(L"Do you want to save the changes to Document1", 
    		L"Information", 
    		MB_ICONINFORMATION | MB_YESNOCANCEL);
    
    	DestroyWindow();
    	PostQuitMessage(0);
    }
    
    // CMainFrame message handlers
    
    
    
    // Notepad.h : main header file for the Notepad application
    //
    #pragma once
    
    #ifndef __AFXWIN_H__
    	#error "include 'stdafx.h' before including this file for PCH"
    #endif
    
    #include "resource.h"       // main symbols
    
    
    // CNotepadApp:
    // See Notepad.cpp for the implementation of this class
    //
    
    class CNotepadApp : public CWinApp
    {
    public:
    	CNotepadApp();
    
    
    // Overrides
    public:
    	virtual BOOL InitInstance();
    
    // Implementation
    	afx_msg void OnAppAbout();
    	DECLARE_MESSAGE_MAP()
    };
    
    extern CNotepadApp theApp;
    
    // Notepad.cpp : Defines the class behaviors for the application.
    //
    
    
    
    #include "stdafx.h"
    #include "Notepad.h"
    #include "MainFrm.h"
    
    #include "NotepadDoc.h"
    #include "NotepadView.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    
    // CNotepadApp
    
    BEGIN_MESSAGE_MAP(CNotepadApp, CWinApp)
    	ON_COMMAND(ID_APP_ABOUT, &CNotepadApp::OnAppAbout)
    	// Standard file based document commands
    	ON_COMMAND(ID_FILE_NEW, &CWinApp::OnFileNew)
    	ON_COMMAND(ID_FILE_OPEN, &CWinApp::OnFileOpen)
    	// Standard print setup command
    	ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinApp::OnFilePrintSetup)
    END_MESSAGE_MAP()
    
    
    // CNotepadApp construction
    
    CNotepadApp::CNotepadApp()
    {
    	// TODO: add construction code here,
    	// Place all significant initialization in InitInstance
    }
    
    
    // The one and only CNotepadApp object
    
    CNotepadApp theApp;
    
    
    // CNotepadApp initialization
    
    BOOL CNotepadApp::InitInstance()
    {
    	// InitCommonControlsEx() is required on Windows XP if an application
    	// manifest specifies use of ComCtl32.dll version 6 or later to enable
    	// visual styles.  Otherwise, any window creation will fail.
    	INITCOMMONCONTROLSEX InitCtrls;
    	InitCtrls.dwSize = sizeof(InitCtrls);
    	// Set this to include all the common control classes you want to use
    	// in your application.
    	InitCtrls.dwICC = ICC_WIN95_CLASSES;
    	InitCommonControlsEx(&InitCtrls);
    
    	CWinApp::InitInstance();
    
    	// Initialize OLE libraries
    	if (!AfxOleInit())
    	{
    		AfxMessageBox(IDP_OLE_INIT_FAILED);
    		return FALSE;
    	}
    	AfxEnableControlContainer();
    	// Standard initialization
    	// If you are not using these features and wish to reduce the size
    	// of your final executable, you should remove from the following
    	// the specific initialization routines you do not need
    	// Change the registry key under which our settings are stored
    	// TODO: You should modify this string to be something appropriate
    	// such as the name of your company or organization
    	SetRegistryKey(_T("Local AppWizard-Generated Applications"));
    	LoadStdProfileSettings(4);  // Load standard INI file options (including MRU)
    	// Register the application's document templates.  Document templates
    	//  serve as the connection between documents, frame windows and views
    	CSingleDocTemplate* pDocTemplate;
    	pDocTemplate = new CSingleDocTemplate(
    		IDR_MAINFRAME,
    		RUNTIME_CLASS(CNotepadDoc),
    		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
    		RUNTIME_CLASS(CNotepadView));
    	if (!pDocTemplate)
    		return FALSE;
    	AddDocTemplate(pDocTemplate);
    
    
    
    	// Parse command line for standard shell commands, DDE, file open
    	CCommandLineInfo cmdInfo;
    	ParseCommandLine(cmdInfo);
    
    
    	// Dispatch commands specified on the command line.  Will return FALSE if
    	// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
    	if (!ProcessShellCommand(cmdInfo))
    		return FALSE;
    
    	// The one and only window has been initialized, so show and update it
    	m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
    	m_pMainWnd->UpdateWindow();
    	// call DragAcceptFiles only if there's a suffix
    	//  In an SDI app, this should occur after ProcessShellCommand
    	return TRUE;
    }
    
    
    
    // CAboutDlg dialog used for App About
    
    class CAboutDlg : public CDialog
    {
    public:
    	CAboutDlg();
    
    // Dialog Data
    	enum { IDD = IDD_ABOUTBOX };
    
    protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    
    // Implementation
    protected:
    	DECLARE_MESSAGE_MAP()
    };
    
    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    }
    
    void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    }
    
    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    END_MESSAGE_MAP()
    
    
    
    // App command to run the dialog
    void CNotepadApp::OnAppAbout()
    {
    	CAboutDlg aboutDlg;
    	
    	// DoModal create the modal based dailog box
    	aboutDlg.DoModal();
    }
    
    
    // CNotepadApp message handlers
    
    /*
    	CDocument
    	
    	CDocument is a class that has no display, 
    	and typically doesn't react much with 
    	the messaging system of windows. 
    	
    	It is used as a class to manage your data/document. 
    	
    	MFC will create the code automatically which handles the event of 
    	File->Save, File->SaveAs, File->Close, and File->Open. 
    	All you need to do is to fill 
    	in the blank functions in the CDocument class.
    
    	
    	CView
    
    	A view is attached to a document and 
    	acts as an intermediary between the 
    	document and the user. 
    	The view renders an image of the document 
    	on the screen or printer and 
    	interprets user input as operations 
    	upon the document.
    
    	A view is a child of a frame window. 
    	More than one view can share a frame window, 
    	as in the case of a splitter window. 
    	The relationship between a view class, 
    	a frame window class, 
    	and a document class is established by a 
    	CDocTemplate object. 
    
    	When the user opens a new window or splits 
    	an existing one, the framework constructs 
    	a new view and attaches it to the document.
    
    	A view can be attached to only one document, 
    	but a document can have multiple views 
    	attached to it at once — 
    	for example, if the document is displayed 
    	in a splitter window or 
    	in multiple child windows 
    	in a multiple document interface (MDI) 
    	application. 
    
    	Your application can support 
    	different types of views for a given 
    	document type. 
    	For example, a word-processing program 
    	might provide both a complete text view 
    	of a document and an outline view 
    	that shows only the section headings. 
    	These different types of views can be 
    	placed in separate frame windows or 
    	in separate panes of a single frame window 
    	if you use a splitter window.
    
    	A view may be responsible for handling 
    	several different types of input, 
    	such as keyboard input, 
    	mouse input or input via drag-and-drop, 
    	as well as commands from menus, 
    	toolbars, or scroll bars. 
    	A view receives commands forwarded by its 
    	frame window. 
    	If the view does not handle a given command,
    	it forwards the command to its associated 
    	document. 
    	Like all command targets, 
    	a view handles messages via a message map.
    
    	The view is responsible for displaying 
    	and modifying the document's data but not 
    	for storing it. 
    	The document provides the view with the necessary details about its data. You can let the view access the document's data members directly, or you can provide member functions in the document class for the view class to call.
    
    	When a document's data changes, the view 
    	responsible for the changes typically 
    	calls the CDocument::UpdateAllViews function 
    	for the document, which notifies all the 
    	other views by calling the OnUpdate 
    	member function for each. 
    
    	The default implementation of OnUpdate 
    	invalidates the view's entire client area. 
    	You can override it to invalidate only 
    	those regions of the client area that map 
    	to the modified portions of the document.
    
    	To use CView, derive a class from it and 
    	implement the OnDraw member function 
    	to perform screen display.
    
    	You can also use OnDraw to perform 
    	printing and print preview. 
    
    	The framework handles the print loop for 
    	printing and previewing your document.
    
    	A view handles scroll-bar messages 
    	with the CWnd::OnHScroll 
    	and CWnd::OnVScroll member functions. 
    	You can implement 
    	scroll-bar message handling
    	(CWnd::OnHScroll and CWnd::OnVScroll) 
    	or 
    	you can use the CView derived 
    	class CScrollView 
    	to handle scrolling for you.
    
    	Besides CScrollView, 
    	the Microsoft Foundation Class Library 
    	provides nine other classes derived from CView: 
    	
    */
    
    // NotepadDoc.h : interface of the CNotepadDoc class
    //
    
    
    #pragma once
    
    
    class CNotepadDoc : public CDocument
    {
    
    	protected: // create from serialization only
    	
    	CNotepadDoc();
    	
    	/* 
    		Enables objects of CObject-derived 
    		classes to be created dynamically 
    		at run time.
    	*/
    	DECLARE_DYNCREATE(CNotepadDoc)
    
    // Attributes
    public:
    
    // Operations
    public:
    
    // Overrides
    public:
    	virtual BOOL OnNewDocument();
    	virtual void Serialize(CArchive& ar);
    
    // Implementation
    public:
    	virtual ~CNotepadDoc();
    #ifdef _DEBUG
    	virtual void AssertValid() const;
    	virtual void Dump(CDumpContext& dc) const;
    #endif
    
    protected:
    
    // Generated message map functions
    protected:
    	DECLARE_MESSAGE_MAP()
    };
    
    
    // NotepadDoc.cpp : implementation of the CNotepadDoc class
    //
    
    
    #include "stdafx.h"
    #include "Notepad.h"
    
    #include "NotepadDoc.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    
    // CNotepadDoc
    
    IMPLEMENT_DYNCREATE(CNotepadDoc, CDocument)
    
    BEGIN_MESSAGE_MAP(CNotepadDoc, CDocument)
    
    END_MESSAGE_MAP()
    
    
    // CNotepadDoc construction/destruction
    
    CNotepadDoc::CNotepadDoc()
    {
    	// TODO: add one-time construction code here
    
    }
    
    CNotepadDoc::~CNotepadDoc()
    {
    }
    
    BOOL CNotepadDoc::OnNewDocument()
    {
    	if (!CDocument::OnNewDocument())
    		return FALSE;
    
    	// TODO: add reinitialization code here
    	// (SDI documents will reuse this document)
    	cout << "****";
    	return TRUE;
    }
    
    
    
    
    // CNotepadDoc serialization
    
    void CNotepadDoc::Serialize(CArchive& ar)
    {
    	if (ar.IsStoring())
    	{
    		// TODO: add storing code here
    	}
    	else
    	{
    		// TODO: add loading code here
    	}
    }
    
    
    // CNotepadDoc diagnostics
    
    #ifdef _DEBUG
    void CNotepadDoc::AssertValid() const
    {
    	CDocument::AssertValid();
    }
    
    void CNotepadDoc::Dump(CDumpContext& dc) const
    {
    	CDocument::Dump(dc);
    }
    #endif //_DEBUG
    
    
    // CNotepadDoc commands
    
    
    // NotepadView.h : interface of the CNotepadView class
    //
    
    
    #pragma once
    
    
    class CNotepadView : public CView
    {
    protected: // create from serialization only
    	CNotepadView();
    	DECLARE_DYNCREATE(CNotepadView)
    
    // Attributes
    public:
    	CNotepadDoc* GetDocument() const;
    
    // Operations
    public:
    
    // Overrides
    public:
    	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
    	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    protected:
    	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
    	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
    	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
    
    // Implementation
    public:
    	virtual ~CNotepadView();
    #ifdef _DEBUG
    	virtual void AssertValid() const;
    	virtual void Dump(CDumpContext& dc) const;
    #endif
    
    protected:
    
    // Generated message map functions
    protected:
    	DECLARE_MESSAGE_MAP()
    };
    
    #ifndef _DEBUG  // debug version in NotepadView.cpp
    inline CNotepadDoc* CNotepadView::GetDocument() const
       { return reinterpret_cast<CNotepadDoc*>(m_pDocument); }
    #endif
    
    
    
    // NotepadView.cpp : implementation of the CNotepadView class
    //
    
    #include "stdafx.h"
    #include "Notepad.h"
    
    #include "NotepadDoc.h"
    #include "NotepadView.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    
    // CNotepadView
    
    IMPLEMENT_DYNCREATE(CNotepadView, CView)
    
    BEGIN_MESSAGE_MAP(CNotepadView, CView)
    	// Standard printing commands
    	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
    	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
    	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
    END_MESSAGE_MAP()
    
    // CNotepadView construction/destruction
    
    CNotepadView::CNotepadView()
    {
    	// TODO: add construction code here
    
    }
    
    CNotepadView::~CNotepadView()
    {
    }
    
    BOOL CNotepadView::PreCreateWindow(CREATESTRUCT& cs)
    {
    	// TODO: Modify the Window class or styles here by modifying
    	//  the CREATESTRUCT cs
    
    	return CView::PreCreateWindow(cs);
    }
    
    // CNotepadView drawing
    
    void CNotepadView::OnDraw(CDC* /*pDC*/)
    {
    	CNotepadDoc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	if (!pDoc)
    		return;
    
    	// TODO: add draw code for native data here
    }
    
    
    // CNotepadView printing
    
    BOOL CNotepadView::OnPreparePrinting(CPrintInfo* pInfo)
    {
    	// default preparation
    	return DoPreparePrinting(pInfo);
    }
    
    void CNotepadView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    	// TODO: add extra initialization before printing
    }
    
    void CNotepadView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    	// TODO: add cleanup after printing
    }
    
    
    // CNotepadView diagnostics
    
    #ifdef _DEBUG
    void CNotepadView::AssertValid() const
    {
    	CView::AssertValid();
    }
    
    void CNotepadView::Dump(CDumpContext& dc) const
    {
    	CView::Dump(dc);
    }
    
    CNotepadDoc* CNotepadView::GetDocument() const // non-debug version is inline
    {
    	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNotepadDoc)));
    	return (CNotepadDoc*)m_pDocument;
    }
    #endif //_DEBUG
    
    
    // CNotepadView message handlers

    Thanks for your explanation.

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,100

    Re: Simple Notepad with MDI and Printing features

    When you create a new MFC project, a readme.txt file is created that gives you a basic overview of the classes and files that were created.

    You have included this file in the text posted above (scroll about halfway and you'll see this).

    Read through this file and afterwards ask specific questions on the parts that you don't understand.

  13. #13
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Simple Notepad with MDI and Printing features

    I have read this readMe.txt and also my program.

    I wonder why we need to dynamic create a CMainFrame and other classes as well since all application has main frame.

    Code:
    DECLARE_DYNCREATE(CMainFrame)
    Second question is what these four functions does ?

    Code:
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    
    virtual void DoDataExchange(CDataExchange* pDX);
    
    
    BOOL CNotepadApp::InitInstance()
    {
    	// InitCommonControlsEx() is required on Windows XP if an application
    	// manifest specifies use of ComCtl32.dll version 6 or later to enable
    	// visual styles.  Otherwise, any window creation will fail.
    	INITCOMMONCONTROLSEX InitCtrls;
    	InitCtrls.dwSize = sizeof(InitCtrls);
    	// Set this to include all the common control classes you want to use
    	// in your application.
    	InitCtrls.dwICC = ICC_WIN95_CLASSES;
    	InitCommonControlsEx(&InitCtrls);
    
    	CWinApp::InitInstance();
    
    	// Initialize OLE libraries
    	if (!AfxOleInit())
    	{
    		AfxMessageBox(IDP_OLE_INIT_FAILED);
    		return FALSE;
    	}
    	AfxEnableControlContainer();
    	// Standard initialization
    	// If you are not using these features and wish to reduce the size
    	// of your final executable, you should remove from the following
    	// the specific initialization routines you do not need
    	// Change the registry key under which our settings are stored
    	// TODO: You should modify this string to be something appropriate
    	// such as the name of your company or organization
    	SetRegistryKey(_T("Local AppWizard-Generated Applications"));
    	LoadStdProfileSettings(4);  // Load standard INI file options (including MRU)
    	// Register the application's document templates.  Document templates
    	//  serve as the connection between documents, frame windows and views
    	CSingleDocTemplate* pDocTemplate;
    	pDocTemplate = new CSingleDocTemplate(
    		IDR_MAINFRAME,
    		RUNTIME_CLASS(CNotepadDoc),
    		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
    		RUNTIME_CLASS(CNotepadView));
    	if (!pDocTemplate)
    		return FALSE;
    	AddDocTemplate(pDocTemplate);
    
    
    
    	// Parse command line for standard shell commands, DDE, file open
    	CCommandLineInfo cmdInfo;
    	ParseCommandLine(cmdInfo);
    
    
    	// Dispatch commands specified on the command line.  Will return FALSE if
    	// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
    	if (!ProcessShellCommand(cmdInfo))
    		return FALSE;
    
    	// The one and only window has been initialized, so show and update it
    	m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
    	m_pMainWnd->UpdateWindow();
    	// call DragAcceptFiles only if there's a suffix
    	//  In an SDI app, this should occur after ProcessShellCommand
    	return TRUE;
    }
    Thanks for your explanation.

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,514

    Re: Simple Notepad with MDI and Printing features

    Victor Nijegorodov

  15. #15
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Simple Notepad with MDI and Printing features

    Thanks for the reply.

    I understand the dialog data exchange process.

    Below is my understanding :

    UpdateDate() -> CAboutDlg:oDataExchange(CData *Exchange) -> CDialog:oDataExchange(CData *Exchange) -> DDX Function(Control).

    What is the difference between PreCreateWindow, OnCreate and Create ?

    Why got so many unknown statemnet inside InitInstance() since i only called create function without AppWizard ?

    Code:
    BOOL CNotepadApp::InitInstance()
    {
    	// InitCommonControlsEx() is required on Windows XP if an application
    	// manifest specifies use of ComCtl32.dll version 6 or later to enable
    	// visual styles.  Otherwise, any window creation will fail.
    	INITCOMMONCONTROLSEX InitCtrls;
    	InitCtrls.dwSize = sizeof(InitCtrls);
    	// Set this to include all the common control classes you want to use
    	// in your application.
    	InitCtrls.dwICC = ICC_WIN95_CLASSES;
    	InitCommonControlsEx(&InitCtrls);
    
    	CWinApp::InitInstance();
    
    	// Initialize OLE libraries
    	if (!AfxOleInit())
    	{
    		AfxMessageBox(IDP_OLE_INIT_FAILED);
    		return FALSE;
    	}
    	AfxEnableControlContainer();
    	// Standard initialization
    	// If you are not using these features and wish to reduce the size
    	// of your final executable, you should remove from the following
    	// the specific initialization routines you do not need
    	// Change the registry key under which our settings are stored
    	// TODO: You should modify this string to be something appropriate
    	// such as the name of your company or organization
    	SetRegistryKey(_T("Local AppWizard-Generated Applications"));
    	LoadStdProfileSettings(4);  // Load standard INI file options (including MRU)
    	// Register the application's document templates.  Document templates
    	//  serve as the connection between documents, frame windows and views
    	
    	
    	CSingleDocTemplate* pDocTemplate;
    	pDocTemplate = new CSingleDocTemplate(
    		IDR_MAINFRAME,
    		RUNTIME_CLASS(CNotepadDoc),
    		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
    		RUNTIME_CLASS(CNotepadView));
    	
    	if (!pDocTemplate)
    		return FALSE;
    	
    	AddDocTemplate(pDocTemplate);
    
    
    	// Parse command line for standard shell commands, DDE, file open
    	CCommandLineInfo cmdInfo;
    	ParseCommandLine(cmdInfo);
    
    
    	// Dispatch commands specified on the command line.  Will return FALSE if
    	// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
    	if (!ProcessShellCommand(cmdInfo))
    		return FALSE;
    
    	// The one and only window has been initialized, so show and update it
    	m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
    	m_pMainWnd->UpdateWindow();
    	// call DragAcceptFiles only if there's a suffix
    	//  In an SDI app, this should occur after ProcessShellCommand
    	return TRUE;
    }
    I don't understand some of the code. How to accept drag file to CDocument and display through CView ?


    Thanks for your help.

Page 1 of 3 123 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)