|
|
Comments and Discussions
|
|
 |

|
kbomb987's scrollbar addition is a fine improvement to this control, but I wanted to be able to properly calculate the RECT of the text, based on font size, etc., existing in the control so I could accurately size the scrollbar. The net effect is the scrollbar will now scroll to the very end of the text, without any extra space.
Undoubtedly there could be further improvements or refinements to what I have done.
In XColorStatic.h, change the function declaration of ResetScrollBar to:
void ResetScrollBar(CRect* a_pMainRect, CRect* a_pWithTextRect);
Change the method definition in XColorStatic.cpp to be:
void CXColorStatic::ResetScrollBar(CRect* a_pMainRect, CRect* a_pWithTextRect)
{
// Need for scrollbars?
if(a_pMainRect->Height() > a_pWithTextRect->Height())
{
ShowScrollBar( SB_VERT, FALSE ); // Hide it
SetScrollPos( SB_VERT, 0 );
}
else
{
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_PAGE | SIF_RANGE;
si.nPage = a_pMainRect->Height();
si.nMax = a_pWithTextRect->Height();
si.nMin = 0 ;
SetScrollInfo(SB_VERT, &si);
EnableScrollBarCtrl( SB_VERT, TRUE );
}
}
In XColorStatic.cpp, replace OnPaint() with the following (I have included the previous conributions of David Fleming's tab handling and kbomb987's scrollbar functionality):
void CXColorStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.SaveDC();
dc.SetTextColor(m_rgbText);
dc.SetBkColor(m_rgbBackground);
dc.SetBkMode(OPAQUE);
dc.SelectObject(m_pBrush);
CRect rect;
GetClientRect(rect);
// cannot have both an icon and text
if (m_hIcon)
{
int nIconX = ::GetSystemMetrics(SM_CXICON);
int nIconY = ::GetSystemMetrics(SM_CYICON);
rect.left = rect.left + (rect.Width() - nIconX) / 2;
rect.top = rect.top + (rect.Height() - nIconY) / 2;
dc.DrawIcon(rect.left, rect.top, m_hIcon);
}
else
{
dc.SelectObject(&m_font);
// get static's text
CString strText = _T("");
GetWindowText(strText);
UINT nFormat = 0;
DWORD dwStyle = GetStyle();
// set DrawText format from static style settings
if (dwStyle & SS_CENTER)
nFormat |= DT_CENTER;
else if (dwStyle & SS_LEFT)
nFormat |= DT_LEFT;
else if (dwStyle & SS_RIGHT)
nFormat |= DT_RIGHT;
if (dwStyle & SS_CENTERIMAGE) // vertical centering ==> single line only
nFormat |= DT_VCENTER | DT_SINGLELINE;
else
nFormat |= DT_WORDBREAK;
// Added to expand tabs...
if(strText.Find(_T('\t')) != -1)
nFormat |= DT_EXPANDTABS;
//
// BEGIN SCROLL CODE
//
// Create a rect above our target rect that will not allow drawing to.
// We will make 200 pixels above our target non-drawing. That should give us enough
// room to scroll text upwards.
RECT excluderect;
excluderect.top = rect.top - 200;
excluderect.bottom = rect.top;
excluderect.left = rect.left;
excluderect.right = rect.right;
dc.ExcludeClipRect(&excluderect);
// Draw the text accounting for any scrolling of the scroll bar.
rect.top -= GetScrollPos( SB_VERT );
//
// END SCROLL CODE
//
rect.left += m_nXMargin;
rect.top += m_nYMargin;
dc.DrawText(strText, rect, nFormat);
CRect rWithTextRect, rMainRect;
GetClientRect(&rMainRect);
rWithTextRect = rMainRect;
dc.DrawText(strText, &rWithTextRect, DT_CALCRECT | DT_WORDBREAK);
ResetScrollBar(&rMainRect, &rWithTextRect);
}
dc.RestoreDC(-1);
}
I hope this is a useful addition to this control.
Best regards to the Code Project Community,
-Lee
|
|
|
|

|
Thanks to Hans for an excellent Static Control.
Transparency is a goal in many of my demo type Dialog Apps.
The previous solution works, but requires too much knownledge of the background in some cases.
Here's a simple solution to have the text draw over the backgound that is already there.
To turn on Transparency for the Static control, use the Dialog editor to set the Extended Style checkbox for Transparency to ON -- leaving it Off defaults to the normal Opaque text with solid background.
First in OnPaint, the top of the member function will now look like:
void CXColorStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.SaveDC();
dc.SetTextColor(m_rgbText);
if(!(GetExStyle() & WS_EX_TRANSPARENT))
{
dc.SetBkColor(m_rgbBackground);
dc.SetBkMode(OPAQUE);
}
else
{
dc.SetBkMode(TRANSPARENT);
}
dc.SelectObject(m_pBrush);
CRect rect;
GetClientRect(rect);
...(rest is the same as before)
Second in EraseBackground:
BOOL CXColorStatic::OnEraseBkgnd(CDC* pDC)
{
if(!(GetExStyle() & WS_EX_TRANSPARENT))
{
CRect cr;
GetClientRect(cr);
pDC->FillRect(&cr, m_pBrush);
}
return TRUE; //CStatic::OnEraseBkgnd(pDC);
}
Wayne L. Burgess
TEN-K Computer Services Inc.
|
|
|
|
|
|

|
Everything seems to work great, and I was also able to modify it to allow for transparency (really easy).
However, I have noticed that it does not display tabs correctly (\t escape sequence). They are displayed OK in a normal CStatic, but appear as a box or other sort of character when using this class.
Any thoughts on why and how to fix it?
Thanks.
|
|
|
|

|
I found the solution already. It happens to be in another article in this section (the one about creating a CStatic-derived class to mimic VB's Label (but I don't have the link handy).
Anyway, just add this snippet in the OnPaint function just after the other style flags settings:
if(strText.Find(_T('\t')) != -1)
nFormat |= DT_EXPANDTABS;
It worked for me.
|
|
|
|

|
Here is how to add vertical scroll bars to the static control.
Replace your XColorStatic.h and XColorStatic.cpp with the following code below. Note the new functions added ResetScrollBars(), WindowProc(), OnVScroll() to handle the scroll bar. The actual drawing of the scrolled text occurs within OnPaint() and is commented by // BEGIN SCROLL CODE and // END SCROLL CODE. You can tweak the hardcoded values for amount of scrolling and size of clipping region inside OnPaint(). Have fun!
// XColorStatic.h Version 1.0
//
// Author: Hans Dietrich
// hdietrich2@hotmail.com
//
// This software is released into the public domain.
// You are free to use it in any way you like.
//
// This software is provided "as is" with no expressed
// or implied warranty. I accept no liability for any
// damage or loss of business that this software may cause.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef XCOLORSTATIC_H
#define XCOLORSTATIC_H
/////////////////////////////////////////////////////////////////////////////
// CXColorStatic window
class CXColorStatic : public CStatic
{
private:
int m_iLineHeight;
// Construction
public:
CXColorStatic();
virtual ~CXColorStatic();
// Attributes
public:
void SetBackgroundColor(COLORREF rgb, BOOL bRedraw = TRUE);
void SetTextColor(COLORREF rgb, BOOL bRedraw = TRUE);
void SetBold(BOOL bFlag, BOOL bRedraw = TRUE);
void SetFont(LPCTSTR lpszFaceName, int nPointSize, BOOL bRedraw = TRUE);
void SetFont(LOGFONT * pLogFont, BOOL bRedraw = TRUE);
void SetFont(CFont *pFont, BOOL bRedraw = TRUE);
void SetIcon(HICON hIcon, BOOL bRedraw = TRUE);
void SetMargins(int x, int y) { m_nXMargin = x; m_nYMargin = y; }
void ShowText(CString strText);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CXColorStatic)
protected:
virtual void PreSubclassWindow();
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
//}}AFX_VIRTUAL
// Implementation
protected:
CFont m_font;
COLORREF m_rgbText;
COLORREF m_rgbBackground;
CBrush * m_pBrush;
BOOL m_bBold;
int m_nXMargin, m_nYMargin;
HICON m_hIcon;
// Generated message map functions
void ResetScrollBar();
protected:
//{{AFX_MSG(CXColorStatic)
afx_msg void OnPaint();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif //XCOLORSTATIC_H
//////////////////////////////////////////////////////////////
// XColorStatic.cpp
//////////////////////////////////////////////////////////////
// XColorStatic.cpp Version 1.0
//
// Author: Hans Dietrich
// hdietrich2@hotmail.com
//
// This software is released into the public domain.
// You are free to use it in any way you like.
//
// This software is provided "as is" with no expressed
// or implied warranty. I accept no liability for any
// damage or loss of business that this software may cause.
//
///////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "XColorStatic.h"
#include "FontSize.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////////////
// CXColorStatic
BEGIN_MESSAGE_MAP(CXColorStatic, CStatic)
//{{AFX_MSG_MAP(CXColorStatic)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_VSCROLL()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////////
// ctor
CXColorStatic::CXColorStatic()
{
m_rgbText = GetSysColor(COLOR_BTNTEXT);
m_rgbBackground = GetSysColor(COLOR_BTNFACE);
m_pBrush = new CBrush(m_rgbBackground);
m_bBold = FALSE;
m_hIcon = NULL;
m_nXMargin = m_nYMargin = 0;
// The amount of scroll to make when clicking the UP or DOWN arrows on the scroll bar.
m_iLineHeight = 8;
}
///////////////////////////////////////////////////////////////////////////////
// dtor
CXColorStatic::~CXColorStatic()
{
TRACE(_T("in CXColorStatic::~CXColorStatic\n"));
if (m_font.GetSafeHandle())
m_font.DeleteObject();
if (m_pBrush)
{
m_pBrush->DeleteObject();
delete m_pBrush;
}
m_pBrush = NULL;
}
///////////////////////////////////////////////////////////////////////////////
// PreSubclassWindow
void CXColorStatic::PreSubclassWindow()
{
TRACE(_T("in CXColorStatic::PreSubclassWindow\n"));
// get current font
CFont* pFont = GetFont();
if (!pFont)
{
HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
if (hFont == NULL)
hFont = (HFONT) GetStockObject(ANSI_VAR_FONT);
if (hFont)
pFont = CFont::FromHandle(hFont);
}
ASSERT(pFont);
ASSERT(pFont->GetSafeHandle());
// create the font for this control
LOGFONT lf;
pFont->GetLogFont(&lf);
m_font.CreateFontIndirect(&lf);
}
///////////////////////////////////////////////////////////////////////////////
// OnPaint
void CXColorStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.SaveDC();
dc.SetTextColor(m_rgbText);
dc.SetBkColor(m_rgbBackground);
dc.SetBkMode(OPAQUE);
dc.SelectObject(m_pBrush);
CRect rect;
GetClientRect(rect);
// cannot have both an icon and text
if (m_hIcon)
{
int nIconX = ::GetSystemMetrics(SM_CXICON);
int nIconY = ::GetSystemMetrics(SM_CYICON);
rect.left = rect.left + (rect.Width() - nIconX) / 2;
rect.top = rect.top + (rect.Height() - nIconY) / 2;
dc.DrawIcon(rect.left, rect.top, m_hIcon);
}
else
{
dc.SelectObject(&m_font);
// get static's text
CString strText = _T("");
GetWindowText(strText);
UINT nFormat = 0;
DWORD dwStyle = GetStyle();
// set DrawText format from static style settings
if (dwStyle & SS_CENTER)
nFormat |= DT_CENTER;
else if (dwStyle & SS_LEFT)
nFormat |= DT_LEFT;
else if (dwStyle & SS_RIGHT)
nFormat |= DT_RIGHT;
if (dwStyle & SS_CENTERIMAGE) // vertical centering ==> single line only
nFormat |= DT_VCENTER | DT_SINGLELINE;
else
nFormat |= DT_WORDBREAK;
//
// BEGIN SCROLL CODE
//
// Create a rect above our target rect that will not allow drawing to.
// We will make 200 pixels above our target non-drawing. That should give us enough
// room to scroll text upwards.
RECT excluderect;
excluderect.top = rect.top - 200;
excluderect.bottom = rect.top;
excluderect.left = rect.left;
excluderect.right = rect.right;
dc.ExcludeClipRect(&excluderect);
// Draw the text accounting for any scrolling of the scroll bar.
rect.top -= GetScrollPos( SB_VERT );
//
// END SCROLL CODE
//
rect.left += m_nXMargin;
rect.top += m_nYMargin;
dc.DrawText(strText, rect, nFormat);
}
dc.RestoreDC(-1);
ResetScrollBar();
}
///////////////////////////////////////////////////////////////////////////////
// OnEraseBkgnd
BOOL CXColorStatic::OnEraseBkgnd(CDC* pDC)
{
CRect cr;
GetClientRect(cr);
pDC->FillRect(&cr, m_pBrush);
return TRUE; //CStatic::OnEraseBkgnd(pDC);
}
///////////////////////////////////////////////////////////////////////////////
// SetFont
void CXColorStatic::SetFont(LOGFONT *pLogFont, BOOL bRedraw /*= TRUE*/)
{
ASSERT(pLogFont);
if (!pLogFont)
return;
if (m_font.GetSafeHandle())
m_font.DeleteObject();
LOGFONT lf = *pLogFont;
lf.lfWeight = m_bBold ? FW_BOLD : FW_NORMAL;
m_font.CreateFontIndirect(&lf);
if (bRedraw)
RedrawWindow();
}
///////////////////////////////////////////////////////////////////////////////
// SetFont
void CXColorStatic::SetFont(LPCTSTR lpszFaceName,
int nPointSize,
BOOL bRedraw /*= TRUE*/)
{
// null face name is ok - we will use current font
LOGFONT lf;
memset(&lf, 0, sizeof(lf));
if (lpszFaceName == NULL)
{
CFont *pFont = GetFont();
ASSERT(pFont);
pFont->GetLogFont(&lf);
}
else
{
_tcsncpy(lf.lfFaceName, lpszFaceName, sizeof(lf.lfFaceName)/sizeof(TCHAR)-1);
}
lf.lfHeight = GetFontHeight(nPointSize);
SetFont(&lf, bRedraw);
}
///////////////////////////////////////////////////////////////////////////////
// SetFont
void CXColorStatic::SetFont(CFont *pFont, BOOL bRedraw /*= TRUE*/)
{
ASSERT(pFont);
if (!pFont)
return;
LOGFONT lf;
memset(&lf, 0, sizeof(lf));
pFont->GetLogFont(&lf);
SetFont(&lf, bRedraw);
}
///////////////////////////////////////////////////////////////////////////////
// SetTextColor
void CXColorStatic::SetTextColor(COLORREF rgb, BOOL bRedraw /*= TRUE*/)
{
m_rgbText = rgb;
if (bRedraw)
RedrawWindow();
}
///////////////////////////////////////////////////////////////////////////////
// SetBold
void CXColorStatic::SetBold(BOOL bFlag, BOOL bRedraw /*= TRUE*/)
{
m_bBold = bFlag;
LOGFONT lf;
memset(&lf, 0, sizeof(lf));
CFont *pFont = GetFont();
ASSERT(pFont);
pFont->GetLogFont(&lf);
lf.lfWeight = m_bBold ? FW_BOLD : FW_NORMAL;
SetFont(&lf, bRedraw);
}
///////////////////////////////////////////////////////////////////////////////
// SetBackgroundColor
void CXColorStatic::SetBackgroundColor(COLORREF rgb, BOOL bRedraw /*= TRUE*/)
{
m_rgbBackground = rgb;
if (m_pBrush)
{
m_pBrush->DeleteObject();
delete m_pBrush;
}
m_pBrush = new CBrush(m_rgbBackground);
if (bRedraw)
RedrawWindow();
}
///////////////////////////////////////////////////////////////////////////////
// SetIcon
void CXColorStatic::SetIcon(HICON hIcon, BOOL bRedraw /*= TRUE*/)
{
ASSERT(hIcon);
m_hIcon = hIcon;
if (bRedraw)
RedrawWindow();
}
void CXColorStatic::ShowText(CString strText)
{
SetWindowText(strText);
RedrawWindow();
// Reset the scroll bar position back to 0.
SetScrollPos(SB_VERT, 0);
}
void CXColorStatic::ResetScrollBar()
{
CRect rFrame;
GetClientRect( rFrame );
// Need for scrollbars?
if( 0 )//rFrame.Height() > m_iDocHeight + 8 )
{
ShowScrollBar( SB_VERT, FALSE ); // Hide it
SetScrollPos( SB_VERT, 0 );
}
else
{
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_PAGE | SIF_RANGE;
si.nPage = rFrame.Height();
si.nMax = 500;//m_iDocHeight + 8;
si.nMin = 0 ;
SetScrollInfo(SB_VERT, &si);
EnableScrollBarCtrl( SB_VERT, TRUE );
}
}
void CXColorStatic::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int iScrollBarPos = GetScrollPos( SB_VERT );
CRect rFrame;
GetClientRect( rFrame );
switch( nSBCode )
{
case SB_LINEUP:
iScrollBarPos = max( iScrollBarPos - m_iLineHeight, 0 );
break;
case SB_LINEDOWN:
iScrollBarPos = min( iScrollBarPos + m_iLineHeight,
GetScrollLimit( SB_VERT ) );
break;
case SB_PAGEUP:
iScrollBarPos = max( iScrollBarPos - rFrame.Height(), 0 );
break;
case SB_PAGEDOWN:
iScrollBarPos = min( iScrollBarPos + rFrame.Height(),
GetScrollLimit( SB_VERT ) );
break;
case SB_THUMBTRACK:
case SB_THUMBPOSITION:
iScrollBarPos = nPos;
break;
}
SetScrollPos( SB_VERT, iScrollBarPos );
Invalidate();
}
LRESULT CXColorStatic::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if( message == WM_NCHITTEST || message == WM_NCLBUTTONDOWN ||
message == WM_NCLBUTTONDBLCLK )
return ::DefWindowProc( m_hWnd, message, wParam, lParam );
return CStatic::WindowProc(message, wParam, lParam);
}
|
|
|
|

|
in my formview derived resource, i used ur classfor dipslay the current time.
i used the SetWindowText(), but the new text appears as in small font and your control didnot repaints(old text remains there,along with the new value in small font and in a corner). the text will only appear when the entire formview is painted.
pls gimme a solution.!!!
mind is everything.
|
|
|
|

|
I have the same problem. Did you ever find a solution??
|
|
|
|

|
void CXColorStatic::ShowText(CString csText)
{
SetWindowText(csText);
RedrawWindow();
}
saved me for my purpose, set the text by this function, even now a small problem is there..let us try..
mind is everything.
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
XColorStatic is a simple CStatic-based control that provides font changes, text and background colors, and icon display.
| Type | Article |
| Licence | CPOL |
| First Posted | 16 Oct 2003 |
| Views | 105,946 |
| Bookmarked | 67 times |
|
|