How can we vertically align text in the edit window?

I created an edit box as:

m_EditWnd.Create(m_hWnd, rect, NULL, ES_LEFT | ES_AUTOHSCROLL | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE, WS_EX_CLIENTEDGE | WS_EX_LEFT | WS_EX_LTRREADING); 

There is a style for horizontal alignment like ES_CENTER for horizontal alignment of text, but is it possible to center the text in the center?

+7
c ++ mfc
source share
2 answers

Here is a class that inherits CEdit and allows vertical alignment

 /// HEADER ////////////////////////////////////////// class CEditVC : public CEdit { public: CEditVC(); protected: CRect m_rectNCBottom; CRect m_rectNCTop; public: virtual ~CEditVC(); protected: afx_msg void OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp); afx_msg void OnNcPaint(); afx_msg UINT OnGetDlgCode(); DECLARE_MESSAGE_MAP() }; /// IMPLEMENTATION ///////////////////////////////////////// CEditVC::CEditVC() : m_rectNCBottom(0, 0, 0, 0) , m_rectNCTop(0, 0, 0, 0) { } CEditVC::~CEditVC() { } BEGIN_MESSAGE_MAP(CEditVC, CEdit) ON_WM_NCCALCSIZE() ON_WM_NCPAINT() ON_WM_GETDLGCODE() END_MESSAGE_MAP() void CEditVC::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp) { CRect rectWnd, rectClient; //calculate client area height needed for a font CFont *pFont = GetFont(); CRect rectText; rectText.SetRectEmpty(); CDC *pDC = GetDC(); CFont *pOld = pDC->SelectObject(pFont); pDC->DrawText("Ky", rectText, DT_CALCRECT | DT_LEFT); UINT uiVClientHeight = rectText.Height(); pDC->SelectObject(pOld); ReleaseDC(pDC); //calculate NC area to center text. GetClientRect(rectClient); GetWindowRect(rectWnd); ClientToScreen(rectClient); UINT uiCenterOffset = (rectClient.Height() - uiVClientHeight) / 2; UINT uiCY = (rectWnd.Height() - rectClient.Height()) / 2; UINT uiCX = (rectWnd.Width() - rectClient.Width()) / 2; rectWnd.OffsetRect(-rectWnd.left, -rectWnd.top); m_rectNCTop = rectWnd; m_rectNCTop.DeflateRect(uiCX, uiCY, uiCX, uiCenterOffset + uiVClientHeight + uiCY); m_rectNCBottom = rectWnd; m_rectNCBottom.DeflateRect(uiCX, uiCenterOffset + uiVClientHeight + uiCY, uiCX, uiCY); lpncsp->rgrc[0].top +=uiCenterOffset; lpncsp->rgrc[0].bottom -= uiCenterOffset; lpncsp->rgrc[0].left +=uiCX; lpncsp->rgrc[0].right -= uiCY; } void CEditVC::OnNcPaint() { Default(); CWindowDC dc(this); CBrush Brush(GetSysColor(COLOR_WINDOW)); dc.FillRect(m_rectNCBottom, &Brush); dc.FillRect(m_rectNCTop, &Brush); } UINT CEditVC::OnGetDlgCode() { if(m_rectNCTop.IsRectEmpty()) { SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOMOVE | SWP_FRAMECHANGED); } return CEdit::OnGetDlgCode(); } 
+6
source share

this is impossible, as far as I know. But you can exercise your own control to make this possible.

Check out this article .

Hope this helps.

By the way, this is a question that he deserves for google before publishing. You can find many such things already on the Internet.

0
source share

All Articles