검색을 해 봐도 strlen에 묻혀 문자열을 줄여주는 소스 구하기가 쉽지만은 안네요.^^;
많은 분들에게 이 글이 도움이 되었으면 좋겠습니다.

본 소스는 문자열 길이를 픽셀단위로 줄여주는 기능을 가집니다.
예를 들어,
보통 리스트 컨트롤에 칼럼의 Width가 문자열의 Width보다 작으면, "..."으로
줄여서 보여주는 기능과 비슷합니다.

//////////////////////////////////////////////////////////////////////////////////////
// HWND hWndText : 문자열이 출력될 객체의 핸들(GetDlgItem()으로 나오는 핸들값)
// char *szInText    : 소스 문자열
// char *szOutText  : 줄인 후의 문자열
// int nPixelLength   : 결과를 얻고자 하는 픽셀 단위의 문자열 길이("..."을 포함한 길이입니다.)
// GetTextExtentPoint32는 문자열단위, GetCharWidth32는 문자단위로 길이를 알 수 있습니다.
//////////////////////////////////////////////////////////////////////////////////////
void GetShortString(HWND hWndText, char *szInText, char *szOutText, int nPixelLength)
{
    HDC hdcStatic = ::GetDC(hWndText);
    SIZE szBox, szShort;
    int i;

    GetTextExtentPoint32(hdcStatic, ".", 1, &szShort);
    nPixelLength -= szShort.cx*3;
   
    for(i=0; i<(int)strlen(szInText); i++)
    {
        //GetCharWidth32(hdcStatic,szInText[i],szInText[i],&nLength);
        GetTextExtentPoint32(hdcStatic, szInText, i+1, &szBox);
        if(szBox.cx >= nPixelLength)
            break;
    }
   
    strncpy(szOutText, szInText, i);
    szOutText[i] = 0;

    while(1)
    {
        GetTextExtentPoint32(hdcStatic, szOutText, strlen(szOutText), &szBox);
        if(nPixelLength > szBox.cx+szShort.cx)
            strcat(szOutText, " ");
        else
            break;
    }

    strcat(szOutText, "...");

    if(hWndText && hdcStatic)
    {
        ::ReleaseDC(hWndText, hdcStatic);
    }
}


'프로그래밍 팁 > etc' 카테고리의 다른 글

IP주소값 가져오기  (0) 2010.03.29
윈도우 제공 API를 이용한 암호화  (0) 2010.03.25
IP 차단하기(방화벽)  (0) 2010.01.31
압축 소스(ZIP, ZLIB)  (4) 2010.01.28
콘솔 출력 내용 메모리에 담기  (0) 2010.01.28
Posted by 띠깜
,