|
Just in case anyone is interested, I figured out a way to draw the CLabel such that it looks like colored glass with text on it, hovering over the background (semi-transparent but with color).
Here's what you do:
All of this code modification is in the OnPaint function
Here's the original section (near the beginning of OnPaint):
CDC* pDCMem;
CBitmap* pOldBitmap = NULL;
if (!m_bTransparent)
{
pDCMem = new CDC;
pDCMem->CreateCompatibleDC(&dc);
bmp.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
pOldBitmap = pDCMem->SelectObject(&bmp);
}
Replace the existing "else" right below that, with the following:
else
{
pDCMem = new CDC;
pDCMem->CreateCompatibleDC(&dc);
bmp.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
pOldBitmap = pDCMem->SelectObject(&bmp);
CBrush brush;
brush.CreateSolidBrush(RGB(122,255,122));
pDCMem->FillRect(rc, &brush);
}
What this part does is create a compatible bitmap in memory and paints it a light shade of green (you can obvious change the color as you see fit).
Then down at the bottom of the function, is the original code:
if (!m_bTransparent)
{
dc.BitBlt(0,0,rc.Width(),rc.Height(),pDCMem,0,0,SRCCOPY);
pDCMem->SelectObject ( pOldBitmap ) ;
delete pDCMem;
}
Add just below that:
else
{
dc.BitBlt(0,0,rc.Width(),rc.Height(),pDCMem,0,0,SRCAND);
pDCMem->SelectObject ( pOldBitmap ) ;
delete pDCMem;
}
All of this occurs after the text has been drawn. So what you have is a bitmap in memory of a light green colored rectangle with text drawn on it. What this last section of code does is BitBlt that onto the background using SRCAND which combines the source and destination using a logical AND.
There you go... semi-transparent with color.
|
|
|
|
|
I have a problem that all the label disappear after I call SetWindowPos function. Anyone has any idea?
Thanks a lot.
|
|
|
|
|
I have had the same problem!
I removed the function call GetParent()->UpdateWindow()
from void CLabel::UpdateSurface().
Now everthing runs fine
|
|
|
|
|
Hello, I am having the problem with screen flickerring when the label is repeatly painted. I noticed that the double buffer is only applied when m_bTransparent = FALSE in the OnPaint() function. How to avoid the flickerinf problem for the transperent label?
Thank you very much for your help.
|
|
|
|
|
This class is great!!! WOW~~!
I had have the same problem.
I solved that like Following Code.
EastSeaBoy is my nickname.
=========================================
0. ADD a Member Variable.<br />
CString m_strText;
<br />
<br />
1. void CLabel::OnPaint() <br />
{<br />
.....<br />
strText = m_strText;
.....<br />
}<br />
<br />
2. CLabel& CLabel::SetText(const CString& strText)<br />
{<br />
if(IsWindow(this->GetSafeHwnd())) <br />
{<br />
m_strText = strText;
UpdateSurface();<br />
}<br />
return *this;<br />
}
==========================================
Right?
|
|
|
|
|
After modify the code, you should call SetText() to show a text when you want to show something. Just like this:
m_label
.SetTextColor(F_C_NOCONNECT)
.SetText("No connect") <--------------notice this
.ShowWindow(SW_SHOW);
If you do not call SetText(), this label can not be visible, even it already has a text you enterd in the VC Text Properties window.
|
|
|
|
|
you are great!
thank you!!!
|
|
|
|
|
There is more code if you want to catch all cases:
BOOL CLabel::Create(LPCTSTR lpszText, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
m_sText = lpszText;
return CStatic::Create(lpszText, dwStyle, rect, pParentWnd, nID);
}
and
void CLabel::PreSubclassWindow()
{
CStatic::PreSubclassWindow();
CString sText;
GetWindowText(sText);
if(m_sText.IsEmpty() && ! sText.IsEmpty())
m_sText = sText;
CFont* cf = GetFont();
if(cf != NULL)
cf->GetObject(sizeof(m_lf), &m_lf);
else
GetObject(GetStockObject(SYSTEM_FONT), sizeof(m_lf), &m_lf);
ReconstructFont();
}
|
|
|
|
|
Hello, I need to change the transparent label text every 500MS with graphic background. It shows a white flash sometimes when it is repainted. Any idea to improve this?
Thank you very much.
|
|
|
|
|
I can't vote (5) for the article more than once! Good job as usual, Norm!
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
Has anyone come up with a fix for setting the window state ( EnableWindow(FALSE) ) in OnInitDialog?
Yofnik
|
|
|
|
|
Just an FYI:
The line in Label.cpp with "mailto:" breaks UNICODE compatibility. I see you've used _T() with other literals, so that one probably just slipped through... You may want to issue a quick article update.
--
Edward Livingston
(aka ExtraLean)
--
"I still maintain that seeing a nipple is far less disturbing than seeing someone get their brains blown out." -- Chris Maunder
|
|
|
|
|
Hi guys,
Am trying to use CLabel in a skinned dialog app. now the problem is sometimes when I set the text of the control using SetWindowText there is no transparency any more.
e.g in OnInitDialog
m_static.SetWindowText("Hello world");<br />
m_static.SetTransparent(TRUE);<br />
m_static.SetWindowText("Wassup?");
this works fine, but when i have something like this in a button click handler
m_static.SetWindowText("Testing ");
the background appears and i have to SetTransparent again for it to become transparent.
I don't think this is the way it should work..any suggestions? thanx
|
|
|
|
|
|
'LoadCursorA' : cannot convert parameter 2 from 'const int' to 'const char *'
m_stcMail.SetMailLink(TRUE,FALSE)
.SetTextColor(RGB(0,0,255))
.SetFontUnderline(TRUE)
.SetLinkCursor(LoadCursor(NULL,IDC_HAND));
m_link.SetLink(TRUE,FALSE)
.SetTextColor(RGB(0,0,255))
.SetFontUnderline(TRUE)
.SetLinkCursor(LoadCursor(NULL, IDC_HAND));
Marcus Carey
|
|
|
|
|
add
#define IDC_HAND MAKEINTRESOURCE(32649)
|
|
|
|
|
I am creating a subclass of CWnd that I create dynamically and add to a dialog. This class contains several CLabels that I create and position myself (no templates involved). So far I have only tried using the SetFontSize and SetTextColor functions, and the color changes but not the font size. I tried calling the same functions with a CLabel that I created on a dialog with a template, and both worked fine.
Here is the code in the Create function of my CWnd subclass:
m_sBP.Create("BP", lStaticStyle, CRect(iDPSplit, iCol1Top, iRightEdge, iCol2Top), this);
m_sBP.SetFontSize(14);
m_sBP.SetTextColor(RGB(255,0,0));
where m_sBP is a CLabel declared in the header file.
Can anyone help me out?
Thanks
-=Connor=-
|
|
|
|
|
I might have just solved my own problem - I changed the font and it seems to be working now.
-=Connor=-
|
|
|
|
|
Has anyone come up with a fix for this yet? If not, does anybody know of another static class that works and can change text attributes?
Tired of my application aborting all the time!!!!
|
|
|
|
|
What leak? The tone of your message stinks, I suggest buddy you FIX it.
To iterate is human, to recurse is devine.
|
|
|
|
|
First off I'm not your buddy. Second my tone was only that of someone looking for a solution since February. Sounds a bit defensive to me.
|
|
|
|
|
Does anybody know how to implement text wrapping for this control?
If I have a single word that is longer than the width of the static it is cut off. Word break style wont do the trick since its a single wordthat is bigger than the static.
Any Ideas
|
|
|
|
|
Quick hack, at least work as my usage
change the OnPaint() function in CLabel.cpp, from the 3D part to the end of the function
//
// 3333 DDDDD
// 3 D D
// 33 D D E F X
// 3 D D
// 3333 DDDDD
//
//
if (m_bRotation)
{
int nAlign = pDCMem->SetTextAlign (TA_BASELINE);
CPoint pt;
GetViewportOrgEx (pDCMem->m_hDC,&pt) ;
SetViewportOrgEx (pDCMem->m_hDC,rc.Width() / 2, rc.Height() / 2, NULL) ;
pDCMem->TextOut (0, 0, strText) ;
SetViewportOrgEx (pDCMem->m_hDC,pt.x / 2, pt.y / 2, NULL) ;
pDCMem->SetTextAlign (nAlign);
}
else
{
CRect newRC;
TEXTMETRIC tag;
CSize sz;
CString s = (LPCTSTR)strText;
::GetTextExtentPoint32(pDCMem->GetSafeHdc(), strText,
strText.GetLength(), &sz);
CStringArray asText;
int nLine = 1;
CString s2;
int nIdx, nX = 0;
if (sz.cx > rc.Width())
{
s2.Empty();
// need wrapping
while (s.GetLength() > 0)
{
nIdx = s.Find(' ', 0);
if (nIdx != -1)
{
::GetTextExtentPoint32(pDCMem->GetSafeHdc(),
s.Left(nIdx+1), s.Left(nIdx+1).GetLength(), &sz);
if (nX + sz.cx > rc.Width())
{
nX = 0;
nLine++;
asText.SetAtGrow(nLine-1, s2);
TRACE1("%s\n", s2);
s2.Empty();
}
else
{
nX += sz.cx;
s2 += (LPCTSTR)s.Left(nIdx + 1);
s = s.Right(s.GetLength() - nIdx -1);
}
}
else
{
if (s2.GetLength() > 0)
{
nLine++;
asText.SetAtGrow(nLine-1, s2);
s2.Empty();
}
::GetTextExtentPoint32(pDCMem->GetSafeHdc(),
s, s.GetLength(), &sz);
if (sz.cx < rc.Width())
{
nLine++;
asText.SetAtGrow(nLine-1, s);
TRACE1("%s\n", s);
s.Empty();
}
else // no space inside
{
for (int i=0; i<s.GetLength(); i++)
{
::GetTextExtentPoint32(pDCMem->GetSafeHdc(),
s.Left(i), i, &sz);
if (sz.cx > rc.Width())
{
nLine++;
asText.SetAtGrow(nLine-1, s.Left(i-1));
s = s.Right(s.GetLength() - i);
break;
}
}
}
}
}
}
else
{
asText.SetAtGrow(0, strText);
}
pDCMem->GetTextMetrics(&tag);
LONG nDiff = rc.bottom - rc.top - tag.tmHeight * nLine - 2;
if (nDiff > 0)
{
newRC.left = rc.left;
newRC.top = rc.top + nDiff/2;
newRC.right = rc.right;
newRC.bottom = newRC.top + tag.tmHeight + 1;
}
else
newRC = rc;
CRect newRC2;
for (int i=0; i<nLine; i++)
{
newRC2.top = newRC.top + tag.tmHeight * i;
newRC2.left = newRC.left;
newRC2.right = newRC.right;
newRC2.bottom = newRC2.top + tag.tmHeight;
TRACE1("%s\n", asText[i]);
pDCMem->DrawText(asText[i],newRC2,dwFlags);
if (m_bFont3d)
{
pDCMem->SetTextColor(m_cr3DHiliteColor);
if (m_3dType == Raised)
newRC2.OffsetRect(-1,-1);
else
newRC2.OffsetRect(1,1);
pDCMem->DrawText(asText[i],rc,dwFlags);
m_3dType;
}
}
}
// Restore DC's State
pDCMem->SetBkMode(nMode);
pDCMem->SelectObject(pOldFont);
pDCMem->SetTextColor(crText);
if (!m_bTransparent)
{
dc.BitBlt(0,0,rc.Width(),rc.Height(),pDCMem,0,0,SRCCOPY);
// continue DC restore
pDCMem->SelectObject ( pOldBitmap ) ;
delete pDCMem;
}
|
|
|
|
|
Has anyone come up with a fix for the memory leak in version 1.6?
|
|
|
|
|
After opening and closing a dialog a few times with the CLabel control, the GDI Objects stop decrementing.
|
|
|
|
|