MFC analysis (1) In the document view structure, the default command processing

zhaozj2021-02-08  217

In the document view structure, the default command processing

FMD (http://www.fmdstudio.net)

In the document view structure, the default command processing

In the framework built in the document view structure, many command IDs have default command processing. Many features are completed by them, but these features are not "visible" in the program, not convenient to understand the program.

The following summary of common default processing and its processes

You can reload these functions to achieve specific features when needed

1.ID_FILE_NEW

2.ID_FILE_OPEN

3.ID_FILE_SAVE

4.ID_FILE_SAVE_AS

5.ID_FILE_SAVE_COPY_AS

6.ID_FILE_CLOSE

7.ID_FILE_UPDATE

8.ID_FILE_PRINT_SETUP

9.ID_FILE_PRINT

10.ID_FILE_PRINT_PREVIEW

11. Default editing control ID

12.id_window_new

13.id_window_arrange

14.id_window_cascade

15.id_window_tile_horz

16.id_window_tile_vert

17.id_window_split

18.ID_APP_ABOUT

19.ID_APP_EXIT

20.id_help_index

21.ID_HELP_USING

22.id_context_help

23.id_help

24.ID_DEFAULT_HELP

25.id_next_pane

26.ID_PREV_PANE

27.id_ole_insert_new

28.id_ole_edit_links

29.ID_view_toolbar

30.ID_View_status_bar

1.ID_FILE_NEW

CWINAPP :: Onfilenew

Call m_pdocmanager-> onfilenew ()

Void cdocmanager :: OnfileNew ()

{

IF (m_templatelist.isempty ())

{

TRACE0 ("Error: No Document Templates Registered with CWINApp./N);

AfxMessageBox (AFX_IDP_FAILED_TO_CREATE_DOC);

Return;

}

CDOCTemplate * ptemplate = (cdoctemplate *) m_templatelist.gethead ();

// If you contain multiple document templates, display a new document type dialog

IF (M_TemplateList.getCount ()> 1)

{

// More one one document template to Choose from

// BRING UP DIALOG PROMPTING User

CNEWTYPEDLG DLG (& M_TemplateList);

INT NID = DLG.DOMODAL ();

IF (NID == IDOK)

PTEMPLATE = DLG.M_PSelectedTemplate;

Else

Return; // none - Cancel Operation

}

Assert (PTEMPLATE! = NULL);

Ask_KINDOF (CDOCTemplate, PTEMPLATE);

// Establish an empty file

PTEMPLATE-> OpenDocumentFile (NULL);

// if returns null, the user has already been aleted

}

2.ID_FILE_OPEN

CWINAPP :: onfileopen

Call m_pdocmanager-> onfileOpen ()

Void cdocmanager :: onfileopen () {

/ / Appeared file name in the file dialog file

Cstring newname;

IF (! doPROMPTFILENAME (newname, afx_ids_openfile,

OFN_HIDEREADOSLY | OFN_FILEMUSTEXIST, TRUE, NULL)

Return;

// Use OpenDocumentFile

AFXGetApp () -> OpenDocumentFile (NewName);

// if returns null, the user has already been aleted

}

3.ID_FILE_SAVE

CDocument :: onfilesave ()

Call DOFILESAVE ()

Void cdocument :: onfilesave ()

{

DOFILESAVE ();

}

DOFILESAVE () will call DOSAVE ()

Bool cdocument :: Dofilesave ()

{

DWORD DWATTRIB = getFileAttributes (m_strpathname);

// If the file is read-only, or there is no existence

IF (dwattrib & file_attribute_readonly)

{

// Use DOSAVE with NULL parameters

IF (! DOSAVE (NULL))

{

TRACE0 ("Warning: File Save with new name failed./n");

Return False;

}

}

Else

{

// Use dosave (....)

IF (! DOSAVE (M_StrPathname))

{

TRACE0 ("Warning: File Save Failed./N);

Return False;

}

}

Return True;

}

DOSAVE () implementation

Bool cdocument :: DOSAVE (LPCTSTR LPSZPATHNAME, BOOL BREPLACE)

// If the file name parameter is empty, let the user name

// NOTE: LPSZPATHNAME CAN Be Different Than 'M_StrPathname'

{

CString newname = lpszpathname;

IF (newname.isempty ())

{

CDOCTemplate * pTemplate = getDOCTemplate ();

Assert (PTEMPLATE! = NULL);

NEWNAME = m_strpathname;

IF (BREPLACE && newname.isempty ())

{

NewName = m_STRTITLE;

// Check for Dubious filename

INT ibad = newname.findoneof (_T ("#%; ///")));

IF (iBAD! = -1)

NewName.ReleaseBuffer (IBAD);

// append the default suffix if there is one

Cstring strexT;

IF (Ptemplate-> getDocstring (strexT, cdoctemplate :: filterext) &&

! strext.isempty ())

{

Assert (strexT [0] == '.');

NewName = strext;

}

}

// "Save as" dialog

IF (! AfxGetApp () -> DOPROMPTFILENAME (NewName,

BREPLACE? AFX_IDS_SAVEFILE: AFX_IDS_SAVEFILECOPY, OFN_HIDEREADONLY | OFN_PATHMUSTEXIST, FALSE, PTEMPLATE))

Return False; // Don't Even Attempt to Save

}

CWAITCURSOR WAIT;

/ / Save, complete by OnsaveDocument

IF (! onsavedocument (newname))

{

IF (LPSZPATHNAME == NULL)

{

// be Sure to delete the file

Try

{

Cfile :: remove (newname);

}

Catch_all (e)

{

Trace0 ("Warning: failed to delete file / n");

DELETE_EXCEPTION (E);

}

END_CATCH_ALL

}

Return False;

}

// reset the title and change the document name

IF (BREPLACE)

SetPathname (newname);

Return True; // Success

}

Onsedocument using the document class completes the save action

Bool cdocument :: OnSaveDocument (lpctstr lpszpathname)

{

CfileException Fe;

CFILE * PFILE = NULL;

Pfile = getfile (lpszpathname, cfile :: modecreate |

Cfile :: ModeReadwrite | CFILE :: ShareExClusive, & fE);

IF (pfile == null)

{

Reportsaveloadexception (LPSZPATHNAME, & FE,

TRUE, AFX_IDP_INVALID_FILENAME);

Return False;

}

// Establish a saved carchive

Carchive Savearchive (Pfile, Carchive :: Store | CARCHIVE :: Bnoflush ";

SavearchiVe.m_pdocument = this;

SavearchiVe.m_bforceflat = false;

Try

{

CWAITCURSOR WAIT;

// Use the serialization operation of the document class to complete the actual archive behavior

Serialize (savearchive); // Save me

Savearchive.close ();

ReleaseFile (Pfile, False);

}

Catch_all (e)

{

ReleaseFile (Pfile, True);

Try

{

Reportsaveloadexception (lpszpathname, e,

TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);

}

END_TRY

DELETE_EXCEPTION (E);

Return False;

}

END_CATCH_ALL

SetModifiedFlag (false); // back to unmodified

Return True; // Success

}

4.ID_FILE_SAVE_AS

CDocument :: onfilesaveas ()

Specific implementation: DOSAVE that calls Null parameters (.)

Void cdocument :: onfilesaveas ()

{

IF (! DOSAVE (NULL))

TRACE0 ("Warning: File Save-as Failed./N");

5.ID_FILE_SAVE_COPY_AS

The celeserverdoc :: onfilesavecopyas

6.ID_FILE_CLOSE

CDocument :: OnfileClose

void cdocument :: onfileclose ()

{

// Save content

IF (! savemodified ())

Return;

// Close the document

OnClosedocument ();

}

If you need to save, call DOFILESAVE (..)

Bool cdocument :: savemodified ()

{

// Nothing to change

IF (! ismodified ())

Return True;

// Get the name of the file

CString name;

IF (m_strpathname.isempty ())

{

// Get Name based on CAPTION

Name = m_STRTILE;

IF (Name.isempty ())

Verify (name.loadstring);

}

Else

{

// Get Name Based on File Title of Path Name

Name = m_strpathname;

IF (AfxData.Bmarked4)

{

AfxGetFileTitle (m_strpathname,

Name.getBuffer (_MAX_PATH), _MAX_PATH);

Name.releaseBuffer ();

}

}

CSTRING PROMPT;

AFXFORMATSTRING1 (Prompt, AFX_IDP_ASK_TO_SAVE, NAME);

// Dialog: The file has been changed, is it saved?

Switch (AFXMESSAGEBOX (Prompt, MB_YESNOCANCEL, AFX_IDP_ASK_TO_SAVE))

{

Case IDCANCEL:

Return False; // Don't Continue

Case iDYES:

//save document

IF (! dofilesave ())

Return False; // Don't Continue

Break;

Case IDNO:

// if not savings, Revert the document

Break;

DEFAULT:

Assert (false);

Break;

}

Return True;

}

7.ID_FILE_UPDATE

ColeserverDoc :: onupdatedocument

8.ID_FILE_PRINT_SETUP

CWINAPP :: OnfilePrintSetup

9.ID_FILE_PRINT

CView :: onfileprint

10.ID_FILE_PRINT_PREVIEW

CView :: OnfilePrintpreview

11. Default editing control ID

ID_EDIT_CLEAR

ID_EDIT_CLEAR_ALL

ID_EDIT_COPY

ID_EDIT_CUT

ID_EDIT_FIND

ID_EDIT_PASTE

ID_EDIT_PASTE_LINK

ID_EDIT_PASTE_SPECIAL

ID_EDIT_REPEAT

ID_EDIT_REPLACE

ID_EDIT_SELECT_ALL

ID_EDIT_UNDO

ID_EDIT_REDO

These IDs have corresponding processing functions in the corresponding edit

12.id_window_new

CmdiframeWnd :: OnWindOwnew

Void cmdiframeWnd :: OnWindOwnew ()

{

/ / Find the current window, get the corresponding document template, create a new window cmdigetAndWnd * PACTIVECHILD = MDiGetACTIVE ();

CDocument * pdocument;

IF (PACTIVECHILD == Null ||

(pDocument = PACTIVECHILD-> getActiveDocument ()) == NULL)

{

TRACE0 ("Warning: No Active Document for Windownew Command./N");

AFXMessageBox (AFX_IDP_COMMAND_FAILURE);

Return; // Command Faled

}

// OtherWise We Have A New Frame!

CDOCTemplate * pTemplate = pDocument-> getDocchemplate ();

AskERT_VALID (PTEMPLATE);

CFrameWnd * Pframe = PTemplate-> CreateNewFrame (pDocument, pactivechild);

IF (pframe == null)

{

Trace0 ("Warning: failed to create new frame./N");

Return; // Command Faled

}

PTEMPDATE-> InitialUpdateFrame (Pframe, PDocument);

}

13.id_window_arrange

ID_WINDOW_ARRANGE, and ID_WINDOW_CASCADE, ID_WINDOW_TILE_HORZ, ID_WINDOW_TILE_VERT is processed by ONMDIWINDOWCMD ()

OnMDIWINDOWCMD sends the corresponding message to m_hwndmdiclient

Bool cmdiframewnd :: OnMDIWindowcmd (uint nid)

{

Assert (m_hwndmdiclient! = Null);

UINT MSG;

Uint wparam = 0;

Switch (NID)

{

DEFAULT:

Return False; // Not for US

Case id_window_arrange:

MSG = WM_MDIICONARRANGE;

Break;

Case id_window_cascade:

MSG = WM_MDICASCADE;

Break;

Case id_window_tile_horz:

WPARAM = MDITILE_HORIZONTAL;

// Fall Through

Case ID_window_tile_vert:

Assert (mditile_vertical == 0);

MSG = WM_MDITILE;

Break;

}

:: SendMessage (m_hwndmdiclient, msg, wparam, 0);

Return True;

}

14.id_window_cascade

See ID_WINDOW_ARRANGE

15.id_window_tile_horz

See ID_WINDOW_ARRANGE

16.id_window_tile_vert

See ID_WINDOW_ARRANGE

17.id_window_split

CSplitterWnd :: dokeyboardsplit

Control the segmentation window with a keyboard

Bool csplitterWnd :: dokeyboardsplit ()

{

Assert_Valid (this);

INT HT; IF (M_nRows> 1 && M_ncols> 1)

HT = Splitter InterSection1; // Split EXISTING ROW COL

Else IF (M_nRows> 1)

HT = vsplitterbar1; // split existing row

Else IF (m_ncols> 1)

HT = HSPLitterbar1; // Split EXISTING COL

Else IF (M_NMaxRows> 1 && M_nmaxcols> 1)

HT = Bothsplitterbox; // We can Split Both

Else IF (m_nmaxrows> 1)

HT = vsplitterbox; // we can split rows

Else IF (m_nmaxcols> 1)

HT = hsplitterbox; // we can split columns

Else

Return false; // can't split

// start tracking

StartTracking (HT);

CRECT RECT;

Rect.Left = m_recttracker.width () / 2;

Rect.top = m_recttracker.height () / 2;

IF (m_pttrackoffset.y! = 0)

Rect.top = m_recttracker.top;

IF (m_pttrackoffset.x! = 0)

RECT.LEFT = m_btracking2? m_recttracker2.left: m_recttracker.left;

Rect.offsetRect (-m_pttrackoffset.x, -m_pttrackoffset.y);

Clienttoscreen (& Re);

SetCursorpos (Rect.Left, Rect.Top);

Return True;

}

18.ID_APP_ABOUT

Establish CWINAPP :: onappaBout ();

19.ID_APP_EXIT

CWINApp :: onappexit

Send a WM_CLOSE message directly to the main window

Void cwinapp :: onappexit ()

{

// Same As Double-CLICKING ON Main Window CLOSE Box

Assert (m_pmainwnd! = Null);

m_pmainwnd-> sendMessage (WM_Close);

}

20.id_help_index

CWINApp :: onhelpindex

21.ID_HELP_USING

CWINApp :: onhelpusing

22.id_context_help

CWINAPP :: onContexthelp

23.id_help

CWINApp :: onhelp

24.ID_DEFAULT_HELP

CWINApp :: onhelpindex

25.id_next_pane

CsplitterWnd :: OnNextPaneCMD

26.ID_PREV_PANE

CsplitterWnd :: OnNextPaneCmd

27.id_ole_insert_new

28.id_ole_edit_links

29.ID_view_toolbar

Switch AFX_IDW_TOOLBAR Toolbar

Message mapping

ON_UPDATE_COMMAND_UI (id_view_toolbar, onupdateControlbarmenu) on_command_ex (id_view_toolbar, onbarcheck)

Bool cframewnd :: Onbarcheck (uint nid)

{

Assert (id_view_status_bar == afx_idw_status_bar);

Assert (id_view_toolbar == afx_idw_toolbar);

Assert (id_view_rebar == afX_IDW_REBAR);

CControlbar * PBAR = getControlbar (NID);

IF (PBAR! = NULL)

{

/ / Set this tool strip status

ShowControlbar (PBAR, (PBAR-> getStyle () & ws_visible) == 0, FALSE);

Return True;

}

Return False;

}

Void CFrameWnd :: OnUpdateControlbarmenu (ccmdui * pcmdui)

{

Assert (id_view_status_bar == afx_idw_status_bar);

Assert (id_view_toolbar == afx_idw_toolbar);

Assert (id_view_rebar == afX_IDW_REBAR);

Ccontrolbar * PBAR = getControlbar (PCMDUI-> M_NID);

IF (PBAR! = NULL)

{

PCMDUI-> SetCheck ((PBar-> getStyle () & ws_visible)! = 0);

Return;

}

PCMDUI-> Continuerouting ();

}

30.ID_View_status_bar

Implementation method is the same as ID_View_toolbar

转载请注明原文地址:https://www.9cbs.com/read-3130.html

New Post(0)