|
I need to constantly redraw the Text every 0.5sec, the screen flicker is really annoying. I tried double buffer also tried this class but no luck. Anyone can help me? Thank you very very much.
|
|
|
|
|
this code is simple and fast and no blink and no extra variables, all you have to do just change the OnPaint() function.
BEGIN_MESSAGE_MAP(CStaticEx, CStatic)
//{{AFX_MSG_MAP(CStaticEx)
ON_WM_PAINT()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_SETTEXT,OnSetText)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStaticEx message handlers
void CStaticEx::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRect rect;
GetClientRect(rect);
CString text;
GetWindowText(text);
dc.SelectObject(GetFont());
dc.SetBkMode(TRANSPARENT);
dc.DrawText(text, rect, DT_LEFT);
// Do not call CStatic::OnPaint() for painting messages
}
LRESULT CStaticEx::OnSetText(WPARAM wParam, LPARAM lParam)
{
ShowWindow(SW_HIDE);
Default();
ShowWindow(SW_SHOW);
return 0;
}
eric feng
www.infospec.com
|
|
|
|
|
|
Zorglab,
Thanks for your sample code. I tried to change the static
text color, but I couldn't do it. Is it an easy way to do it? Thanks.
James Lauta
|
|
|
|
|
Hello,
it is a good controls. However, do u have any idea of how to make the CStatic controls, with Icon or Bitmap displayed instead of text, to be transparent ?
Best regards,
hing
|
|
|
|
|
Let me show a simple enhancement to display Icon Type CStatic:
Modify the OnPaint() function as below:
void CTransparentStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
BOOL bfont = FALSE;
BOOL bIcon = FALSE;
BOOL bBmp = FALSE;
// Where to draw text
CRect client_rect;
GetClientRect(client_rect);
// Get the caption
CString szText;
GetWindowText(szText);
HICON hIcon;
hIcon = GetIcon();
if(hIcon)
{
// mean icon type
bIcon = TRUE;
}
HBITMAP hBmp;
hBmp = GetBitmap();
if(hBmp)
{
// mean bitmap type
bBmp = TRUE;
}
// Get the font
CFont *pFont, *pOldFont;
pFont = GetFont();
if(pFont)
{
bfont = TRUE;
}
if(bIcon)
{
dc.DrawIcon(0, 0, hIcon);
}
if(bBmp)
{
// Left for an exercise for interested users
//
}
if(bfont)
{
pOldFont = dc.SelectObject(pFont);
// Map "Static Styles" to "Text Styles"
#define MAP_STYLE(src, dest) if(dwStyle & (src)) dwText |= (dest)
#define NMAP_STYLE(src, dest) if(!(dwStyle & (src))) dwText |= (dest)
DWORD dwStyle = GetStyle(), dwText = 0;
MAP_STYLE( SS_RIGHT, DT_RIGHT );
MAP_STYLE( SS_CENTER, DT_CENTER );
MAP_STYLE( SS_CENTERIMAGE, DT_VCENTER | DT_SINGLELINE );
MAP_STYLE( SS_NOPREFIX, DT_NOPREFIX );
MAP_STYLE( SS_WORDELLIPSIS, DT_WORD_ELLIPSIS );
MAP_STYLE( SS_ENDELLIPSIS, DT_END_ELLIPSIS );
MAP_STYLE( SS_PATHELLIPSIS, DT_PATH_ELLIPSIS );
NMAP_STYLE( SS_LEFTNOWORDWRAP |
SS_CENTERIMAGE |
SS_WORDELLIPSIS |
SS_ENDELLIPSIS |
SS_PATHELLIPSIS, DT_WORDBREAK );
// Set transparent background
dc.SetBkMode(TRANSPARENT);
// Draw the text
dc.DrawText(szText, client_rect, dwText);
// Select old font
dc.SelectObject(pOldFont);
}
}
Again, Thx a lot
|
|
|
|
|
hi,in my program I WRITE:
in ***.h file:
CStatic m_Static;
in **.cpp file:
{
UpdateData(TRUE);
DWORD dw = m_Static.GetStyle();
if( dw & SS_SUNKEN)
m_Static.ModifyStyle(SS_SUNKEN,0);
else
m_Static.ModifyStyle(0,SS_SUNKEN);
m_Static.Invalidate();
UpdateData(FALSE);
}
but "SS_SUNKEN" don't work! why?
|
|
|
|
|
Why not just intercept it?
|
|
|
|
|
It's all fine ang great. But what if you need to redraw the text? It will be drawn on top of the old text and there will be a mess. What to do in this case?
|
|
|
|
|
yes, i have the question too. i am trying to get a resolve. if you have a resolve, please tell me.
|
|
|
|
|
You can call InvalidateRect from parent to invalidate the rect of the cstatic control. It will redraw itself.
From microsoft kb: 639659:
void CRichEdit20::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CRect rect;
GetClientRect(&rect);
ClientToScreen(&rect);
GetParent()->ScreenToClient(&rect);
GetParent()->InvalidateRect(&rect, TRUE);
CRichEditCtrl::OnChar(nChar, nRepCnt, nFlags);
}
Good luck
Tong
|
|
|
|
|
It did use, but blinking when redraw the text.
you are welcome
|
|
|
|
|
I was able to solve this problem by never calling SetWindowText() at all (It doesn't like transparent backgrounds). I keep track of the text internally, with:
CString m_Text;
I then overloaded the = operator:
CString CTransparentStatic::operator=(CString strText)
{
//Set text property
m_Text = strText;
// Invalidate the rect
CRect rect;
GetClientRect(&rect);
ClientToScreen(&rect);
GetParent()->ScreenToClient(&rect);
GetParent()->InvalidateRect(&rect, TRUE);
return strText;
}
When you draw the text, just use:
dc.DrawText(m_Text, client_rect, dwText);
This should work, and no annoying blinking.
|
|
|
|
|
Thx imaudi,
works great. I made it in a little bit different way.
in the TransparentStatic.cpp
1. add the membervariable CString m_Text;
2. remove:
CString szText;
GetWindowText(szText);
3. change dc.drawText to: "dc.DrawText(m_Text, client_rect, dwText);"
Then i create the static and set m_Text in it.
It´s the same than yours but without changing the constructor... however
daSickboy
|
|
|
|
|
this code is simple and fast and no blink and no extra variables, all you have to do just change the OnPaint() function.
BEGIN_MESSAGE_MAP(CStaticEx, CStatic)
//{{AFX_MSG_MAP(CStaticEx)
ON_WM_PAINT()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_SETTEXT,OnSetText)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStaticEx message handlers
void CStaticEx::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRect rect;
GetClientRect(rect);
CString text;
GetWindowText(text);
dc.SelectObject(GetFont());
dc.SetBkMode(TRANSPARENT);
dc.DrawText(text, rect, DT_LEFT);
// Do not call CStatic::OnPaint() for painting messages
}
LRESULT CStaticEx::OnSetText(WPARAM wParam, LPARAM lParam)
{
ShowWindow(SW_HIDE);
Default();
ShowWindow(SW_SHOW);
return 0;
}
eric feng
www.infospec.com
|
|
|
|
|
eric feng wrote: dc.SelectObject(GetFont());
dc.SetBkMode(TRANSPARENT);
dc.DrawText(text, rect, DT_LEFT);
You should re-select the font originally selected by the device context before exiting the method
<br />
CFont *pOldFont = dc.SelectObject(GetFont());<br />
dc.SetBkMode(TRANSPARENT);<br />
dc.DrawText(text, rect, DT_LEFT);<br />
dc.SelectObject(pOldFont);<br />
|
|
|
|
|
This code works but the align center is ignored, all text is justified left regardless. The DT_LEFT needs to looked up and set to what the control is set to.
|
|
|
|
|
Awesome answer and simple solution imaudi!
Helped me on another static class (XColorStatic)
where I was changing text in a transparent situation.
Thanks!!!
Wayne
Wayne L. Burgess
TEN-K Computer Services Inc.
|
|
|
|
|
I use the class in windowsce.net,Static control show the desktop's color.Why?
|
|
|
|
|
Actually, I didn't tried nor developed this control on/for Windows CE.
|
|
|
|
|
Sometime we want to put bitmap in the dialog and want set a certain color to be transparent, I want to know if this control can make bitmap transparent.
|
|
|
|
|
No, this control is only meant to make text transparent.
Anyway, you can use masked bitmaps.
|
|
|
|
|
I am using the derived class on a formview, but when I change the text of the class, I lose the transparency. Also, I am dynamically swapping the views in/out of a frame, but again the same things happens when I swap back...
Any ideas?
|
|
|
|
|
Strange...
|
|
|
|
|
When I used SetWindowText() in a dialog I lost the transparency, too. Does anybody have a solution for this problem?
|
|
|
|