Microsoft HomeproductssearchsupportshopWrite Us   Microsoft Home
Magazine
 |  Community
 |  Workshop
 |  Tools & Samples
 |  Training
 |  Site Info

Workshop

How to Break Text Based on Locale


Different languages might have different rules for hyphenation, spacing, and where a line should be broken on a page. For this reason, the MLang IMLangLineBreakConsole interface provides a locale-aware line-breaking functionality for console-based applications.

To use this feature to break a Unicode string for output:

  1. Make sure the Component Object Library has been initialized.

    Before you can use any of the functionality provided by MLang, you must first initialize the Component Object Library through a call to CoInitialize. Every call to CoInitialize must be accompanied by a call to CoUninitialize when the application terminates. CoUninitialize ensures that the application does not quit until it has received all of its pending messages.

  2. Obtain a pointer to an IMLangLineBreakConsole interface.

    The IMLangLineBreakConsole interface is part of the MultiLanguage Object. If no such object exists, you must create one and obtain a pointer to the corresponding IMultiLanguage interface through a call to CoCreateInstance. Once this has been accomplished, call QueryInterface through IMultiLanguage for a pointer to an IMLangLineBreakConsole interface.

  3. Use the IMLangLineBreakConsole methods to break the string into the proper number of columns for output.

    For Unicode strings, use BreakLineW. For multibyte strings, use BreakLineA. The following code sample breaks the null-terminated Unicode string pwszStr, based on the constant NUMCOLUMNS and the given locale, and prints it to the screen.

    // pwszStr - null-terminated Unicode string.
    // locale - locale identifier of the output string.
    // pMLLBC - pointer to an IMLangLineBreakConsole interface.
    
    long cchSize = wcslen(pwszStr);
        // Size of pwszStr in characters.
    int MaxColumns = NUMCOLUMNS;
        // Desired number of columns for output.
    long cchLine;
        // Number of characters to output in the current line.
    long cchSkip;
        // Number of characters to skip before starting the next line.
    long totalLine = 0;
       // Total number of characters processed.
    
    WCHAR wStr[NUMCOLUMNS] = "";
    HRESULT hr = S_OK;
    while((totalLine < cchSize) && SUCCEEDED(hr))
        // Process the entire string unless an error occurs.
    {    
        hr = pMLLBC->BreakLineW(locale, pwszStr + totalLine,
            cchSize - totalLine, MaxColumns, &cchLine, &cchSkip);
        wcsncpy(wStr, cchSkip + totalLine, cchLine);
            // Copy characters of pwszStr for output.
        wStr[cchLine] = L'\0';
        totalLine = totalLine + cchLine + cchSkip;
            // Increase the total number of characters processed. 
        wprintf(L"%s\n", wStr);
            // Output the string to the console-based application.
    }
    
  4. Remember to release the interfaces and uninitialize the Component Object Library before your program terminates.

The following link provides more information about the operations described in this article.


Does this content meet your programming needs? Write us!

Back to topBack to top

© 1998 Microsoft Corporation. All rights reserved. Terms of use.

 

Magazine Home
Ask Jane
DHTML Dude
Extreme XML
For Starters
More or Hess
Servin' It Up
Site Lights
Web Men Talking
Member Community Home
Benefits: Freebies & Discounts
Benefits: Promote Your Site
Benefits: Connect with Your Peers
Benefits at a Glance
Online Special-Interest Groups
Your Membership
SBN Stores
Join Now
Workshop Home
Essentials
Content & Component Delivery
Component Development
Data Access & Databases
Design
DHTML, HTML & CSS
Extensible Markup Language (XML)
Languages & Development Tools
Messaging & Collaboration
Networking, Protocols & Data Formats
Reusing Browser Technology
Security & Cryptography
Server Technologies
Streaming & Interactive Media
Web Content Management
Workshop Index
Tools & Samples Home
Tools
Samples, Headers, Libs
Images
Sounds
Style Sheets
Web Fonts
Training Home
SBN Live Seminars
SBN Live Chats
Courses
Peer Support
CD-ROM Training
Books & Training Kits
Certification
SBN Home
New to SBN?
What's New on SBN
Site Map
Site Search
Glossary
Write Us
About This Site