Letter on the window
I have written a similar: http://dev.9cbs.net/develop/Article/29/29193.SHTM
However, there is no problem such as Back Space. And the display font also receives the restriction. This is still immature, but it can be improved.
Class code:
============================================================================================================================================================================================================= ============
--- textFun.h ---
/ ************************************************** ***********
* Note:
* A class of drawing font on any window witch was attached to
* an instance of this class.
*
* BUGS to FIT:
* 1, No Memory DC Supported So That Xorpen Maybe Disturb That
* EXISTING FONT Shape On DC.
* 2, The CLASS Cannot Serialize The Drawing OPTS.
* 3, on small font, The Urgly Face To Be Improved.
*
* By enoloo
* First Edition: 8 / 10,2004
*********************************************************** ********** /
#ifndef _textFun_H_
#define _textFun_h_
#include
#include
#include
#include
Class CTextFun
{
PUBLIC:
CTextFun () {}
CTextFun (HWND HWND, HFONT = NULL,
ColorRef Color = RGB (0, 0, 255));
~ CtextFun ();
PUBLIC:
Void attach (hwnd hwnd, hfont hfont = NULL,
ColorRef Color = RGB (0, 0, 255));
Void Enter (Point Point);
Void Leave ();
Bool isattached () const {return (m_hwnd! = null &&& :: iswindow (m_hwnd));}
PUBLIC:
Bool Pushchar (uint nchar, bool bforward);
Void Popchar ();
Void PushString (LPCTSTR STR2PUSH);
Std: string getString () const {return m_strtext;}
Long getHeight () const {return m_nhem;}
Long getWidth () const {return (m_ptnow.x - m_ptstart.x);}
Point getCurrentPoint () const {return m_ptnow;} protected:
// Create a Default True-Type Font Here.
Void createDefault ();
Void _PushString (LPCTSTR STR2PUSH, BOOL BFORWARD);
protected:
HWND M_HWND;
HFONT M_HFONT;
Std :: string m_strtext;
Point m_ptstart;
Point m_ptnow;
ColorRef M_Clcolor;
Long m_nheight;
BOOL M_BDEFAULTFONT;
}
#ENDIF
============================================================================================================================================================================================================= ======
--- textFun.cpp ---
#include "textfun.h"
CtextFun :: CTextFun (hwnd hwnd, hfont hfont / * = null * /,
ColorRef color / * = rgb * /)
{
Attach (hwnd, hfont, color);
}
CTextFun :: ~ ctextfun ()
{
IF (M_BDefaultFont)
:: DeleteObject (m_hfont);
Leave ();
}
// Note: HFONT MUST BE A TRUE TYPE FONT.
Void ctextfun :: attach (hwnd hwnd, hfont hfont / * = null * /, colorref color / * = rgb * /)
{
ASSERT (HWND);
Assert (:: iswindow (hwnd));
m_hwnd = hwnd;
HDC HDC = NULL;
HDC = :: getDC (hwnd);
ASSERT (HDC);
IF (hfont == null)
{
m_bdefaultfont = true;
/ *
HFONT HFT = (HFONT) :: getCurrentObject (HDC, OBJ_FONT);
Assert (HFT);
M_HFONT = HFT;
* /
CreateDefaultfont ();
}
Else
{
m_bdefaultfont = false;
M_HFONT = HFONT;
}
m_clcolor = color; // color;
HFONT OLDFT = (HFONT) :: SelectObject (HDC, M_HFONT);
TextMetric TM;
:: GetTextMetrics (HDC, & TM);
m_nheight = tm.tmheight;
:: SelectObject (HDC, Oldft);
:: ReleaseDC (HWND, HDC);
}
Void ctextfun :: Enter (Point Point)
{
m_ptstart = m_ptnow = point;
:: destroycaret ();
askERT (:: CreateCaret (M_HWND, NULL, 1, M_NHEIGHT)); :: showcaret (m_hwnd);
:: setCaretPos (Point.x, Point.y);
}
Void ctextfun :: leave ()
{
m_ptstart.x = m_ptnow.x = m_ptstart.y = m_ptnow.y = 0;
m_strtext.rase ();
:: HIDECARET (M_HWND);
:: destroycaret ();
}
// for onchar ...
Bool CTextFun :: Pushchar (uint nchar, bool bforward)
{
Static Bool BfirstChinese = false;
Static char strTemp [3];
IF (nchar> 127 &&! bfirstchinese) // Maybe a Chinese char code
{
IF (BForward)
Strtemp [0] = nchar;
Else
strTemp [1] = nchar;
BfirstCHINESE = TRUE;
Return False; // Wait for the second char CODE
}
Else IF (nchar> 127 && bfirstCHINESE)
{
IF (BForward)
strTemp [1] = nchar;
Else
Strtemp [0] = nchar;
Strtemp [2] = 0;
BfirstCHINESE = FALSE;
}
Else // an ascii char code
{
BfirstCHINESE = false; // back the status here.
Strtemp [0] = nchar;
Strtemp [1] = 0;
}
_PushString (Strtemp, BForward);
Return True;
}
Void ctextfun :: popchar ()
{
int str hardth = m_STRText.length ();
IF (m_strtext.empty ())
Return;
HDC HDC = NULL;
HDC = :: getDC (m_hwnd);
ASSERT (HDC);
IF (! pushchar (m_strtext [strlength - 1], false) // Maybe a Chinese char CODE
{
IF (m_strtext.empty ())
{
m_strtext.rase ();
Return;
}
Pushchar (M_STRText [Strlength - 2], FALSE);
}
Return;
}
Void ctextfun :: _ PushString (LPCTSTR STR2PUSH, BOOL BFORWARD)
{
// CREATE A Caret Always.
Int strlen = _TCSCLEN (STR2PUSH);
askERT (Strlen> = 0);
Assert (:: CreateCaret (M_HWND, NULL, 1, M_NHEIGHT));
:: Showcaret (m_hwnd);
HDC HDC = NULL;
HDC = :: getDC (m_hwnd);
ASSERT (HDC);
INT prex = m_ptnow.x; // save it.
Logbrush logbr; logbr.lbcolor = m_clcolor;
Logbr.lbhatch = 0;
Logbr.lbstyle = BS_SOLID;
HPEN PEN, OLDPEN;
Pen = (HPEN) :: EXTCREATEPEN (PS_GEOMETRIC | PS_SOLID |
PS_ENDCAP_SQUARE | PS_JOIN_BEVEL, 1, & logbr, 0, null;
// Pen = (HPEN) :: Createpen (ps_solid, 1, m_clcolor);
Oldpen = (HPEN) :: SelectObject (HDC, (HGDiobj) Pen;
Hbrush Br, Oldbr;
Br = :: Createsolidbrush (m_clcolor);
Oldbr = (Hbrush) :: SelectObject (HDC, (HGDiobj) BR);
HFONT OLDFT = (HFONT) :: SelectObject (HDC, M_HFONT);
Rect RTQuery;
:: DrawText (HDC, Str2push, _Tcslen (str2push), & rtQuery, dt_calcRect); // Get The Rect for the char to draw
HRGN HRGN;
IF (BForward)
{
m_ptnow.x = m_ptnow.x (RTQuery.right - RTQuery.Left);
HRGN = :: CreateRectrGN (prex, m_ptnow.y, m_ptnow.x, m_ptnow.y m_nheight);
}
Else
{
m_ptnow.x = m_ptnow.x - (RTQuery.right - RTQuery.Left);
HRGN = :: CreateRectrgn (m_ptnow.x, m_ptnow.y, prex, m_ptnow.y m_nheight);
}
// :: extselectClipRGN (HDC, HRGN, RGN_COPY);
// set the Caret's Pos Now
:: setCareTPOS (m_ptnow.x, m_ptnow.y);
:: setBkmode (HDC, Transparent);
// Draw the char code
// :: setPolyfillMode (HDC, Alternate);
INT PRER2 = :: Setrop2 (HDC, R2_NOTXORPEN);
:: BeGinPath (HDC);
IF (BForward)
:: Textout (HDC, prex, m_ptnow.y, str2push, _tcslen (str2push);
Else
:: Textout (HDC, M_PTNow.x, M_PTNow.y, Str2push, _Tcslen (Str2push);
:: Endpath (HDC);
// NOTE: DONT CALL A Silly Function StrokeAndfillPath!
:: StrokeAndfillPath (HDC);
//: SelectClippath (HDC, RGN_and);
:: Setrop2 (HDC, PRER2);
:: SelectObject (HDC, (HgDiobj) Oldpen;
:: SelectObject (HDC, (HGDiobj) Oldbr;
:: SelectObject (HDC, (HgDiobj) Oldft);
// save the char code
IF (BForward)
M_STRText = std :: string (str2push); ELSE
m_strtext = m_strtext.substr (0, m_strtext.length () - _TCSLEN (STR2PUSH));
Return;
}
Void ctextfun :: PushString (LPCTSTR STR2PUSH)
{
_PushString (str2push, true);
}
Void ctextfun :: createDefaultfont ()
{
Logfont LF;
:: MEMSET (& lf, 0, sizeof (logfont));
Lf.lfheight = 40;
Lf.lfweight = / * fw_bold * / fw_normal;
Lf.LFEScapement = 0;
Lf.LForientation = 0;
Lf.lfitalic = false;
Lf.lfunderline = false;
Lf.lfstrikeout = false;
Lf.lfcharset = ansi_charset;
Lf.lfoutPrecision = OUT_DEFAULT_PRECIS;
Lf.lfclipprecision = Draft_quality;
Lf.lfpitchandfamily = variable_pitch | ff_modern;
STRCPY (LF.LFFACENAME, "Arial");
M_HFONT = :: CREATEFONTINDIRECT; & lf;
}
Description:
Class Supports letters all True-Type fonts on a window of one and class object binding (Attach). Support the Bake Escape key. If the wrap can get a height from the class, you can create a new class object below.
Do not support up and down, left and right cursors, no support for MEMDC, because it is made with NOTXORPEN and the path, so text will generate interference when superimposed. Solving these issues should not be very difficult.
use:
1. Bind the form and CTextFun object. The TRUE-TYPE font and color can be specified in the binding. You can also use the default. m_fun is a ctextfun object, called:
m_fun.attach (this-> getsafehwnd ());
2. Display text can call Enter and Leave to complete. The Enter function sets the location of the display cursor, the Leave function clears the saved buffer text data. If in the view, you can call in ONLBUTTONDOWN:
m_fun.enter (point);
In OnChar, you can call:
Void cchildview :: onchar (uint nchar, uint nrepcnt, uint nflags)
{
// Todo: add your message handler code here and / or call default
If (nchar == vk_back) // back key is retracted, and the text box is the same
{
m_fun.popchar ();
CWnd :: OnChar (nchar, nrepcnt, nflags);
Return;
}
Else if (nchar == vk_return) // Enter key, end the display
{
m_fun.leave ();
CWnd :: OnChar (nchar, nrepcnt, nflags);
Return;
}
M_Fun.pushchar (nchar, true); // Normal key, the rebound window shows CWnd :: OnChar (nchar, nrepcnt, nflags);
}
3, the above is synchronized in onchar, if you want to display a string once, you can call:
m_fun.pushstring ("China"); // For example, in ONLBUTTONDOWN
The effect is as follows:
Test code and other J.
By enoloo,
8 / 10,2004