Sometimes we want to display a large text segment, but we cannot find a suitable control. Or the system controls are too monotonous to meet the requirement. We need custom controls to achieve what we want. I have implemented this custom control. The text is automatically wrapped, and the system scroll bar and some simple text display are added,
No more nonsense. paste the Code directly !!
. H file
/*
========================================================== ==========================================
Name: textdisplay. h
Author: Barrett
Version:
Copyright: Your copyright notice
Description: ctextdisplay Declaration
========================================================== ==========================================
*/
# Ifndef textdisplay_h
# Define textdisplay_h
// Des
# Include <coecntrl. h> // link against: Cone. Lib
# Include <FBS. h> // link against: fbscli. Lib
# Include <gulicon. h>
# Include <aknutils. h>
# Include <avkon. HRH>
// Class declaration
/**
* Ctextdisplay
*
*/
Class ctextdisplay: Public ccoecontrol
{
Public: // constructors and destructor
/**
* Destructor.
*/
~ Ctextdisplay ();
/**
* Two-phased constructor.
*/
Static ctextdisplay * newl (const trect & arect, ccoecontrol * aparent );
/**
* Two-phased constructor.
*/
Static ctextdisplay * newlc (const trect & arect, ccoecontrol * aparent );
Tkeyresponse offerkeyeventl (const tkeyevent & akeyevent, teventcode Atype );
PRIVATE:
/**
* Constructor for Ming 1st Stage Construction
*/
Ctextdisplay ();
Void draw (const trect & arect) const;
/**
* EPOC default constructor for faster Ming 2nd Stage Construction
*/
Void constructl (const trect & arect, ccoecontrol * aparent );
PRIVATE:
// Display content
Rpointerarray // The starting position is the row number.
Tint istartline;
// Font color
Trgb ifontcolour;
// Total number of lines of text
Tint itotalline;
// Text display area
Trect irect;
// Text font
Cfont * ifont;
// Scroll bar width
Tint iscrollbarwidth;
// Control background color
Trgb ibackground;
// Start position of text display
Tpoint istartpoit;
// Spacing between texts in each row
Tint ilinetolinespace;
// Maximum number of lines displayed on a screen
Tint imaxdisplayline;
Ccoecontrol * iparent;
// System scroll bar
Ceikscrollbarframe * isbframe;
Teikscrollbarmodel imodel;
// Create a system scroll bar
Void createscrollbars (tint acunt );
Public:
// Calculate the number of rows in the text
Void accounttotalline ();
// Calculate the maximum number of rows displayed on a screen
Tint getmaxdisplay ();
Void setscrollbarwidth (tint awidth );
// Set the background color
Void setbackground (trgb argb );
// Set the displayed text
Void setdisplaycontent (const tdesc & acontent, tbool auseflag = efalse );
// Set the text display font
Void setfont (cfont * afont );
// Set the text display color
Void setfontcolour (trgb argb );
};
# Endif // textdisplay_h
. Cpp File
/*
========================================================== ==========================================
Name: textdisplay. cpp
Author: Barrett
Version:
Copyright: Your copyright notice
Description: ctextdisplay implementation
========================================================== ==========================================
*/
# Include "textdisplay. H"
// # Include "auditioncontainer. H"
_ Trim (klinestart, "<line> ");
_ Begin (klineend, "</line> ");
# Define maxdisplayline 10
# Define linespace 3
# Define lastspace 25
Ctextdisplay: ctextdisplay ()
{
// No implementation required
// Ibackgroundbitmap = NULL;
Istartline = 0;
Itotalline = 0;
Ifontcolour = krgbblack; // default
Istartpoit = tpoint (3, 5 );
Ilinetolinespace = linespace;
Iscrollbarwidth = 10;
}
Ctextdisplay ::~ Ctextdisplay ()
{
If (idisplaycontent)
{
Idisplaycontent-> resetanddestroy ();
Delete idisplaycontent;
Idisplaycontent = NULL;
}
If (isbframe)
{
Delete isbframe;
Isbframe = NULL;
}
}
Ctextdisplay * ctextdisplay: newlc (const trect & arect, ccoecontrol * aparent)
{
Ctextdisplay * Self = new (eleave) ctextdisplay ();
Cleanupstack: pushl (Self );
Self-> constructl (arect, aparent );
Return self;
}
Ctextdisplay * ctextdisplay: newl (const trect & arect, ccoecontrol * aparent)
{
Ctextdisplay * Self = ctextdisplay: newlc (arect, aparent );
Cleanupstack: Pop (); // self;
Return self;
}
Void ctextdisplay: constructl (const trect & arect, ccoecontrol * aparent)
{
// Setrect (arect );
Irect = arect;
Tfontspec fontspec = ieikonenv-> densefont ()-> fontspecintwips ();
Fontspec. iheight * = 1;
Icoeenv-> screendevice ()-> getnearestfontintwips (ifont, fontspec );
Imaxdisplayline = getmaxdisplay ();
Iparent = aparent;
}
Tkeyresponse ctextdisplay: offerkeyeventl (const tkeyevent & akeyevent, teventcode Atype)
{
If (Atype = eeventkeydown) & (akeyevent. iscancode = estdkeydownarrow ))
{
If (itotalline> = istartline + imaxdisplayline)
{
++ Istartline;
}
}
Else if (Atype = eeventkeydown) & (akeyevent. iscancode = estdkeyuparrow ))
{
If (istartline> 0)
{
-- Istartline;
}
}
Isbframe-> movevertthumbto (istartline); // absolute Coordinate
Isbframe-> drawscrollbarsnow ();
Drawnow ();
Return ekeywasconsumed;
}
Void ctextdisplay: setdisplaycontent (const tdesc & acontent, tbool auseflag)
{
If (acontent. Length () = 0)
{
Return;
}
If (idisplaycontent)
{
Idisplaycontent-> resetanddestroy ();
Delete idisplaycontent;
Idisplaycontent = NULL;
}
Idisplaycontent = new rpointerarray
Hbufc * copytext = acontent. alloc ();
If (auseflag)
{
Tbuf <6> bufbegintag (klinestart );
Tbuf <7> bufendtag (klineend );
While (copytext-> length ())
{
Tint posbegin = copytext-> Find (bufbegintag) + bufbegintag. Length ();
Tint posend = copytext-> Find (bufendtag );
If (posbegin! = Kerrnotfound & posend! = Kerrnotfound)
{
Tptrc ptrperson = copytext-> mid (posbegin, posend-posbegin );
Hbufc * htext = ptrperson. alloc ();
Idisplaycontent-> append (htext );
If (copytext-> length ()> posend + bufendtag. Length () + 1)
{
Copytext-> des (). Copy (copytext-> des (). Right (copytext-> length ()-posEnd-bufEndTag.Length ()));
}
Else
{
Break;
}
}
}
Delete copytext;
}
Else
{
Idisplaycontent-> append (copytext );
}
// Obtain the number of rows displayed on the screen
// Calculate the total number of lines displayed in the text
Accounttotalline ();
Createscrollbars (itotalline-imaxdisplayline + 2 );
}
Void ctextdisplay: Draw (const trect & arect) const
{
Cwindowgc & GC = systemgc ();
GC. setbrushcolor (ibackground );
GC. Clear (arect );
GC. usefont (ifont );
GC. setpencolor (ifontcolour );
// Start point
Tpoint point = istartpoit;
// Record the position of the previous segment
Tpoint mark = tpoint (0,-(istartline * ifont-> heightinpixels () + ilinetolinespace ));
Tpoint point2;
If (idisplaycontent = NULL)
{
GC. drawtext (_ L ("NO content! "), Tpoint (arect. Width ()/3, arect. Height ()/3 ));
Return;
}
For (tint I = 0; I <idisplaycontent-> count (); I ++)
{
Tint nstringmaxdisplayline = ifont-> textwidthinpixels (* idisplaycontent) [I]-> des ()/(irect. Width ()-iscrollbarwidth) + imaxdisplayline;
Tbiditext * bidi = tbiditext: newl (* idisplaycontent) [I]-> des (), nstringmaxdisplayline );
Bidi-> wraptext (irect. Width ()-iscrollbarwidth, * ifont, null );
Tint lines = Bidi-> numberoflinesindisplaytext ();
For (tint I = 0; I <lines; I ++)
{
Point2 = tpoint (point. IX, point. Iy + Mark. Iy + ifont-> heightinpixels () * (I + 1) + ilinetolinespace * I );
Tint width = 0;
Tptrc text = Bidi-> lineofdisplaytext (I, width );
GC. drawtext (text, point2 );
}
Mark = point2;
Delete bidi;
}
// Draw a small triangle
If (mark. Iy> irect. Height ()-lastspace | istartline> 0)
{
GC. setbrushstyle (cgraphicscontext: esolidbrush );
GC. setpenstyle (cgraphicscontext: enullpen );
GC. setbrushcolor (krgbblue );
GC. drawrect (trect (tpoint (0, irect. Height ()-lastspace), tsize (irect. Width (), lastspace )));
GC. setbrushcolor (krgbblack );
Tint ntrigonwidth = 12; // The default triangle width is 12 pixels.
Tint ntrigonheight = 12; // The default triangle height is 12 pixels.
// Horizontal and vertical coordinates of the first vertex of a triangle
Tint pointx = arect. Width ()/2;
Tint pointy = arect. Height ()-lastspace + (lastspace-2 * ntrigonheight-1)/2;
If (itotalline >=istartline + imaxdisplayline & itotalline> imaxdisplayline)
{
Carrayfixflat <tpoint> * arrpoint = new (eleave) carrayfixflat <tpoint> (10 );
Arrpoint-> appendl (tpoint (pointx-ntrigonwidth/2, pointy + 1 + ntrigonheight); // + 1 indicates the distance between the two triangles.
Arrpoint-> appendl (tpoint (pointx + ntrigonwidth/2, pointy + 1 + ntrigonheight); // + 1 indicates the distance between the two triangles
Arrpoint-> appendl (tpoint (pointx, pointy + 2 * ntrigonheight ));
GC. drawpolygon (arrpoint );
Delete arrpoint;
}
If (istartline> 0)
{
Carrayfixflat <tpoint> * arrpoint = new (eleave) carrayfixflat <tpoint> (10 );
Arrpoint-> appendl (tpoint (pointx, pointy ));
Arrpoint-> appendl (tpoint (pointx-ntrigonwidth/2, pointy + ntrigonheight ));
Arrpoint-> appendl (tpoint (pointx + ntrigonwidth/2, pointy + ntrigonheight ));
GC. drawpolygon (arrpoint );
Delete arrpoint;
}
GC. setbrushstyle (cgraphicscontext: enullbrush );
}
}
Void ctextdisplay: setfontcolour (trgb argb)
{
Ifontcolour = argb;
}
Void ctextdisplay: setfont (cfont * afont)
{
Ifont = afont;
Imaxdisplayline = getmaxdisplay ();
}
Void ctextdisplay: setbackground (trgb argb)
{
Ibackground = argb;
}
Void ctextdisplay: accounttotalline ()
{
For (tint I = 0; I <idisplaycontent-> count (); I ++)
{
Tint nstringmaxdisplayline = ifont-> textwidthinpixels (* idisplaycontent) [I]-> des ()/(irect. Width ()-iscrollbarwidth) + imaxdisplayline;
Tbiditext * bidi = tbiditext: newl (* idisplaycontent) [I]-> des (), nstringmaxdisplayline );
Bidi-> wraptext (irect. Width ()-iscrollbarwidth, * ifont, null );
Tint lines = Bidi-> numberoflinesindisplaytext ();
Itotalline + = lines;
Delete bidi;
}
}
Tint ctextdisplay: getmaxdisplay ()
{
Return (irect. Height ()-lastspace-istartpoit. Iy)/(ifont-> heightinpixels () + ilinetolinespace );
}
Void ctextdisplay: setscrollbarwidth (tint awidth)
{
Iscrollbarwidth = awidth;
}
// Create a system scroll bar
Void ctextdisplay: createscrollbars (tint acunt)
{
// Construct
Isbframe = new (eleave) ceikscrollbarframe (iparent, null, etrue );
Isbframe-> createdoublespanscrollbarsl (etrue, efalse, etrue, etrue );
Isbframe-> settypeofvscrollbar (ceikscrollbarframe: edoublespan );
Isbframe-> setscrollbarvisibilityl (ceikscrollbarframe: eoff,
Ceikscrollbarframe: eauto );
Imodel = teikscrollbarmodel (acunt, 1, 0 );
// Set the region
Trect arect;
Arect = trect (tpoint (irect. Width ()-iscrollbarwidth, irect. ITL. Iy), tsize (iscrollbarwidth, irect. Height ()-lastspace ));
Isbframe-> tile (& imodel, arect );
// Set the initial position-absolute coordinates
Isbframe-> movevertthumbto (0 );
Isbframe-> drawscrollbarsnow ();
}
// End of File
Use in Container
Step 1: Define a member in the container
Ctextdisplay * itextdisplay;
Step 2: Initialize in constructl
The Code is as follows: // This is used to read a piece of text from the file.
_ Partition (kabout, "C: // about. ini ");
Rfs fs = ceikonenv: static ()-> fssession ();
Rfile FP;
Tint err = FP. Open (FS, kabout, efileread );
If (ERR = 0)
{
Tint nfilesize = 0;
FP. Size (nfilesize );
Hbufc8 * fileinfo = hbufc8: newlc (nfilesize );
Tptr8 file = fileinfo-> des ();
FP. Read (file, nfilesize );
Hbufc * fileinfo16 = hbufc: newlc (file. Length ());
Tptr16 PTR = fileinfo16-> des ();
/* PTR. Copy (File );*/
Convgbk2uni (file, PTR );
Itextdisplay = ctextdisplay: newl (arect, this );
Itextdisplay-> setdisplaycontent (fileinfo16-> des ());
Itextdisplay-> setcontainer1_wl (* This );
Itextdisplay-> setextent (tpoint (0,0), tsize (240,320 ));
Cleanupstack: popanddestroy (2 );
}
FP. Close ();
Before writing a piece of code into the activatel function, I will not mention the reason. I also paste the code for the convgbk2uni function. As follows:
Void convgbk2uni (tdesc8 & original, TDES & res) // This function is not written by me and is searched online
{
// RFS afileserversession = ceikonenv: static ()-> fssession ();
RFS afileserversession;
Afileserversession. Connect ();
// Cleanupstack: Pop ();
Ccnvcharactersetconverter * converter = ccnvcharactersetconverter: newlc ();
If (converter-> preparetoconverttoorfroml (kcharactersetidentifiergbk, afileserversession )! = Ccnvcharactersetconverter: eavailable)
User: Leave (kerrnotsupported );
Tint state = ccnvcharactersetconverter: kstatedefault;
Tptrc8 STR (original );
Hbufc * iinfotext = hbufc: newl (Str. Length ());
Tptr16 PTR = iinfotext-> des ();
If (ccnvcharactersetconverter: eerrorillformedinput = converter-> converttounicode (PTR, STR, State ))
User: Leave (kerrargument );
Res. Zero ();
Res. Copy (PTR );
Afileserversession. Close ();
Cleanupstack: popanddestroy ();
Delete iinfotext;
}
Step 2: Add 1 to the countcomponentcontrols function, and the componentcontrol function returns the control pointer.
Step 3: add it to the offerkeyeventl function.
Return itextdisplay-> offerkeyeventl (akeyevent, Atype );
Step 4: Write the destructor in the destructor.
Step 5: Compile and run the program.
The implementation process and use process are relatively simple. The use process is similar to the use of system controls. In my control, I provide two methods to display text.
The first one is here, and the other is using _ trim (klinestart, "<line> ");
_ Labels (klineend, "</line>.
Itextdisplay-> setdisplaycontent (fileinfo16-> des ();
Itextdisplay-> setdisplaycontent (fileinfo16-> des (), etrue, the displayed content is text like <line> how are you </line>.
At last, you need to pay attention to a problem. When using the system scroll bar, it requires a window, which can be shared with the parent class or defined by the control itself. As shown in
// Construct
Isbframe = new (eleave) ceikscrollbarframe (iparent, null, etrue); iparent is the shared parent class window. If this is not used here, this is used directly, in the next sentence isbframe-> createdoublespanscrollbarsl (etrue, efalse, etrue, etrue); there will be a panic of Kern-exec 3, which is my own understanding or not. If you want to know the big data, give me some advice ~~