⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nstatic.cpp

📁 EVC环境下用SDK开发WINCE的应用程序
💻 CPP
字号:
// NStatic.cpp: implementation of the CNStatic class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "NStatic.h"
#include "UICommon.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CNStatic::CNStatic()
{
//	m_bTransparent = FALSE;
	m_hBitmap = NULL;
	// default text color, style
	m_crTextColor = RGB(0,0,0);
	m_dwTextStyle = DT_LEFT;
//	m_crBkgndColor = KEY_COLOR;
}

CNStatic::~CNStatic()
{
	if( m_hBitmap )
	{
		DeleteObject(m_hBitmap);
		m_hBitmap = NULL;
	}
}

BOOL CNStatic::Create(LPCTSTR lpszText, DWORD dwStyle,
				const RECT& rect, CNWnd* pParentWnd, UINT nID)
{
	BOOL bResult;
	CNWnd* pWnd = this;
	bResult = pWnd->Create(_T("STATIC"), lpszText, dwStyle, rect, pParentWnd, nID);
	if(!bResult)
		return FALSE;

	// Set default font.
	SetFont(NGetNavFont(), FALSE);
	return TRUE;
}


HICON CNStatic::SetIcon(HICON hIcon)
{
	HICON hOldIcon;
	ASSERT(::IsWindow(m_hWnd));
#ifdef _DEBUG
	DWORD dwStyle;
	dwStyle = (DWORD)GetWindowLong(GetSafeHwnd(), GWL_STYLE);
	// the control must have the proper style. 
	ASSERT(SS_ICON & dwStyle);
#endif
	hOldIcon = (HICON)SendMessage(STM_SETIMAGE, (WPARAM)IMAGE_ICON , (LPARAM)hIcon);
	return hOldIcon;
}

HICON CNStatic::GetIcon() const
{
	HICON hIcon;
	ASSERT(::IsWindow(m_hWnd));
	hIcon = (HICON)SendMessage(STM_GETIMAGE, (WPARAM)IMAGE_ICON , (LPARAM)0);
	return hIcon;
}

HBITMAP CNStatic::SetBitmap(HBITMAP hBitmap)
{
	HBITMAP hOldBitmap;
	ASSERT(::IsWindow(m_hWnd));
#ifdef _DEBUG
	DWORD dwStyle;
	dwStyle = (DWORD)GetWindowLong(GetSafeHwnd(), GWL_STYLE);
	// the control must have the proper style. 
	ASSERT(SS_BITMAP & dwStyle);
#endif
	// the calling function is responsible for deleting the old bitmap returned 
	// by this function.
	hOldBitmap = (HBITMAP)SendMessage(STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);
	m_hBitmap = hBitmap;
	return hOldBitmap;
}

HBITMAP CNStatic::GetBitmap() const
{
	HBITMAP hBitmap;
	ASSERT(::IsWindow(m_hWnd));
	hBitmap = (HBITMAP)SendMessage(STM_GETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)0);
	return hBitmap;
}

void CNStatic::PreSubclassWindow(void)
{
	CNWnd::PreSubclassWindow();

	// If it's a bitmap static control, get the bitmap handle
	// loaded by windows.
	DWORD dwStyle = ::GetWindowLong(GetSafeHwnd(), GWL_STYLE);
	if ( dwStyle & SS_BITMAP )
	{
		m_hBitmap = GetBitmap();
	}
#ifdef PC_VERSION
	if ( dwStyle & SS_BITMAP )
		dwStyle &= ~SS_BITMAP;

	dwStyle |= SS_OWNERDRAW;
	SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle);
#endif
}

void CNStatic::OnDrawItem(int /*nIDCtl*/, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	OnDraw(lpDrawItemStruct->hDC);
	return;
}


/*
BOOL CNStatic::OnEraseBkgnd(HDC hdc)
{
	return CNWnd::OnEraseBkgnd(hdc);
	RECT	rcClient;
//	hdc = GetWindowDC();
	// don't erase bkgnd when it's a bitmap control
	if ( !m_bTransparent && m_hBitmap == NULL )
	{
		GetClientRect(&rcClient);
//		FillSolidRect(hdc, &rcClient, m_crBkgndColor);
	}
//	ReleaseDC(hdc);
	return TRUE;
}
*/

void CNStatic::OnDraw(HDC hDC)
{
	RECT	rect;
	GetClientRect(&rect);

	// draw bitmap
	HBITMAP		hOldBitmap;
	if ( m_hBitmap != NULL )
	{
		hOldBitmap = (HBITMAP)::SelectObject(NGetTempMemDC(), m_hBitmap);
		::BitBlt(hDC, rect.left, rect.top, rect.right-rect.left,rect.bottom-rect.top,
			NGetTempMemDC(), 0, 0, SRCCOPY);
		::SelectObject(NGetTempMemDC(), hOldBitmap);
	}

	// draw icon
	// 

	// draw text
	CNString	strCaption;
	GetWindowText(strCaption);
	if( !strCaption.IsEmpty() )
	{
		
		DWORD		dwStyle;
		int			nOldMode = ::SetBkMode(hDC, TRANSPARENT);
		COLORREF	crOldColor = ::SetTextColor(hDC, m_crTextColor);
		
		// reserve the margin of the static control.
		InflateRect(&rect, -3, -3);
		
		if ( strCaption.Find(_T("\n")) == -1 )	// single line
			dwStyle = m_dwTextStyle|DT_VCENTER|DT_SINGLELINE;
		else  // mutiline	
			dwStyle = m_dwTextStyle;
		
		::DrawText(hDC, strCaption,strCaption.GetLength(), 
			&rect, dwStyle);
		
		// resore hDC
		::SetBkMode(hDC,nOldMode);
		::SetTextColor(hDC, crOldColor);
	}
	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -