一个STATIC控件,名字为IDC_STATIC_TEST,原本没有SUNKEN风格(就是凹下去那种风格),再添加一个BUTTON按钮IDC_BUTTON1,想在BUTTON的相应事件里给IDC_STATIC_TEST添加SUNKEN风格,代码如下:
CStatic* p = (CStatic*)GetDlgItem(IDC_STATIC_TEST);
LONG lStyle = GetWindowLong(p->m_hWnd, GWL_STYLE);
lStyle |= SS_SUNKEN;
SetWindowLong(p->m_hWnd, GWL_STYLE, lStyle);
p->Invalidate();
我单步调试的时候,发现我确实修改了IDC_STATIC_TEST的控件风格,但是在界面上来看,却没有任何变化。WHY?
CStatic* p = (CStatic*)GetDlgItem(IDC_STATIC_TEST);
LONG lStyle = GetWindowLong(p->m_hWnd, GWL_STYLE);
lStyle |= SS_SUNKEN;
SetWindowLong(p->m_hWnd, GWL_STYLE, lStyle);
p->Invalidate();
我单步调试的时候,发现我确实修改了IDC_STATIC_TEST的控件风格,但是在界面上来看,却没有任何变化。WHY?
#if !defined(AFX_A16Label_H__A4EABEC5_2E8C_11D1_B79F_00805F9ECE10__INCLUDED_)
#define AFX_A16Label_H__A4EABEC5_2E8C_11D1_B79F_00805F9ECE10__INCLUDED_#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// A16Label.h : header file
//#define NM_LINKCLICK WM_APP + 0x200/////////////////////////////////////////////////////////////////////////////
// CA16Label windowclass CA16Label : public CStatic
{
// Construction
public:
enum FlashType {None, Text, Background };
enum Type3D { Raised, Sunken}; CA16Label();
virtual CA16Label& SetBkColor(COLORREF crBkgnd);
virtual CA16Label& SetTextColor(COLORREF crText);
virtual CA16Label& SetText(const CString& strText);
virtual CA16Label& SetFontBold(BOOL bBold);
virtual CA16Label& SetFontName(const CString& strFont);
virtual CA16Label& SetFontUnderline(BOOL bSet);
virtual CA16Label& SetFontItalic(BOOL bSet);
virtual CA16Label& SetFontSize(int nSize);
virtual CA16Label& SetSunken(BOOL bSet);
virtual CA16Label& SetBorder(BOOL bSet);
virtual CA16Label& SetTransparent(BOOL bSet);
virtual CA16Label& FlashText(BOOL bActivate);
virtual CA16Label& FlashBackground(BOOL bActivate);
virtual CA16Label& SetLink(BOOL bLink,BOOL bNotifyParent);
virtual CA16Label& SetLinkCursor(HCURSOR hCursor);
virtual CA16Label& SetFont3D(BOOL bSet,Type3D type=Raised);
virtual CA16Label& SetRotationAngle(UINT nAngle,BOOL bRotation);
// Attributes
public:
protected:
void ReconstructFont();
COLORREF m_crText;
HBRUSH m_hwndBrush;
HBRUSH m_hBackBrush;
LOGFONT m_lf;
CFont m_font;
CString m_strText;
BOOL m_bState;
BOOL m_bTimer;
BOOL m_bLink;
BOOL m_bTransparent;
FlashType m_Type;
HCURSOR m_hCursor;
Type3D m_3dType;
BOOL m_bFont3d;
BOOL m_bToolTips;
BOOL m_bNotifyParent;
BOOL m_bRotation; // Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CA16Label)
public:
//}}AFX_VIRTUAL// Implementation
public:
virtual ~CA16Label(); // Generated message map functions
protected:
//{{AFX_MSG(CA16Label)
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
afx_msg void OnPaint();
afx_msg void OnSysColorChange();
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.#endif // !defined(AFX_A16Label_H__A4EABEC5_2E8C_11D1_B79F_00805F9ECE10__INCLUDED_)
//#include "stdafx.h"
#include "Resource.h"
#include "A16Label.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifBEGIN_MESSAGE_MAP(CA16Label, CStatic)
//{{AFX_MSG_MAP(CA16Label)
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
ON_WM_PAINT()
ON_WM_SYSCOLORCHANGE()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////
CA16Label::CA16Label()
{
m_crText = GetSysColor(COLOR_WINDOWTEXT);// 1.1
m_hBackBrush = NULL;
::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_lf),&m_lf); m_font.CreateFontIndirect(&m_lf);
m_bTimer = FALSE;
m_bState = FALSE;
m_bTransparent = FALSE;
m_bLink = TRUE;
m_hCursor = NULL;
m_Type = None;
m_bFont3d = FALSE;
m_bNotifyParent = FALSE;
m_bToolTips = FALSE;
m_bRotation = FALSE;
m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::~CA16Label
//
// Description:
//////////////////////////////////////////////////////////////////////////
CA16Label::~CA16Label()
{
// Clean up
m_font.DeleteObject();
::DeleteObject(m_hwndBrush);
::DeleteObject(m_hBackBrush);
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::ReconstructFont
//
// Description: Helper function to build font after it was changed//////////////////////////////////////////////////////////////////////////
void CA16Label::ReconstructFont()
{
m_font.DeleteObject();
BOOL bCreated = m_font.CreateFontIndirect(&m_lf); ASSERT(bCreated);
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::OnPaint
//
// Description: Handles all the drawing code for the A16Label
//////////////////////////////////////////////////////////////////////////void CA16Label::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rc;
GetClientRect(rc);
CString strText;
GetWindowText(strText); ///////////////////////////////////////////////////////
//
// Set up for double buffering...
//
CDC* pDCMem; if (!m_bTransparent)
{
pDCMem = new CDC;
pDCMem->CreateCompatibleDC(&dc);
CBitmap bmp;
bmp.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
pDCMem->SelectObject(&bmp);
}
else
{
pDCMem = &dc;
}
COLORREF crText = pDCMem->SetTextColor(m_crText);
CFont *pOldFont = pDCMem->SelectObject(&m_font);
// Fill in backgound if not transparent
if (!m_bTransparent)
{
CBrush br;
if (m_bState && m_Type == Background)
{
if (m_hBackBrush != NULL)
br.Attach(m_hBackBrush);
else
br.Attach(m_hwndBrush);
}
else
{
if (m_hBackBrush != NULL)
br.Attach(m_hBackBrush);
else
br.Attach(m_hwndBrush);
}
pDCMem->FillRect(rc,&br);
br.Detach();
}
UINT nMode = pDCMem->SetBkMode(TRANSPARENT); // If the text is flashing turn the text color on
// then to the color of the window background. LOGBRUSH lb;
ZeroMemory(&lb,sizeof(lb));
::GetObject(m_hBackBrush,sizeof(lb),&lb);
// Something to do with flashing
if (!m_bState && m_Type == Text)
pDCMem->SetTextColor(lb.lbColor);
DWORD dwFlags = 0; if (GetStyle() & SS_LEFT)
dwFlags = DT_LEFT; if (GetStyle() & SS_RIGHT)
dwFlags = DT_RIGHT; // If the text centered make an assumtion that
// the will want to center verticly as well
if (GetStyle() & SS_CENTERIMAGE)
{
dwFlags = DT_CENTER; // Apply
if (strText.Find("\r\n") == -1)
{
dwFlags |= DT_VCENTER; // And because DT_VCENTER only works with single lines
dwFlags |= DT_SINGLELINE;
}
}
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
{
pDCMem->DrawText(strText,rc,dwFlags);
if (m_bFont3d)
{
pDCMem->SetTextColor(RGB(255,255,255)); if (m_3dType == Raised)
rc.OffsetRect(-1,-1);
else
rc.OffsetRect(1,1); pDCMem->DrawText(strText,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);
delete pDCMem;
}
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::OnTimer
//
// Description: Used in conjunction with 'FLASH' functions//////////////////////////////////////////////////////////////////////////
void CA16Label::OnTimer(UINT nIDEvent)
{
m_bState = !m_bState; InvalidateRect(NULL,TRUE);
UpdateWindow();
CStatic::OnTimer(nIDEvent);
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::OnSetCursor
//
// Description: Used in conjunction with 'LINK' function
//////////////////////////////////////////////////////////////////////////
BOOL CA16Label::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (m_hCursor)
{
::SetCursor(m_hCursor);
return TRUE;
} return CStatic::OnSetCursor(pWnd, nHitTest, message);
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::OnLButtonDown
//
// Description: Called when a link is click on
//////////////////////////////////////////////////////////////////////////
void CA16Label::OnLButtonDown(UINT nFlags, CPoint point)
{
if (m_bNotifyParent)
{
CString strLink; GetWindowText(strLink);
ShellExecute(NULL,"open",strLink,NULL,NULL,SW_SHOWNORMAL);
}
else
{
// To use notification in parent window
// Respond to a OnNotify in parent and disassemble the message
//
NMHDR nm; nm.hwndFrom = GetSafeHwnd();
nm.idFrom = GetDlgCtrlID();
nm.code = NM_LINKCLICK;
GetParent()->SendMessage(WM_NOTIFY,nm.idFrom,(LPARAM) &nm);
}
CStatic::OnLButtonDown(nFlags, point);
}
//
// Function: CA16Label::SetText
//
// Description: Short cut to set window text - caption - A16Label//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetText(const CString& strText)
{
SetWindowText(strText);
return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetTextColor
//
// Description: Sets the text color //////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetTextColor(COLORREF crText)
{
m_crText = crText;
RedrawWindow();
return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetFontBold
//
// Description: Sets the font ot bold //////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetFontBold(BOOL bBold)
{
m_lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
ReconstructFont();
RedrawWindow();
return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetFontUnderline
//
// Description: Sets font underline attribue//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetFontUnderline(BOOL bSet)
{
m_lf.lfUnderline = bSet;
ReconstructFont();
RedrawWindow();
return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetFontItalic
//
// Description: Sets font italic attribute//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetFontItalic(BOOL bSet)
{
m_lf.lfItalic = bSet;
ReconstructFont();
RedrawWindow();
return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetSunken
//
// Description: Sets sunken effect on border//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetSunken(BOOL bSet)
{
if (!bSet)
ModifyStyleEx(WS_EX_STATICEDGE,0,SWP_DRAWFRAME);
else
ModifyStyleEx(0,WS_EX_STATICEDGE,SWP_DRAWFRAME);
return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetBorder//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetBorder(BOOL bSet)
{
if (!bSet)
ModifyStyle(WS_BORDER,0,SWP_DRAWFRAME);
else
ModifyStyle(0,WS_BORDER,SWP_DRAWFRAME);
return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetFontSize
//
// Description: Sets the font size//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetFontSize(int nSize)
{
nSize*=-1;
m_lf.lfHeight = nSize;
ReconstructFont();
RedrawWindow();
return *this;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetBkColor
//
// Description: Sets background color
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetBkColor(COLORREF crBkgnd)
{
if (m_hBackBrush)
::DeleteObject(m_hBackBrush);
m_hBackBrush = ::CreateSolidBrush(crBkgnd); return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetFontName
//
// Description: Sets the fonts face name
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetFontName(const CString& strFont)
{
strcpy(m_lf.lfFaceName,strFont);
ReconstructFont();
RedrawWindow();
return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::FlashText
//
// Description: As the function states
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::FlashText(BOOL bActivate)
{
if (m_bTimer)
KillTimer(1); if (bActivate)
{
m_bState = FALSE;
m_bTimer = TRUE;
SetTimer(1,500,NULL); m_Type = Text;
}
else
m_Type = None; // Fix return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::FlashBackground
//
// Description: As the function states
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::FlashBackground(BOOL bActivate)
{ if (m_bTimer)
KillTimer(1); if (bActivate)
{
m_bState = FALSE; m_bTimer = TRUE;
SetTimer(1,500,NULL); m_Type = Background;
} return *this;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetLink
//
// Description: Indicates the string is a link
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetLink(BOOL bLink,BOOL bNotifyParent)
{
m_bLink = bLink;
m_bNotifyParent = bNotifyParent; if (bLink)
ModifyStyle(0,SS_NOTIFY);
else
ModifyStyle(SS_NOTIFY,0); return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetLinkCursor
//
// Description: Sets the internet browers link
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetLinkCursor(HCURSOR hCursor)
{
m_hCursor = hCursor;
return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetTransparent
//
// Description: Sets the A16Label window to be transpaent
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetTransparent(BOOL bSet)
{
m_bTransparent = bSet;
InvalidateRect(NULL,TRUE);
UpdateWindow(); return *this;
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetFont3D
//
// Description: Sets the 3D attribute of the font.
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetFont3D(BOOL bSet,Type3D type)
{
m_bFont3d = bSet;
m_3dType = type;
RedrawWindow(); return *this;
}void CA16Label::OnSysColorChange()
{
// CStatic::OnSysColorChange();
if (m_hwndBrush)
::DeleteObject(m_hwndBrush); m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
RedrawWindow();
}//////////////////////////////////////////////////////////////////////////
//
// Function: CA16Label::SetRotationAngle
//
// Description: Sets the 3D attribute of the font.
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetRotationAngle(UINT nAngle,BOOL bRotation)
{
// Arrrrh...
// Your looking in here why the font is rotating, aren't you?
// Well try setting the font name to 'Arial' or 'Times New Roman'
// Make the Angle 180 and set bRotation to true.
//
// Font rotation _ONLY_ works with TrueType fonts...
//
//
m_lf.lfEscapement = m_lf.lfOrientation = (nAngle * 10);
m_bRotation = bRotation;
ReconstructFont();
RedrawWindow(); return *this;
}
LONG lStyle = GetWindowLong(p->m_hWnd, GWL_STYLE);
lStyle |= SS_SUNKEN;
SetWindowLong(p->m_hWnd, GWL_STYLE, lStyle);
p->Invalidate();
替换为:
CStatic* p = (CStatic*)GetDlgItem(IDC_STATIC_TEST);
p->ModifyStyleEx(0,WS_EX_STATICEDGE,SWP_DRAWFRAME);