 |
|
 |
I am trying to auto size the text in CLabel so the text always fills the label. I tried the code below, but dc.DrawText() always returns the same size rectangle. void CDebateTimerDlg::AutoSizeFont(void) { CPaintDC dc(&m_Label); // device context for painting CString text = _T("Sample Text"); CRect rect, rc; m_Label.GetClientRect(&rect); rc = rect; bool bLeft = false; bool bRight = false; do { if (rc.right < rect.right) m_font.nSize = int(m_font.nSize * 1.1); else if (rc.right > rect.right) m_font.nSize = int(m_font.nSize * 0.9); rc = rect; m_Label.SetFontSize(m_font.nSize); dc.DrawText(text, text.GetLength(), &rc, DT_SINGLELINE | DT_CALCRECT); // rc never changes. bLeft = int(0.9 * rect.right) <= rc.right; bRight = rc.right <= int(1.1 * rc.right); } while (!(bLeft && bRight)); } ==================== Any suggestions? This code is suppose to maximize the font to between 90 - 110% of the label width.
-- modified 9 Dec '11.
|
|
|
|
 |
|
 |
This CLabel class is quite old but still has a great use!
I had use CLabel extensively from some time in a bingo game for BedOS and other MFC applications. I however have several remarks about it, I hope this to reach the author.
1. WM_SETTEXT not overrided
Sometimes there is a thread or DLL or process whick communicate with WM_SETTEXT message, not always text can be set with SetText() member function. But I think that OnSetText() is not overrided and UpdateSurface() is not call in this version (1.6)....
2. I had placed a call with WM_SETTEXT to a transparent label in a timer. When it has to be redrawn frequently I saw that there is a flick problem because in OnSetText() there is a call to Default() function which redraws client rectangle in the nasty default control color first. This can be solved with commenting Default() in OnSetText(). But this will lead to another problem - text is not going to be changed with SetWindowText() function because this is also in Default()....
This can be solved as follow:
- in Label.h add m_Text var:
CString m_Text ;
- in PreSubclassWindow() add:
GetWindowText(m_Text);
- at the top of OnPaint() replace
GetWindowText(strText);
with
strText = m_Text ;
- add OnSetText() override:
ON_MESSAGE(WM_SETTEXT, OnSetText)
- replace content of OnSetText() with:
void CLabel::OnSetText(WPARAM wParam, LPARAM lParam)
{
// Default();
TCHAR* strText = (TCHAR*)lParam ;
if(IsWindow(this->GetSafeHwnd()))
{
m_Text = strText ;
UpdateSurface();
}
}
-replace content of SetText() with:
CLabel::SetText(const CString& strText)
{
if(IsWindow(this->GetSafeHwnd()))
{;
if(m_Text != strText)
{
m_Text = strText ;
SetWindowText(strText);
//UpdateSurface();
}
}
return *this;
}
(otherwise UpdateSurface() is going to be called twice!
Hope that this helps!
tonim
modified on Wednesday, June 9, 2010 5:56 PM
|
|
|
|
 |
|
 |
Glad there's still somebody coding in MFC!
I've moved to .net 10 years ago, and won't be doing any further work in this article. Please modify the code.,
Two heads are better than one.
|
|
|
|
 |
|
 |
add mouseover and mouseleave method,some suggestion?
|
|
|
|
 |
|
 |
Thanks for the CLabel class. There are no license restrictions listed on Code Project, regarding this class. Are there license restrictions?
Thanks.
Clint
|
|
|
|
 |
|
 |
No restrictions, use anywhere it's completely free without ties.
|
|
|
|
 |
|
|
 |
|
 |
Hi! A Very useful article!!
I am doing DDX_Control with RichEdit control in my form view to the member variable of your CLabel.
Everything is working fine except SetFontSize() for me.
I am displaying link using SetText() and SetHyperLink() functions.
How to change the caption's font size(which is set using SetText()) displayed in control?
Any suggestion will be very helpful..
Thanks..
modified on Wednesday, January 21, 2009 10:41 AM
|
|
|
|
 |
|
 |
I had some flashing problems on transparent label redraw when they are positioned over a bitmap drawned dynamically. I fixed the problem with the following changes.
CLabel& CLabel::SetText(const CString& strText)
{
if(IsWindow(this->GetSafeHwnd()))
{
SetRedraw(FALSE);
SetWindowText(strText);
SetRedraw(TRUE);
UpdateSurface();
}
return *this;
}
void CLabel::UpdateSurface()
{
CRect (rc);
GetWindowRect(rc);
//RedrawWindow();
GetParent()->ScreenToClient(rc);
GetParent()->InvalidateRect(rc,TRUE);
GetParent()->UpdateWindow();
}
Thanks for your good class
|
|
|
|
 |
|
 |
Thanks very much for the code!
By default, set byCharSet=ANSI_CHARSET, the font doesn't work in some languages such as Simplified Chinese.
|
|
|
|
 |
|
 |
Thanks for this class.
Is it possible to change alignment to right aligned (for numeric number) ?
And also to change vertical alignement to bottom aligned ?
|
|
|
|
 |
|
 |
Is it possible to rotate bitmaps? Thanks for a great piece of code.
|
|
|
|
 |
|
 |
Hello,
I am using CLabel in an application and I am trying to set a background image to it.
I added some new methods for this and I intended to override DrawItem to repaint the control.
However, the DrawItem method doesn't seem to be called from anywhere. From what I understood from the documentation, I must set the style of the control to SS_OWNERDRAW (I do this in PreSubClassWindow).
It still doesn't work.
Could you give me a hand here?
Thanks a lot.
Ovidiu
|
|
|
|
 |
|
 |
I'm looking for information on using this code in a commercial product. I see no copyright or license information on this site or in the code so I'm assuming it's free to use, but I'd like to be sure. And since I can't find a way to send a message directly to the author I'm positing here.
Thanks.
|
|
|
|
 |
|
 |
I'm the author of this control, please use it freely without any restrictions at all.
Norm Almond
Software Kinetics
.net is a box of never ending treasures, every day I get find another gem.
|
|
|
|
 |
|
 |
Hi great code thanks !
I tried enabling / disabling your CLabel via:
myLabel.EnableWindow(FALSE);
myLabel.EnableWindow(TRUE);
All the set formating seems to disappear !?
Bug ?
Thanks
Chris
|
|
|
|
 |
|
 |
This has been one of my all time favorite classes over the years.
So much so, that I've used it lots of times to dress up quick projects.
I recently downloaded and updated my files to see if I could resolve an issue I had with flashing text on-off-on inadvertently wiping out the text value.
Unfortunately, it looks like this version is a LOT slower to refresh than the old one. For example I have about 50 small little CLabels on my latest dialog project that I use as little colored status indicators as I read in a text file. Because this file reader reads in hundreds of lines at a shot when I first start, I watch my labels flash madly until it is loaded.
The newest verion 1.6 really bogs down updating the labels compared to my old version (I'm guessing I had 1.1, but I'm not sure there is no comment at top). I'm guessing that each functions call to UpdateSurface()precludes you from making a lot of calls in a row.
The real killer is that my flashing background problem still seems to be there. Has anyone else noticed FlashBackground(FALSE) + FlashBackground(TRUE) + FlashBackground(FALSE) ... seems to disable the flashing ability?
A++ for a great little class over the years.
B+ for keeping up with it and adding new even cooler features
C- for allowing the feature creep to impair performance.
|
|
|
|
 |
|
 |
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.
CString m_strText; // Label Text // (Add) for Protecting Flicker(EastSeaBoy)
1. void CLabel::OnPaint()
{
.....
// GetWindowText(strText); // (Del) for Protecting Flicker(EastSeaBoy)
strText = m_strText; // (Add) for Protecting Flicker(EastSeaBoy)
.....
}
2. CLabel& CLabel::SetText(const CString& strText)
{
if(IsWindow(this->GetSafeHwnd()))
{
//SetWindowText(strText); // (Del) for Protecting Flicker(EastSeaBoy)
m_strText = strText; // (Add) for Protecting Flicker(EastSeaBoy)
UpdateSurface();
}
return *this;
}
==========================================
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!!!
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |