Vertical Text Centering in an Edit Control

I have come across many posts asking about the ability to center text vertically in a simple, single-line edit control. Therefore, I decided to sit down and write a code that demonstrates how to achieve this. I have used a WM_NCCALCSIZE message to adjust the NC area.

Because a control in a dialog is subclassed, all messages needed to create a window and calculate window areas were already handled before subclassing occurs. To force the WM_NCCALCSIZE NC area, I decided to call SetWindowPos once, from the WM_CTLCOLOR reflection message handler (WM_CTLCOLOR_REFLECT) to establish new NC area dimensions.

WM_CTLCOLOR is received by the control after subclassing, before the control is visible. You can use any other message handler to call SetWindowPos; make sure it occurs before the control is shown. Of course, because the NC area has changed, I had to handle WM_NCPAINT to paint a new NC area that the edit control does not handle because it has no idea about its existence whatsoever.

For that, I have added two member variables of the CRect type that hold calculated bottom and top rectangles of the new NC area after calculating the client area height that is needed to accommodate the font. Computation of the new NC is handled in the WM_NCCALCSIZE handler. Because the control is not resized, this computation has to be performed once only. In WM_NCPAINT, I simply use rectangles to tell DC what to fill with a color of the default system brush, so be careful when changing background color.

To use this class:

  1. Copy the CVEdit header and implementation file to your project directory (or central repository of your custom classes).
  2. Include both files in a project. Delete the .clw file and invoke a class wizard to re-create it.
  3. In the dialog containing the edit control, use the wizard to subclass the control (add member variable). Make sure you choose the control type and CVEdit class from the combo.

THIS CODE IS DESIGNED TO WORK WITH A SINGLE-LINE EDIT CONTROL. It just illustrates how to use WM_NCCALCSIZE to adjust the vertical margins of the edit control. Modify it to your needs.



About the Author

John Z. Czopowik VC++ MVP

Microsoft VC++ MVP

Downloads

Comments

  • One minor suggestion.

    Posted by DennisWoo on 10/30/2004 11:02am

    Hi John, I have made a small change to the code to handle brush color when the edit control is in "Read only" mode. Here is the line modified in the CVEdit::OnNcPaint() function: CBrush Brush = (this->GetStyle() & ES_READONLY) == ES_READONLY ? GetSysColor(COLOR_BTNFACE) : GetSysColor(COLOR_WINDOW) ;

    • Re: One minor suggestion.

      Posted by JohnCz on 10/30/2004 11:25am

      I think this is very good suggestion. Again: This article (as I have indicated) is dedicated only to showing how vertically center text in a single edit control. It was intended only for that purpose, not for developing a class for new control. This small project is open for any suggestions/addition to make it such if needed.

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds