SAMPLE: Changing Text Alignment in an Edit Control Dynamiclly

Last reviewed: February 15, 1996
Article ID: Q66942
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.0 and 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

A Microsoft Windows edit control aligns its contents to the left or right margins, or centers its contents, depending on the window style of the control. The control styles ES_LEFT, ES_CENTER, and ES_RIGHT specify left-, center-, and right-alignment, respectively.

Only multiline edit controls can be right-aligned or centered. Single-line edit controls are always left-aligned, regardless of the control style given.

Windows does not support altering the alignment style of a multiline edit control after it has been created. However, there are two methods that you can use to cause a multiline edit control in a dialog box to appear to change alignment. Note that in each of these methods, the dialog box that contains the control must be created with the DS_LOCALEDIT style.

MORE INFORMATION

The first method applies to all platforms. The second method does not apply to Windows 95. Under Windows 95, EM_SETHANDlE and EM_GETHANDLE are not supported. For more information, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q130759
   TITLE     : EM_SETHANDLE and EM_GETHANDLE Messages Not Supported

Method 1

Create three controls: one left-aligned, one centered, and one right- aligned. Each has the same dimensions and position in the dialog box, but only one is initially made visible.

When the alignment is to change, call ShowWindow() to hide the visible control and to make one of the other controls visible.

To keep the text identical in all three controls, use the EM_GETHANDLE and EM_SETHANDLE messages to share the same memory among all three controls.

Method 2

Initially create a single control. When the text alignment is to change, retrieve location, size, and style bits for the existing edit control. Create a new control with the same size and in the same location, but change the style bits to reflect the new alignment.

Send the EM_GETHANDLE to each control to retrieve a handle to the memory that stores the contents. Send an EM_SETHANDLE to each control to exchange the memory used by each. Finally, destroy the original control.

There is a sample application named EDALIGN in the Microsoft Software Library that demonstrates each of these methods. Note, however, that this sample is a Windows 3.1 sample only.

Download EDALIGN.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download EDALIGN.EXE (size: 21720 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the \SOFTLIB\MSLFILES directory
          Get EDALIGN.EXE (size: 21720 bytes) 
    


Additional reference words: 3.00 3.10 3.50 4.00 95 EDALIGN.EXE
KBCategory: kbui kbfile
KBSubcategory: UsrCtl


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 15, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.