|
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_)
|
|
|
|
|
Doesn't that work in a CStatic directly?
WS_EX_TRANSPARENT Specifies that a window created with this style is to be transparent. That is, any windows that are beneath the window are not obscured by the window.
Marc
Help! I'm an AI running around in someone's f*cked up universe simulator. Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus Every line of code is a liability - Taka Muraoka
|
|
|
|
|
I thought so, but it doesn't. The CStatic still appear opaque.
|
|
|
|
|
Don't you just hate it when this kind of sh*t happens?
Good article.
I am curious about one other thing though. Since you probably would be using this class on top of a window that is already owner drawn, wouldn't you just put the text you want to display into that window, instead of layering a transparent static window on top of it? Or is my assumption about usage wrong?
Marc
Help! I'm an AI running around in someone's f*cked up universe simulator. Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus Every line of code is a liability - Taka Muraoka
|
|
|
|
|
Don't you just hate it when this kind of sh*t happens?
Yes, indeed.
Good article.
Thanks.
I am curious about one other thing though. Since you probably would be using this class on top of a window that is already owner drawn, wouldn't you just put the text you want to display into that window, instead of layering a transparent static window on top of it? Or is my assumption about usage wrong?
Of course, that's a possible solution, but I wanted to make an already existing static control transparent. And since my bitmap isn't owner drawn...
The biggest advantage, I think, of the transparent CStatic, is you define it's position and attributes in the dialog resource editor which is WYSIWYG and you get a real time preview (except the transparency).
|
|
|
|
|
To my experience, WS_EX_TRANSPARENT is *almost* like returning TRUE to WM_ERASEBKGND. If you draw something in reply to WM_PAINT, as a Static control does, you can't see the difference (unless the control is bigger than the contained text, maybe).
The Static control paints the text using an opaque background, that you can disable by handling WM_CTLCOLORSTATIC messages and calling SetBkMode() on the given HDC. This way you have to explicitly refresh the control whenever the background changes or the control is resized.
Cheers,
Paolo
------
"airplane is cool, but space shuttle is even better" (J. Kaczorowski)
|
|
|
|
|
I tried this, but it doesn't work. Why ?
|
|
|
|
|
I have the following code in a CStatic-derived class:
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)GetStockObject(NULL_BRUSH);
At the end of my CStatic::CtlColor() override and it works. As I said, there are also other things to consider: for example, if you change the font attributes (bold, italic, underline, etc.) dynamically you may have to invalidate the portion of the parent window (or underlying controls) overlapped by the static control.
Paolo
------
"airplane is cool, but space shuttle is even better" (J. Kaczorowski)
|
|
|
|
|
After giving it a last try, I found a better way to make all static control on a dialog transparent : put the following code in the CWnd::OnCtlColor() override of the dialog.
HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (nCtlColor == CTLCOLOR_STATIC)
{
pDC->SetBkMode(TRANSPARENT);
hbr = (HBRUSH) GetStockObject(NULL_BRUSH);
}
return hbr;
}
Thanks for your help and sorry for the late answer .
Zorglab
BTW : the WS_EX_TRANSPARENT style enables transparency for the mouse. This means mouse clicks and moves are not forwarded (or forwarded after all controls).
|
|
|
|
|
While this will work, wait until you change the text of your static control. The old text won't be erased and the new text will be drawn on top of it. It will be a garbled mess.
One of the above replies mentioned invalidating the portion of the parent window the underlies the static control. I'm going to experiment with that. I hope it works, because I really need it to.
|
|
|
|
|
YES!!!!
It works!!!
Many, Many thanks!!!!
F@#ing Microsoft! How in the h#ll would I know to use GetStockObject(NULL_BRUSH) ???
|
|
|
|
|
hi,it will work fine.
void CTransparentStatic::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
// We want to get mouse clicks via STN_CLICKED
DWORD dwStyle = GetStyle();
::SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, dwStyle | SS_NOTIFY | WS_EX_TRANSPARENT);
CStatic::PreSubclassWindow();
}
|
|
|
|