|
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?
|
|
|
|
|
From Eric Feng's post above, override the OnSetText function. It clears up the SetWindowText problem nicely.
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;
}
|
|
|
|
|
Same thing here.
It makes this class pretty much useless since I already have a control that is much more flexible than this one. Of course my control has a problem also, but it stays transparent at least.
|
|
|
|
|
Hi, Zorglab
IT WORK FINE:
class MyClass : public CStatic, public MyOtherClass
IT DO NOT WORK (Halt the App)
class MyClass : public MyOtherClass, public CStatic
Do you know why ?
|
|
|
|
|
Quite strange...
What's that MyOtherClass ? Where is it derived from ?
|
|
|
|
|
Copy of MyLabel.cpp and MyLabel.h files
// MyLabel.cpp : implementation file
//
#include "stdafx.h"
#include "LabelT.h"
#include "MyLabel.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
Teste::Teste()
{
}
Teste::~Teste()
{
}
/////////////////////////////////////////////////////////////////////////////
// MyLabel
MyLabel::MyLabel()
{
}
MyLabel::~MyLabel()
{
}
BEGIN_MESSAGE_MAP(MyLabel, CStatic)
//{{AFX_MSG_MAP(MyLabel)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// MyLabel message handlers
void MyLabel::OnPaint()
{
CPaintDC dc(this); // device context for painting
// Where to draw text
CRect client_rect;
GetClientRect(client_rect);
// Get the caption
CString szText;
GetWindowText(szText);
// Get the font
CFont *pFont, *pOldFont;
pFont = GetFont();
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);
}
///////// end of file .cpp ////////////////////////
#if !defined(AFX_MYLABEL_H__90C354CD_12A8_4EDF_B7C4_60F52CDF2093__INCLUDED_)
#define AFX_MYLABEL_H__90C354CD_12A8_4EDF_B7C4_60F52CDF2093__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyLabel.h : header file
//
class Teste
{
public:
Teste();
virtual ~Teste();
int a;
};
/////////////////////////////////////////////////////////////////////////////
// MyLabel window
class MyLabel : public CStatic, public Teste
{
// Construction
public:
MyLabel();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(MyLabel)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~MyLabel();
// Generated message map functions
protected:
//{{AFX_MSG(MyLabel)
afx_msg void OnPaint();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MYLABEL_H__90C354CD_12A8_4EDF_B7C4_60F52CDF2093__INCLUDED_)
|
|
|
|
|