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

📄 yctrls.cpp

📁 Windows mobile下的透明控件(皮肤控件) 当前Windows mobile下的皮肤控件还很少
💻 CPP
📖 第 1 页 / 共 2 页
字号:

#include "YCtrls.h"

/*********************************************************************************************
*								CWin														 *
*																							 *
*		YCode Y代码 YCtrl Y控件 Windows Mobile 透明控件 2008 YCtrl 1.0						 *
*			作者:卢益贵 QQ:48092788 luyigui.blog.nnsky.com									 *
*																							 *
**********************************************************************************************/
CWin::CWin()
{
	m_cidClassID = CID_Y_WIN;
	m_hWnd = NULL;
	m_nLeft = 0;
	m_nTop = 26;

	m_nWidth = 240;
	m_nHeigth = 320 - 26;
	m_wsWinState = WS_Y_NORMAL;
	m_rcClientRect.left = 0;
	m_rcClientRect.top = 0;
	m_rcClientRect.right = m_nWidth;
	m_rcClientRect.bottom = m_nHeigth;
	m_hBitmap = NULL;
	m_hDC = NULL;
	m_pCtrls = new CList;
	m_pParent = NULL;
	m_pOwner = NULL;
	m_bEnabled = TRUE;
	m_bStretch = TRUE;
	m_pszText = NULL;
	m_nTextLen = 0;
	m_hFont = NULL;
	m_clTextColor = 0xFFFF00;
	m_dwMouseState = 0;
	m_hPen = NULL;
}

CWin::~CWin()
{
	CWin *pWin;
	while( GetCtrlCount() )
	{
		pWin = GetCtrl( 0 );
		delete pWin;
	}

	if( m_pParent )
		m_pParent->DeleteCtrl( this );

	if( m_pCtrls )
		delete m_pCtrls;

    if( m_hWnd )
        DestroyWindow( m_hWnd );
	if( m_hBitmap )
		 DeleteObject( m_hBitmap );
	if( m_hDC )
		DeleteDC( m_hDC );
	
	if( m_pszText )
		free( (void *)m_pszText );
	if( m_hFont )
		DeleteObject( m_hFont );
	if( m_hPen )
		DeleteObject( m_hPen );
}

void CWin::OnCreated( void )
{
}

void CWin::CreateStyle( pCREATESTYLE pCreateStyle )
{
	pCreateStyle->dwStyle = WS_VISIBLE;
	pCreateStyle->dwExStyle = 0;
}

void CWin::CreateBitmap( void )
{
	if( !m_hWnd )
		return;
	if( m_hBitmap )
		 DeleteObject( m_hBitmap );
	if( !m_hDC )
		m_hDC = CreateCompatibleDC( GetDC( m_hWnd ) );
	if( m_hBitmap )
		 DeleteObject( m_hBitmap );
	m_hBitmap = CreateCompatibleBitmap( GetDC( m_hWnd ), m_nWidth, m_nHeigth );
	SelectObject( m_hDC, m_hBitmap );
}

BOOL CWin::Create( CWin *pOwner, CWin *pParent, HMENU hMenu, int nLeft, int nTop, int nWidth, int nHeigth, 
				   LPCWSTR lpszClassName, LPCWSTR lpszCaption )
{
	HWND			hParent;

	m_pParent = pParent;
	m_pOwner = pOwner;
	if( NULL == pParent )
		hParent = NULL;
	else
	{
		hParent = pParent->GetHandle();
		m_pParent->AddCtrl( this );
	}

	CREATESTYLE	cs;
	CreateStyle( &cs );
    m_hWnd = CreateWindowEx( cs.dwExStyle, lpszClassName,  lpszCaption,  cs.dwStyle, 
							 nLeft, nTop, nWidth, nHeigth, hParent,  hMenu, g_pApp->GetInstance(), (PVOID)this );
	
	m_nTextLen = lstrlen( lpszCaption );
	m_pszText = (WCHAR *)malloc( m_nTextLen * 2 + 2 );
	lstrcpy( m_pszText, lpszCaption );
	if( m_hWnd )
	{
		m_nLeft = nLeft;
		m_nTop = nTop;
		m_nWidth = nWidth;
		m_nHeigth = nHeigth;
		GetClientRect( m_hWnd, &m_rcClientRect );
		SetWindowLong( m_hWnd, GWL_USERDATA, (LONG) this );
		if( SetWindowLong( m_hWnd, GWL_USERDATA, (LONG) this ) )
		{
			GetObject( (HFONT) GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &m_lFont );
			m_hFont = CreateFontIndirect( &m_lFont );
			CreateBitmap();
			OnCreated();
			OnDraw();
			return TRUE;
		}
	}
	
	return FALSE;		
}

LRESULT CWin::WindowProc( HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	LRESULT r;

	switch( uMsg )
    {
		case WM_PAINT:
			OnPaint( hWnd );
			return 0;
		case WM_LBUTTONDOWN:
			m_dwMouseState = MS_Y_DOWN;
			return DefWindowProc( hWnd, uMsg, wParam, lParam );
		case WM_LBUTTONUP:			
			r = DefWindowProc( hWnd, uMsg, wParam, lParam );
			if( (m_dwMouseState & MS_Y_DOWN) && (((short)LOWORD(lParam)) >= 0) && (((short)LOWORD(lParam))  <= m_nWidth) && 
				(((short)HIWORD(lParam)) >= 0) && (((short)HIWORD(lParam)) <= m_nHeigth) )
				SendMessage( m_hWnd, WM_LBUTTONCLICK, 0, 0);
			m_dwMouseState &= (~(MS_Y_DOWN | MS_Y_MOVE));
			return r;
		case WM_MOUSEMOVE:
			if( (((short)LOWORD(lParam)) < 0) || (((short)LOWORD(lParam)) > m_nWidth) || (((short)HIWORD(lParam)) < 0) || (((short)HIWORD(lParam)) > m_nHeigth) )
			{
				m_dwMouseState &= (~MS_Y_MOVE);
				SendMessage( m_hWnd, WM_MOUSELEAVE, 0, 0);				
			}else
				m_dwMouseState |= MS_Y_MOVE;
			return DefWindowProc( hWnd, uMsg, wParam, lParam );
		case WM_MOVE:
			m_wsWinState = WS_Y_MOVE;
			m_nLeft = (int)LOWORD(lParam);
			m_nTop = (int)HIWORD(lParam);
			r = DefWindowProc( hWnd, uMsg, wParam, lParam );
			if( m_pParent )
				m_pParent->OnPaint();
			ChangeSkin();
			return r;
		case WM_SIZE:
			switch( wParam )
			{
				case SIZE_MINIMIZED:
					m_wsWinState = WS_Y_MIN;
					break;
				case SIZE_MAXIMIZED:
					m_wsWinState = WS_Y_MAX;
					break;
				case SIZE_MAXSHOW:
					m_wsWinState = WS_Y_MAXSHOW;
					break;
				case SIZE_MAXHIDE:
					m_wsWinState = WS_Y_MAXHIDE;
					break;
				default:
					m_wsWinState = WS_Y_NORMAL;
			}
			m_nWidth = (int)LOWORD(lParam);
			m_nHeigth = (int)HIWORD(lParam);
			GetClientRect( hWnd, &m_rcClientRect );
			r = DefWindowProc( hWnd, uMsg, wParam, lParam );			
			if( m_pParent )
				m_pParent->OnPaint();
			CreateBitmap();
			ChangeSkin();
			return r;
		case WM_GETTEXT:
			if( m_nTextLen < (int) wParam )
			{
				memcpy( (void *)lParam, (void *)m_pszText, (int) m_nTextLen * 2 );
				((WCHAR *)lParam)[(int) m_nTextLen] = 0;
			}else
			{
				memcpy( (void *)lParam, (void *)m_pszText, (int) wParam * 2 );
				((WCHAR *)lParam)[(int) wParam] = 0;
			}
			return 0;
		case WM_SETTEXT:
			if( m_pszText )
				free( (void *)m_pszText );
			m_nTextLen = lstrlen( (WCHAR *)lParam );
			m_pszText = (WCHAR *) malloc( m_nTextLen + 2 );
			lstrcpy( m_pszText, (WCHAR *)lParam );
			return 0;
		case WM_GETTEXTLENGTH:
			return m_nTextLen;
        default:
			return DefWindowProc( hWnd, uMsg, wParam, lParam );
    }	
}

LRESULT CALLBACK CWin::WinProc( HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	CWin *pThis;

    pThis = (CWin*) GetWindowLong( hWnd, GWL_USERDATA );
    if( pThis )
    {
        return pThis->WindowProc( hWnd, uMsg, wParam, lParam );
    }

    return DefWindowProc( hWnd, uMsg, wParam, lParam );
}

BOOL CWin::RegClass( LPCWSTR lpszClassName )
{
	WNDCLASS    wc;

	ZeroMemory( &wc, sizeof( wc ));
	wc.lpfnWndProc = CWin::WinProc;
    wc.hInstance = g_pApp->GetInstance();
    wc.lpszClassName = lpszClassName;
	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	if( NULL == RegisterClass( &wc ))
        return	FALSE;
	else
		return TRUE;
}

void CWin::OnDraw( void )
{
}

void CWin::OnPaint( HDC hdc )
{
	BitBlt( hdc, 0, 0, m_nWidth, m_nHeigth, m_hDC, 0, 0, SRCCOPY );
}

void CWin::OnPaint( HWND hWnd )
{
	HDC hdc;
	PAINTSTRUCT ps;
	hdc = BeginPaint(hWnd, &ps);
	OnPaint( hdc );	
	EndPaint(hWnd, &ps);
}

void CWin::OnPaint( void )
{
	HDC hdc;

	hdc = GetDC( m_hWnd );
	OnPaint( hdc );	
	ReleaseDC( m_hWnd, hdc );
}

void CWin::Invalidate( void )
{
	if( m_hWnd )
	{
		OnDraw();
		OnPaint();
		//InvalidateRect( m_hWnd , &m_rcClientRect, TRUE );
	}
}

void CWin::GetCtrlBKGND( CWin *pCtrl, HDC hdc )
{
	POINT p;
	p.x = 0;
	p.y = 0;
	ClientToScreen( pCtrl->GetHandle(), &p);
	ScreenToClient( m_hWnd, &p);
	BitBlt( hdc, 0, 0, pCtrl->GetWidth(), pCtrl->GetHeigth(), m_hDC, p.x, p.y, SRCCOPY );
}

HWND CWin::GetHandle(void)
{
	return m_hWnd;
}

void CWin::SetParent( CWin *pParent )
{
	if( (m_pParent != pParent) && pParent )
	{
		if( m_pParent )
			m_pParent->DeleteCtrl( this );
		m_pParent = pParent;
		m_pParent->AddCtrl( this );
		::SetParent( m_hWnd, m_pParent->GetHandle() );
		Invalidate();
	}
}

CWin *CWin::GetParent( void )
{
	return m_pParent;
}

CWin *CWin::GetOwner( void )
{
	return m_pOwner;
}

int CWin::GetLeft( void )
{
	return m_nLeft;
}

int CWin::GetTop( void )
{
	return m_nTop;
}

int CWin::GetWidth( void )
{
	return m_nWidth;
}

int CWin::GetHeigth( void )
{
	return m_nHeigth;
}

void CWin::SetPos( int nLeft, int nTop, int nWidth, int nHeigth )
{
	m_nLeft = nLeft;
	m_nTop = nTop;
	m_nWidth = nWidth;
	m_nHeigth = nHeigth;
	if( m_pParent )
		m_pParent->OnPaint();
	MoveWindow( m_hWnd, nLeft, nTop, nWidth, nHeigth, TRUE );
	Invalidate();
}

void  CWin::AddCtrl( CWin *pCtrl )
{
	m_pCtrls->Add( pCtrl );
}

void  CWin::DeleteCtrl( CWin *pCtrl )
{
	m_pCtrls->Delete( pCtrl );
}

int  CWin::GetCtrlCount( void )
{
	return m_pCtrls->GetCount();
}

CWin *CWin::GetCtrl( int nIndex )
{
	return (CWin *)m_pCtrls->GetItem( nIndex );
}

void CWin::SetVisible( BOOL bVisible )
{
	if( bVisible != GetVisible() )
		ShowWindow( m_hWnd, bVisible );
}

BOOL CWin::GetVisible( void )
{
	return IsWindowVisible( m_hWnd );
}

void CWin::SetEnabled( BOOL bEnabled )
{
	if( bEnabled != GetEnabled() )
	{
		EnableWindow( m_hWnd, bEnabled );
		OnDraw();
		OnPaint();
	}
}

BOOL CWin::GetEnabled( void )
{
	return IsWindowEnabled( m_hWnd );
}

void CWin::SetStretch( BOOL bStretch )
{
	if( m_bStretch != bStretch )
	{
		m_bStretch = bStretch;
		ChangeSkin();
	}
}

BOOL CWin::GetStretch( void )
{
	return m_bStretch;
}

void CWin::ChangeSkin( void )
{
	int i;	
	Invalidate();
	for( i = 0; i < GetCtrlCount(); i++ )
		GetCtrl( i )->ChangeSkin();
}

LOGFONT CWin::GetFont( void )
{
	return m_lFont;
}

void CWin::SetFont( LOGFONT * lFont )
{
	m_lFont = *lFont;
	if( m_hFont )
		DeleteObject(m_hFont);
	m_hFont = CreateFontIndirect( &m_lFont );
	SelectFont( GetDC( m_hWnd ), m_hFont );
	Invalidate();
}

void CWin::SetTextColor( COLORREF clTextColor )
{
	if( m_clTextColor != clTextColor )
	{
		m_clTextColor = clTextColor;
		Invalidate();
	}
}

COLORREF CWin::GetTextColor( void )
{
	return m_clTextColor;
}

LPCWSTR CWin::GetText( void )
{
	return m_pszText;
}

int CWin::GetTextLen( void )
{
	return m_nTextLen;
}

void CWin::SetText( LPCWSTR lpszText )
{
	SendMessage( m_hWnd, WM_SETTEXT, lstrlen( lpszText ), (UINT) lpszText );
}


DWORD CWin::GetClassID( void )
{
	return m_cidClassID;
}

/*********************************************************************************************
*								CForm														 *
*																							 *
*		YCode Y代码 YCtrl Y控件 Windows Mobile 透明控件 2008 YCtrl 1.0						 *
*			作者:卢益贵 QQ:48092788 luyigui.blog.nnsky.com									 *
*																							 *
**********************************************************************************************/
CForm::CForm()
{
	g_pApp->AddForm( this );
	m_fsFormStyle = FS_FORM;
	m_bOnMinQuit = FALSE;
	m_cidClassID = CID_Y_FORM;
}


CForm::~CForm()
{
	g_pApp->DeleteForm( this );
}

void CForm::CreateStyle( pCREATESTYLE pCreateStyle )
{
	pCreateStyle->dwStyle = CS_HREDRAW | CS_VREDRAW;
	pCreateStyle->dwExStyle = 0;
}

BOOL CForm::Create( HMENU hMenu, int nLeft, int nTop, int nWidth, int nHeigth, LPCWSTR lpszClassName, LPCWSTR lpszCaption )
{
	RegClass( lpszClassName );
	return CWin::Create( NULL, NULL, hMenu, nLeft, nTop, nWidth, nHeigth, lpszClassName, lpszCaption);
}

void CForm::Show( int nCmdShow )
{
	ShowWindow( m_hWnd, nCmdShow );
    UpdateWindow( m_hWnd );
}

void CForm::OnDraw( void )
{
	if( WS_Y_MOVE != m_wsWinState )
		g_pApp->DrawCtrlSkin( m_hDC, &m_rcClientRect, CII_FORM_CLIENT, m_bStretch );
}  

void CForm::SetFormStyle( FORMSTYLE fsFormStyle, BOOL bTop )
{
	int nl, nt, nw, nh;
	DWORD dwFlag = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE;
	HWND hTop = HWND_NOTOPMOST;
	nl = m_nLeft;
	nt = m_nTop;
	nw = m_nWidth;
	nh = m_nHeigth;
	if( fsFormStyle != m_fsFormStyle )
	{
		m_fsFormStyle = fsFormStyle;

		switch( m_fsFormStyle )
		{
			case FS_FULLSCREEN:
				nl = 0;
				nt = 0;
				nw = GetSystemMetrics( SM_CXSCREEN );
				nh = GetSystemMetrics( SM_CYSCREEN );
				dwFlag &= (~(SWP_NOMOVE | SWP_NOSIZE));
				break;
			case FS_SCREENCLIENT:
				nl = 0;
				nt = MENU_HEIGHT;
				nw = GetSystemMetrics( SM_CXSCREEN );
				nh = GetSystemMetrics( SM_CYSCREEN ) - MENU_HEIGHT * 2;
				dwFlag &= (~(SWP_NOMOVE | SWP_NOSIZE));
				break;
			case FS_SCREENCLIENT_NOMENU:
				nl = 0;
				nt = MENU_HEIGHT;
				nw = GetSystemMetrics( SM_CXSCREEN );
				nh = GetSystemMetrics( SM_CYSCREEN ) - MENU_HEIGHT;
				dwFlag &= (~(SWP_NOMOVE | SWP_NOSIZE));
				break;
			case FS_SCREENCENTER:
				nl = ( GetSystemMetrics( SM_CXSCREEN ) - m_nWidth) / 2;
				nt = ( GetSystemMetrics( SM_CYSCREEN ) - m_nHeigth) / 2;
				dwFlag &= (~SWP_NOMOVE);
				break;
		} 
	}
	if( bTop )
		hTop = HWND_TOPMOST;

	SetWindowPos( m_hWnd, hTop, nl, nt, nw, nh, dwFlag);
}

LRESULT CForm::WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	switch( uMsg )
    {
		case WM_SIZE:
			if( (SIZE_MINIMIZED == wParam) && m_bOnMinQuit )
			{
				g_pApp->Terminate();
				return 0;
			}
        default:
			return CWin::WindowProc( hWnd, uMsg, wParam, lParam );

⌨️ 快捷键说明

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