Professional UI Solutions
Site Map   /  Register
 
 

Forum

Please Log In to post a new message or reply to an existing one. If you are not registered, please register.

NOTE: Some forums may be read-only if you are not currently subscribed to our technical support services.

Forums » Prof-UIS Tech Support » Centering CExtHyperLinkButton text? Collapse All
Subject Author Date
Soo Wei Tan Feb 19, 2007 - 12:26 PM

How do I center the text displayed in a CExtHyperLinkButton? Also, is it possible to make the text multiline? I tried doing this in the VS2005 dialog editor, but the changes don’t appear when running the app.

Technical Support Feb 20, 2007 - 3:22 AM

Thank you for reporting the problem. It is already fixed. You can download the latest code from our ftp site or update the CExtHyperLinkButton::OnPaintHyperLink() method in this way:

void CExtHyperLinkButton::OnPaintHyperLink(
    CDC & dc,
    CRect rcText,
    bool bVisited,
    bool bEnabled,
    bool bDrawFocusRect,
    bool bFocus,
    bool bHover,
    bool bPushed
    ) const
{
    ASSERT_VALID( this );
    ASSERT( dc.GetSafeHdc() != NULL );
CExtSafeString strHyperLinkURL = OnQueryHyperLinkURL( true );
    if( strHyperLinkURL.IsEmpty() )
        return;
CRect rcInitial( rcText );
int nOldBkMode = dc.SetBkMode( TRANSPARENT );

HFONT hFont = OnQueryFont();
    if( ! PmBridge_GetPM()->AdjustControlFont(
            hFont,
            (CObject*)this
            )
        )
    {
        LOGFONT _lf;
        ::memset( &_lf, 0, sizeof(LOGFONT) );
        ::GetObject( (HGDIOBJ)hFont, sizeof(LOGFONT), (LPVOID) &_lf );
        if( m_nButtonType == BS_DEFPUSHBUTTON )
            _lf.lfWeight = 
                (_lf.lfWeight > FW_BOLD) 
                    ? _lf.lfWeight 
                    : FW_BOLD;
        if(        ( bHover /*|| bFocus*/ || bPushed )
            &&    GetTextUnderline()
            )
            _lf.lfUnderline = 1;
        hFont = ::CreateFontIndirect( &_lf );
    }
    ASSERT( hFont != NULL );
CFont _fontDestructor;
    _fontDestructor.Attach( hFont );

CFont * pFont = CFont::FromHandle( hFont ); CFont * pFontOld = dc.SelectObject( pFont );

COLORREF clrText =
            OnQueryHyperLinkTextColor(
                bVisited,
                bEnabled,
                bDrawFocusRect,
                bFocus,
                bHover,
                bPushed
                );
COLORREF clrTextOld = dc.SetTextColor( clrText );

INT nTextLength = strHyperLinkURL.GetLength();

CRect rcTextLocation( 0, 0, 0, 0);
    rcText.DeflateRect( 2, 2 );

DWORD dwStyle = GetStyle();
bool bMultiline = (dwStyle&BS_MULTILINE) != 0 ? true : false;

INT nAlign = 0;
const UINT __BS_H_MASK = (BS_LEFT|BS_CENTER|BS_RIGHT); const UINT __BS_V_MASK = (BS_TOP|BS_VCENTER|BS_BOTTOM);
    if( (dwStyle&__BS_H_MASK) == BS_RIGHT )
        nAlign |= CExtPaintManager::__ALIGN_HORIZ_RIGHT;
    else if( (dwStyle&__BS_H_MASK) == BS_LEFT )
        nAlign |= CExtPaintManager::__ALIGN_HORIZ_LEFT;
    else
        nAlign |= CExtPaintManager::__ALIGN_HORIZ_CENTER;
    
    if( (dwStyle&__BS_V_MASK) == BS_BOTTOM )
        nAlign |= CExtPaintManager::__ALIGN_VERT_BOTTOM;
    else if( (dwStyle&__BS_V_MASK) == BS_TOP )
        nAlign |= CExtPaintManager::__ALIGN_VERT_TOP;
    else
        nAlign |= CExtPaintManager::__ALIGN_VERT_CENTER;

UINT nDtMeasureFlags =
        DT_LEFT|DT_TOP|DT_CALCRECT;

    if( bMultiline )
    {
        rcTextLocation = rcText;
        rcTextLocation.OffsetRect( -rcTextLocation.TopLeft() );
        rcTextLocation.bottom = rcTextLocation.top;
        nDtMeasureFlags |= DT_WORDBREAK;
    }
    else
        nDtMeasureFlags |= DT_SINGLELINE;

    dc.DrawText(
        LPCTSTR( strHyperLinkURL ), 
        nTextLength,
        rcTextLocation,
        nDtMeasureFlags
        );
    rcTextLocation.OffsetRect(
        rcText.TopLeft() - rcTextLocation.TopLeft()
        );
    
    UINT nDtDrawFlags = 0;
    if( (nAlign&CExtPaintManager::__ALIGN_HORIZ_MASK) == CExtPaintManager::__ALIGN_HORIZ_RIGHT )
    {
        nDtDrawFlags |= DT_RIGHT;
        rcTextLocation.OffsetRect(
            rcText.Width() - rcTextLocation.Width(),
            0
            );
    }
    else if( (nAlign&CExtPaintManager::__ALIGN_HORIZ_MASK) == CExtPaintManager::__ALIGN_HORIZ_CENTER )
    {
        nDtDrawFlags |= DT_CENTER;
        rcTextLocation.OffsetRect(
            ( rcText.Width() - rcTextLocation.Width() ) / 2,
            0
            );
    }
    else
        nDtDrawFlags |= DT_LEFT;
    
    if( (nAlign&CExtPaintManager::__ALIGN_VERT_MASK) == CExtPaintManager::__ALIGN_VERT_BOTTOM )
    {
        nDtDrawFlags |= DT_BOTTOM;
        rcTextLocation.OffsetRect(
            0,
            rcText.Height() - rcTextLocation.Height()
            );
    }
    else if( (nAlign&CExtPaintManager::__ALIGN_VERT_MASK) == CExtPaintManager::__ALIGN_VERT_TOP )
    {
        nDtDrawFlags |= DT_TOP;
    }
    else
    {
        nDtDrawFlags |= DT_VCENTER;
        rcTextLocation.OffsetRect(
            0,
            ( rcText.Height() - rcTextLocation.Height() ) / 2
            );
    }

    if( bMultiline )
        nDtDrawFlags |= DT_WORDBREAK;
    else
        nDtDrawFlags |= DT_SINGLELINE;

    dc.DrawText(
        LPCTSTR( strHyperLinkURL ),
        nTextLength,
        rcTextLocation,
        nDtDrawFlags
        );

CRect rcFocus( rcTextLocation );
    rcFocus.InflateRect( 2, 2, 2, 1 );        

    dc.SetTextColor( clrTextOld );
    dc.SelectObject( pFontOld );
    dc.SetBkMode( nOldBkMode );

    if( bDrawFocusRect && bFocus )
        dc.DrawFocusRect( &rcFocus );
}