This is a big job for learning VC. It was a super notebook and suddenly found out. It was uploaded for your reference!
I. Functional requirements:
1. Add more functions based on the original Notepad program:
1). can change the background color.
2). The font color can be changed.
3). The font can be changed.
4). Alignment of paragraphs.
Ii. Overall Design:
The first statement is: this program inherits from cricheditview.
1. Add the "format (o)" menu item:
(1) Add the "font (f)" sub menu.
(2) Add the "background color (B)" sub menu.
(3) Add the font color sub-menu.
2. Add a new toolbar:
(1) Add the bold button.
(2) Add the Italic button.
(3) Add the underline button.
(4) add the "Left alignment", "center", and "right alignment" buttons.
(5) add the "text color" and "background color" buttons.
3. Status Bar display time:
Displays the system time in real time.
4. Change the application icon:
5. Add a shortcut menu.
Iii. Detailed Design:
1. Add the "format (o)" menu item:
(1) Add the "font (f)" sub menu:
After the menu is added, add the message response function to the View class. In this function, the cfontdialog class of the general dialog box is called to change the font.
The Code is as follows:
Void cmywordview: onformatfont () // set the font
{
// Todo: add your command handler code here
Cmyworddoc * pdoc = getdocument ();
Assert_valid (pdoc );
Cfontdialogcfd (& pdoc-> m_lf, cf_screenfonts | cf_inittologfontstruct );
If (CFD. domodal () = idok) // if you click OK, set the font
{
Charformatcf;
Cf. cbsize = sizeof (charformat );
Cf. dwmask = cfm_face | cfm_size;
: Lstrcpy (Cf. szfacename, CFD. getfacename ());
Cf. yheight = CFD. getsize () * 2;
Setcharformat (CF );
}
}
(2) Add the "background color (B)" sub menu:
After the menu is added, add the message response function to the View class. And call the color dialog box to set the color.
Note that:
The background color inherited from cricheditview does not seem suitable. I saw that I didn't seem to have changed the background color. I could only change the background color of the font.
Because the background color will overwrite the previous text and must be output again
The font size, color, and other information of the previous text must be stored and output.
My code is:
Void cmywordview: onbkcolor () // change the background color
{
// Todo: add your command handler code here
Cmyworddoc * pdoc = getdocument ();
Assert_valid (pdoc );
Ccolordialog DLG;
Cricheditctrl & editctrl = getricheditctrl ();
Charformat CF;
If (DLG. domodal () = idok)
{
Editctrl. setbackgroundcolor (false, DLG. getcolor (); // obtain the color selected by the user
Cf. dwmask = cfm_color;
Cf. cbsize = sizeof (charformat );
Editctrl. getdefacharcharformat (CF );
Cf. crtextcolor = RGB (0, 0 );
Cf. dweffects & = ~ Cfe_autocolor;
Editctrl. setdefacharcharformat (CF );
}
}
(3) add the "font color (t)" sub menu:
Similar to changing the background color, you must call the general dialog box-color dialog box.
After obtaining the color set by the user, re-design and output the characters.
The Code is as follows:
Void cmywordview: ontxtcolor () // change the font color
{
// Todo: add your command handler code here
Charformatcf;
Cmyworddoc * pdoc = getdocument ();
Assert_valid (pdoc );
Ccolordialogdlg;
If (DLG. domodal () = idok)
{
Cf. crtextcolor = DLG. getcolor (); // obtain the selected color.
Zeromemory (& CF, sizeof (charformat ));
Cf. crtextcolor = DLG. getcolor ();
Cf. dwmask = cfm_charset | cfm_color;
Cf. dweffects = Cf. dweffects &~ Cfe_autocolor; // remove the original color effect and change it to a new color.
Setcharformat (CF); // set
}
}
2. Add a new toolbar:
A. Create a new toolbar:
First, create a new toolbar resource in the resource view.
Secondly, because the toolbar belongs to the framework class, a new toolbar is displayed in the oncreate function of the cmainframe class. The tool bar can be modeled after it is automatically generated.
In the resource view, add the bold, italic, and underline buttons to the new toolbar resources.
B. Use the charformat structure in RichEdit structure to set bold, italic, and underline.
The Code is as follows:
Void cmywordview: oncharbold () // whether the font is bold
{
// Todo: add your command handler code here
Charformatcf;
Cf = getcharformatselection ();
If (! (Cf. dwmask & cfm_bold) |! (Cf. dweffects & cfe_bold) // if it is not in bold, change it to bold.
Cf. dweffects = cfe_bold;
Else // otherwise, change to the original font
Cf. dweffects = 0;
Cf. dwmask = cfm_bold;
Setcharformat (CF );
}
The other three are similar.
(4) add the "Left alignment", "center", and "right alignment" buttons.
Directly call the member function onparaalign () of the cricheditview class.
Void cmywordview: onparaleft ()
{
// Todo: add your command handler code here
Onparaalign (pfa_left); // left-aligned paragraph
}
(5) add the "text color" and "background color" buttons:
The IDS of these two toolbar buttons are the same as those of the Response List menu in the menu item.
3. Status Bar display time:
The system time can be displayed in real time. You must obtain the system time and set the timer to update from time to time.
Obtain the system time and display it on the status bar:
Ctimet = ctime: getcurrenttime ();
Cstring STR = T. Format ("% H: % m: % s"); // set the display time of the status bar
Cclientdc DC (this );
Csize SZ = Dc. gettextextent (STR );
Int Index = 0;
Index = m_wndstatusbar.commandtoindex (ids_timer );
M_wndstatusbar.setpaneinfo (index, ids_timer, sbps_normal, Sz. CX );
M_wndstatusbar.setpanetext (1, STR );
Settimer (, null); // sets the timer
When the timer arrives, the following function Update time is called and displayed:
Void cmainframe: ontimer (uint nidevent)
{
// Todo: add your message handler code here and/or call default
Ctime T = ctime: getcurrenttime ();
Cstring STR = T. Format ("% H: % m: % s ");
Cclientdc DC (this );
Csize SZ = Dc. gettextextent (STR );
M_wndstatusbar.setpaneinfo (1, ids_timer, sbps_normal, Sz. CX );
M_wndstatusbar.setpanetext (1, STR );
Cframewnd: ontimer (nidevent );
}
4. Change the application icon:
You only need to erase the original icon and draw a new one.
Note: you must add two icons of different sizes.
5. Add a shortcut menu.
In the right-click message response function, add the design and display the pop-up menu:
Voidcmywordview: onrbuttonup (uint nflags, cpoint point) // create a pop-up menu
{
Hwnd;
Point P;
Getcursorpos (& P );
: Screentoclient (hwnd, & P );
Cmenu menupopup;
If (menupopup. createpopupmenu ())
{
Menupopup. appendmenu (mf_string, id_edit_undo, "undo (& U) \ tctrl + z ");
Menupopup. appendmenu (mf_string, id_edit_cut, "Cut (& C) \ tctrl + X ");
Menupopup. appendmenu (mf_string, id_edit_copy, "Copy (& C) \ tctrl + C ");
Menupopup. appendmenu (mf_string, id_edit_paste, "paste (& P) \ tctrl + V ");
Menupopup. appendmenu (mf_string, id_edit_select_all, "select all (& L) \ tctrl + ");
Menupopup. appendmenu (mf_string, id_format_font, "font (& F )");
Menupopup. appendmenu (mf_string, idm_bkcolor, "background color (& B )");
Menupopup. appendmenu (mf_string, idm_txtcolor, "font color (& T )");
Menupopup. trackpopupmenu (tpm_leftalign, p. x, P. Y, this );
}
Cricheditview: onrbuttonup (nflags, point );
}
Iv. Test and implementation:
V. Summary:
Through the development of this super notepad, I have a better understanding of the framework of MFC, and also feel the ease of use for the rich classes in MFC. I wrote it After inheriting ceditview according to my instructor's courseware at the beginning, but I found that although it can implement the notepad function, it is difficult to extend other functions of ceditview. As a result, I inherited from cscrollview, which has good scalability, but all its functions had to be compiled by myself, and I felt a little broken at the time. But I still need to check the information, read the books, and implement it myself. From the beginning, only outputs cannot be input, to can be input, but cannot wrap or delete,
The mouse cannot be processed after a line break or deletion occurs. In this process, I learned a lot, not only knowledge, but also programming ideas. Many things are not what we think. For example
The original string takes the total length (n-1), sets the text color as the background color, and then outputs the re-assigned string.
Because it inherits from cscrollview, it takes a long time to write too many files. If you submit a job, you may not be able to perform the most basic operations. I decided to write it over the winter vacation.
Finally, I inherited from the cricheditview, and implemented functions such as changing the font, font color, and background color. I also learned a lot.
Reprinted please indicate the source: dunni column http://blog.csdn.net/u012027907
Advantages of this program: added the text editing Extension function to enable it to edit rich texts.
Disadvantages of this program: the interface design is still old and has limited functions. Should be aligned with the WordPad or word, although it cannot be the same, but try to like its interface, and can achieve its 1/5 function is very good.