MFC draw text drawtext, gettextextent, gettextmetrics

Source: Internet
Author: User
// Textview. CPP: Implementation of the ctextview class // # include "stdafx. H "# include" text. H "# include" textdoc. H "# include" textview. H "# ifdef _ debug # define new debug_new # UNDEF this_filestatic char this_file [] = _ file __; # endif ////////////////////////////////////// //////////////////////////////////////// /ctextviewimplement_dyncreate (ctextview, cview) begin_message_map (ctextview, cview) // {afx_msg_map (Ctextview) on_wm_create () on_wm_char () returns () //} afx_msg_map // standard printing commandson_command (id_file_print, cview: onfileprint) on_command (outputs, cview :: onfileprint) on_command (id_file_print_preview, cview: onfileprintpreview) end_message_map () //////////////////////////////////////// /// // ctextview construction/destruct Ionctextview: ctextview () {// todo: Add Construction Code herem_strline = ""; m_ptstart = cpoint (0, 0); m_iwidth = 0;} ctextview ::~ Ctextview () {} bool ctextview: precreatewindow (createstruct & CS) {// todo: Modify the window class or styles here by modifying // The createstruct csreturn cview :: precreatewindow (CS );} //////////////////////////////////////// /// // ctextview drawingvoid ctextview:: ondraw (CDC * PDC) {ctextdoc * pdoc = getdocument (); assert_valid (pdoc); cstring STR ("Wu xxabc Hello World"); PDC-> textout (0,200, str );} //////////////////////////////////////// /// // ctextview printingbool ctextview:: onprepareprinting (cprintinfo * pinfo) {// default preparationreturn doprepareprinting (pinfo);} void ctextview: onbeginprinting (CDC */* PDC */, cprintinfo */* pinfo */) {// todo: add extra initialization before printing} void ctextview: onendprinting (CDC */* PDC */, cprintinfo */* pinfo */) {// todo: add cleanup after printing }/////////////////////////////////// //////////////////////////////////////// /// ctextview diagnostics # ifdef _ debugvoid ctextview:: assertvalid () const {cview: assertvalid ();} void ctextview: dump (cdumpcontext & DC) const {cview: dump (DC);} ctextdoc * ctextview :: getdocument () // non-debug version is inline {assert (m_pdocument-> iskindof (runtime_class (ctextdoc); Return (ctextdoc *) m_pdocument ;} # endif/_ debug ////////////////////////////////// //////////////////////////////////////// //// ctextview message handlersint ctextview:: oncreate (maid) {If (cview: oncreate (maid) =-1) Return-1; cclientdc (this); textmetric metric; DC. gettextmetrics (& metric); // createsolidcaret (metric. tmavecharwidth, metric. tmheight); m_bmpcaret.loadbitmap (idb_wu); createcaret (& m_bmpcaret); showcaret (); settimer (1,100, null); Return 0;} void ctextview: onchar (uint nchar, uint nrepcnt, uint nflags) {cfont font; font. createpointfont (300, "文 ", null); cclientdc DC (this); cfont * poldfont = dc. selectObject (& font); If (0x0d = nchar) {// entertextmetric MT; DC. gettextmetrics (& mt); m_ptstart.y + = Mt. tmheight; m_strline.empty ();} else if (0x08 = nchar) {// backspacecolorref colorold = dc. settextcolor (DC. getbkcolor (); DC. textout (m_ptstart.x, m_ptstart.y, m_strline); m_strline = m_strline.left (m_strline.getlength ()-1); DC. settextcolor (colorold);} else {m_strline + = nchar;} DC. textout (m_ptstart.x, m_ptstart.y, m_strline); csize SZ = dc. gettextextent (m_strline); setcaretpos (cpoint (m_ptstart.x + Sz. CX, m_ptstart.y); DC. selectObject (poldfont); cview: onchar (nchar, nrepcnt, nflags);} void ctextview: callback (uint nflags, cpoint point) {setcaretpos (m_ptstart = point ); m_strline.empty (); cview: onlbuttondblclk (nflags, point);} void ctextview: ontimer (uint nidevent) {If (nidevent = 1) {m_iwidth + = 5; cclientdc DC (this); cstring STR ("Wu xxabc Hello World"); csize SZ = dc. gettextextent (STR); crect rect (0,200, m_iwidth, 200 + Sz. cy); DC. settextcolor (RGB (0xff, 0, 0); DC. drawtext (STR, & rect, dt_left);} cview: ontimer (nidevent );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.